Jetty:没有调用Websocket事件

时间:2016-05-25 15:40:04

标签: java websocket jetty embedded-jetty jetty-9

我正在尝试在我的应用程序中创建一个自定义的“http”服务器嵌入Jetty(版本9)。

我现在面临的问题是没有调用Websocket事件。

这是我到目前为止(相关部分):

SocketServer的:

public class SocketServer
{

    public static void main(String[] args) throws Exception
    {

        Server server = new Server(9091);

        HandlerCollection handlerCollection = new HandlerCollection();

        ServletContextHandler sch = new ServletContextHandler();
        sch.addServlet(new ServletHolder(new RandomWebSocketServlet()), "");
        handlerCollection.addHandler(sch);

        server.setHandler(handlerCollection);
        server.start();
        server.join();          
    }
}

RandomWebSocketServlet:

public class RandomWebSocketServlet extends WebSocketServlet {

    private static final long serialVersionUID = -1134228059326523215L;

    public RandomWebSocketServlet() {
    }

    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.setCreator(new AdvancedSocketCreator());
    }

}

AdvancedSocketCreator:

public class AdvancedSocketCreator implements WebSocketCreator {

    public AdvancedSocketCreator() {
    }

    @Override
    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
        return new Handler();
    }

}

处理程序:

@WebSocket
public class Handler {

    protected Session session = null;

    public Handler() {
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        Log.log("WebSocket connected to client: "+session.getRemoteAddress().getHostName()+":"+session.getRemoteAddress().getPort());
        this.session = session;
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        Log.log("WebSocket error");
    }

    @OnWebSocketMessage
    public void onMessage(String message) {
        Log.log("Message: "+message);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        Log.log("WebSocket closed: "+statusCode+" - "+reason);
    }
}

当我尝试打开连接时(例如使用telnet),未调用@OnWebSocketConnect onConnect方法。

我尝试了许多方法,遵循许多互联网教程......但没有成功。

任何帮助?

0 个答案:

没有答案