如何在Jetty中指定WebSocketServlet的URL?

时间:2012-02-10 00:37:28

标签: servlets annotations jetty websocket

我已阅读Creating a WebSocket-Chat-Application with Jetty,我想在Jetty上使用WebSocketServlet创建一个简单的echo websocket。

我已经创建了我的WebSocketServlet

public class ChatSocketServlet extends WebSocketServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html><html><body><h1>Chat</h1></body></html>");
    }

    @Override
    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
        return new ChatWebSocket();
    }

    class ChatWebSocket implements OnTextMessage {

        private Connection connection;

        @Override
        public void onClose(int closeCode, String message) {
            System.out.println("onClose");
        }

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

        @Override
        public void onMessage(String data) {
            try {
                connection.sendMessage(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

但是我应该使用什么URL连接到这个websocket?我似乎无法使用@WebServlet(例如@WebServlet("/HelloServlet")HttpServlet。我试图连接到:

ws://localhost:8080/MyJavaWeb/ChatWebSocket

但它会返回Not Found。是否有其他注释来指定WebSocketServlet的网址?我也不知道从这个servlet上获取doGet输出的URL。

0 个答案:

没有答案