2

我正在尝试在独立的 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 做什么?

4

0 回答 0