Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Enabling WebSockets on Jersey (Tyrus)
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ServerEndpoint(value = "/echo")
public class WebSocketResource
private static final Logger log = LoggerFactory.getLogger(WebSocketResource.class);
* This method is invoked when the client closes a WebSocket connection.
* @param session
* @return
public void onClose(Session session)
{"[Session {}] Session has been closed.", session.getId());
* This method is invoked when an error was detected on the connection.
* @param session
* @param t
* @return
public void onError(Session session, Throwable t)
{"[Session {}] An error has been detected: {}.", session.getId(), t.getMessage());
* This method is invoked each time that the client receives a WebSocket message.
* @param message
* @param session
* @return
public String onMessage(String message, Session session)
{"[Session {}] Sending message: {}", session.getId(), message);
return message; // echo back the message received
* OnOpen (when a socket has been opened) allows us to intercept the creation of a new session.
* The session class allows us to send data to the user.
* In the method onOpen, we'll let the user know that the handshake was
* successful.
public void onOpen(Session session)
{"[Session {}] Session has been opened.", session.getId());
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
Copy link

benallard commented Oct 26, 2017

This is not related to jersey at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment