Jetty 8.1.9.v20130131 Web Socket issues

大兔子大兔子 提交于 2019-12-07 14:32:51

问题


I do have a strange problem with jetty 8 websockets. I found several tutorials that pretty much show the same code, yet I get this error when I try to open the websocket from a local html page:

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 SelectChannelConnector@0.0.0.0: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)

Here is the 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();
    }
}

and the WebSocket (for a simple 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

    }
}

My html page works fine with other websocket test servers, e.g. websocket.org

What could be wrong? I am on Eclipse, created a dynamic web project and use the jetty webapp run configuration. Also, when jetty starts up i can see that

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

is on the classpath.

Any help appreciated,thx!


回答1:


I had a same problem with Run Jetty Run plugin. finally found solution. go to run configuration

  • select your project name under jetty webapp on left panel
  • select jetty version 8.1 on right side under Jetty Tab

Apply / Run that's all




回答2:


Try using the Jetty 8.1.8 version. Today, I had an issue creating a simple servlet via @WebServlet annotation. The jetty server did not respond and I could not figure out why. But when I suddenly changed back to version 8.1.8, everything worked fine. Maybe it's a similar problem.



来源:https://stackoverflow.com/questions/14707694/jetty-8-1-9-v20130131-web-socket-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!