我正在尝试在独立的Jetty上创建一个真正基本的Websocket示例,但是我遇到了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做些什么?