Created
April 20, 2017 17:38
-
-
Save wnds/a60a21de8b95fab624619d8a9b0abf66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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