@ServerEndpoint("/ws")
class WSHandler {
static final Set sessions = new HashSet<>();
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
}
@OnMessage
public void onMessage(String message, Session session) {
for (Session session : sessions) {
if (!session.equals(session)) {
session.getAsyncRemote().sendText(message);
}
}
}
}
class ChatWebSocket(websocket.WebSocketHandler):
connections = set()
def open(self):
self.connections.add(self)
def on_message(self, message):
[c.write_message(message) for c in self.connections if c is not self]
def on_close(self):
self.connections.remove(self)
Неужели так сложно?