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
Store the active sessionList
in another class SessionManager
.
List socketSessions = new ArrayList<>();
Add incoming session in @OnOpen
to list. Remove session from list in @OnClose
@OnClose
public void close(Session session) {
sessionManager.removeSession(session);
}
To send a message to everyone,
public void broadcast(String message){
for(Session session: sessionList){
session.getBasicRemote().sendText(message);
}
}
You can use the sessionManager.broadcast()
method wherever the event triggers.
Here is a complete example of how websocket can be used to make push with HTML5 websocket API. https://metamug.com/article/java-push-notification-with-websocket.php