私は本当に基本的な Websocket の例をスタンドアロンの Jetty で動作させようとしていますが、NullPointerException で立ち往生しています。
ここに私の2つのサーバークラスがあります:
@WebServlet("/MyWebSocketServlet")
public class MyWebSocketServlet extends WebSocketServlet {
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(MyWebSocket.class);
}
}
@WebSocket
public class MyWebSocket{
@OnWebSocketConnect
public void onConnect(Session session){
System.out.println("MyWebSocket.onConnect()");
}
@OnWebSocketMessage
public void onText(String msg) {
System.out.println("MyWebSocket.onText()");
}
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("MyWebSocket.onClose()");
}
}
Chrome で JavaScript を介して通信を確立しようとしています。
<script type="text/javascript">
var echoSocket = new WebSocket(
"ws://localhost:8080/JettyWebsocketsTest/MyWebSocketServlet"
);
echoSocket.onopen = function(e) {
console.log("onopen()");
echoSocket.send("Hello");
}
echoSocket.onmessage = function(e) {
console.log("msg received");
};
</script>
残念ながら、接続は確立されず、代わりにこの NullPointerException がスローされます。
java.lang.NullPointerException
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.upgrade(WebSocketServerFactory.java:400)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:168)
at org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:160)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:558)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:489)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:972)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:906)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
at org.eclipse.jetty.server.Server.handle(Server.java:350)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:910)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:565)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:46)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:545)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:43)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
at java.lang.Thread.run(Thread.java:722)
WebSocketServerFactory のこれらの行は、その例外をスローします。
HttpConnection http = HttpConnection.getCurrentConnection();
EndPoint endp = http.getEndPoint();
利用可能な HttpConnection に対して何ができますか?