I\'ve browsed a lot of Web Socket examples, presentation slides and they are mostly concentrated on a rather simple scenarios in which client-server communication is initiat
Probably this is not most elegant way but just to demonstrate idea. Method broadcast() will send message to all connected clients.
@ServerEndpoint("/echo")
public class ServerEndPoint {
private static Set userSessions = Collections.newSetFromMap(new ConcurrentHashMap());
@OnOpen
public void onOpen(Session userSession) {
userSessions.add(userSession);
}
@OnClose
public void onClose(Session userSession) {
userSessions.remove(userSession);
}
@OnMessage
public void onMessage(String message, Session userSession) {
broadcast(message);
}
public static void broadcast(String msg) {
for (Session session : userSessions) {
session.getAsyncRemote().sendText(msg);
}
}
}