Skip to content

Instantly share code, notes, and snippets.

@wnds
Created April 20, 2017 17:38
Show Gist options
  • Save wnds/a60a21de8b95fab624619d8a9b0abf66 to your computer and use it in GitHub Desktop.
Save wnds/a60a21de8b95fab624619d8a9b0abf66 to your computer and use it in GitHub Desktop.
package org.vivek.j2ee.websockets.server;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@ServerEndpoint(value = "/echo") public class EchoWebSocketEndPoint {
private static List < Session > connectedSessions = new LinkedList < > ();
@OnMessage public void getMessage(final String message, final Session session) {
if (!connectedSessions.contains(session)) {
connectedSessions.add(session);
broadcastToAll("user" + session.getId() + " joined");
} else {
broadcastToAll("user" + session.getId() + " says > " + message);
}
}
private void broadcastToAll(final String s) {
for (Session session: connectedSessions) {
try {
session.getBasicRemote().sendText(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@OnClose public void closeConnectionHandler(Session session, CloseReason closeReason) {
connectedSessions.remove(session);
broadcastToAll("user" + session.getId() + " quit");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment