I see lots of tutorials about how to set up JSR356 websockets with an embedded web server of some sort.
But I want to add some websockets to an existing WAR deployed to a stand alone jetty installation, jetty jetty-9.2.3.v20140905, and I can find very little information on this. I can't figure out what is preventing it from working. As I understand it, the annotated server endpoints should be automatically handled by jetty.
ServerEndpoint:
@ServerEndpoint(value = "/myendpoint")
public class MyServerEndpoint {
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session s) {
logger.info("Server Connected ... " + s.getId());
s.getAsyncRemote().sendText("You are connected to the websocket server");
}
@OnMessage
public void onMessage(String message, Session session) {
// Sent the message back to all connected users
logger.info("Server Session " + session + " Received string message " + message);
for(Session s: session.getOpenSessions()) {
if (s.isOpen()) {
s.getAsyncRemote().sendText(message + " server response");
}
}
}
@OnClose
public void onClose(Session session, CloseReason reason) {
logger.info("Server Session " + session + " closed for reason " + reason);
}
}
ClientEndpoint:
@ClientEndpoint
public class MyClientEndpoint {
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session s) {
logger.info("Client Connected ... " + s.getId());
s.getAsyncRemote().sendText("hello from the client!");
}
@OnMessage
public void onMessage(String message, Session session) {
logger.info("Client Session " + session + " Received string message " + message);
}
@OnClose
public void onClose(Session session, CloseReason reason) {
logger.info("Client Session " + session + " closed for reason " + reason);
}
}
Here is the code (run inside some existing method) to connect the client to the server
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri = "ws://localhost:8080/myWar/myendpoint";
container.connectToServer(MyClientEndpoint.class, URI.create(uri));
Yet spinning up jetty gives an error on the connectToServer line
2014-10-30 12:26:58.658:WARN:oeja.ServletContainerInitializersStarter:main:
org.eclipse.jetty.websocket.api.InvalidWebSocketException: Unable to instantiate websocket: class java.lang.Class
at org.eclipse.jetty.websocket.jsr356.ClientContainer.newClientEndpointInstance(ClientContainer.java:311)
at org.eclipse.jetty.websocket.jsr356.ClientContainer.connectToServer(ClientContainer.java:172)
All I can think is that the URI is incorrect. If jetty is running on 8080 and my .war is named myWar, and my end point is named myendpoint...is that not the correct URI?
Is there some additional step that must be done to 'activate' the server endpoint to listen for connections? I must be missing something obvious.