tomcat websocket servlet监听端口

时间:2013-07-24 14:23:36

标签: java tomcat servlets websocket

我正在尝试编写一个使用org.apache.catalina.websocket.WebSocketServlet的servlet。我找到了一个websocket聊天的例子,但我无法弄清楚如何为websocket服务器(在这个servlet中实现)指定监听端口?例如,我需要侦听端口11337上的客户端连接。但我怎么能表达这个呢?

好的,这是(简化的)代码:

public class TestServlet extends WebSocketServlet {
    private static final Logger logger = LoggerFactory.getLogger(TestServlet.class);

    public TestServlet() {
        logger.error("Initializing TestServlet");
    }

    @Override
    protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
        logger.error("New WS connection, subProtocol=" + subProtocol + ", request=" + request.getRequestURL());
        return new TestConnection();
    }

    private class TestConnection extends MessageInbound {

        @Override
        protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException {
            logger.error("onBinaryMessage");
        }

        @Override
        protected void onTextMessage(CharBuffer charBuffer) throws IOException {
            logger.error("onBinaryMessage: " + charBuffer);
            sendMessage("Test message");
        }

        public void sendMessage(String message) {
            WsOutbound outbound = this.getWsOutbound();
            CharBuffer cb = CharBuffer.wrap(message);
            try {
                outbound.writeTextMessage(cb);
            } catch (IOException e) {
                logger.error("failed to write outbound");
            }
        }
    }
}

我无法找到设置侦听端口的位置和方式。官方websocket文档也没有多大帮助。

所以我猜它可以在servlet设置中设置,但无法找到。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:4)

WebSocket旨在通过HTTP协议工作,因此它不会像普通的TCP Socket那样拥有不同的侦听端口,但它使用与其部署的相同的服务器端口。

所以在这种情况下,如果您使用的是tomcat服务器而不是websocket可能会使用端口8080(如果您没有明确修改)进行通信。您可能需要覆盖MessageInbound类的onOpen和onClose方法,以获得有关Connection建立和连接关闭的通知。有关详细信息,请参阅下面的示例代码。

public class IncomingMessageHandler extends MessageInbound {

    private WsOutbound myoutbound;
    public IncomingMessageHandler() {

    }

    @Override
    public void onOpen(WsOutbound outbound) {
        logger.info("Open Client.");
        this.myoutbound = outbound;
    }

    @Override
    public void onClose(int status) {
        logger.info("Close Client.");           
    }

    /**
     * Called when received plain Text Message
     */
    @Override
    public void onTextMessage(CharBuffer cb) throws IOException {

    }

    /**
     * We can use this method to pass image binary data, eventually !
     */
    @Override
    public void onBinaryMessage(ByteBuffer bb) throws IOException {

    }

    public synchronized void sendTextMessage(String message) {
        try {
            CharBuffer buffer = CharBuffer.wrap(message);
            this.getMyoutbound().writeTextMessage(buffer);
            this.getMyoutbound().flush();

        } catch (IOException e) {   
        }
    }

    /**
     * Set timeout in milliseconds, -1 means never
     */
    @Override
    public int getReadTimeout() {
        return -1;
    }

    public WsOutbound getMyoutbound() {
        return myoutbound;
    }
    public void setMyoutbound(WsOutbound myoutbound) {
        this.myoutbound = myoutbound;
    }
}