我确实对 jetty 8 websockets 有一个奇怪的问题。我发现了几个几乎显示相同代码的教程,但是当我尝试从本地 html 页面打开 websocket 时出现此错误:

2013-02-05 13:14:03.467:INFO:oejs.Server:jetty-8.1.9.v20130131
2013-02-05 13:14:03.770:INFO:oejs.AbstractConnector:Started [email protected]:8080
2013-02-05 13:14:18.002:WARN:oejs.ServletHandler:/echo
java.lang.NullPointerException
    at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:236)
    at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:382)
    at org.eclipse.jetty.websocket.WebSocketServlet.service(WebSocketServlet.java:104)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)

这是 Socket servlet:

public class ExampleWebSocketServlet extends WebSocketServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see WebSocketServlet#WebSocketServlet()
     */
    public ExampleWebSocketServlet() {
        super();
    }



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("OK");
        out.close();
        System.out.println("OK");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getServletContext().getNamedDispatcher("default").forward(request, response);
    }


    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
        return new EchoWebSocket();
    }
}

和 WebSocket(用于简单的 Echo)

public class EchoWebSocket implements WebSocket.OnTextMessage {

    private Connection con;

    @Override
    public void onClose(int closeCode, String message) {

    }

    @Override
    public void onOpen(Connection con) {
        this.con = con;

        try {
            con.sendMessage("Server received Web Socket upgrade ...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(String msg) {
        System.out.println("Received: "+msg);

        try {
            con.sendMessage(msg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //send it back

    }
}

我的 html 页面与其他 websocket 测试服务器一起工作正常,例如websocket.org

可能有什么问题?我在 Eclipse 上,创建了一个动态 Web 项目并使用了 jetty webapp 运行配置。此外,当 jetty 启动时,我可以看到

/Users/sven.haiges/dev/jetty/jetty-distribution-8.1.9.v20130131/lib/jetty-websocket-8.1.9.v20130131.jar

在类路径上。

任何帮助表示赞赏,谢谢!

最佳答案

我对 Run Jetty Run 插件有同样的问题。终于找到了解决办法。
去运行配置

  • 在左侧面板的 jetty webapp 下选择您的项目名称
  • 在右侧的 Jetty Tab 下选择 jetty 版本 8.1

  • 应用/运行
    就这样

    关于websocket - Jetty 8.1.9.v20130131 Web 套接字问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14707694/

    10-15 12:32