tomcat websocket servlet listening port

こ雲淡風輕ζ 提交于 2021-02-09 20:45:00

问题


I'm trying to write a servlet that uses org.apache.catalina.websocket.WebSocketServlet. I've found an example of websocket chat, but I can't figure out how can I specify the listening port for a websocket server (which is implemented in this servlet)? For example I need to listen for clients connection on port 11337. But how I can express this?

OK, here is the (simplified) code:

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");
            }
        }
    }
}

I can't find where and how I can set listening port. Official websocket documentation also doesn't help much.

So I guess it can be set somewhere in servlet settings, but can't find where.

Does anyone have any ideas?


回答1:


WebSocket is designed to work over HTTP protocol and so it won't have different listening port like normal TCP Socket but it use the same server port that it deployed on.

so here in your case, if you are using tomcat server than websocket may use port 8080 (if you haven't modified explicitly) for communication. You may need to override onOpen and onClose methods of MessageInbound class to get notified on Connection established and connection close. Refer below sample code for more details.

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;
    }
}


来源:https://stackoverflow.com/questions/17836806/tomcat-websocket-servlet-listening-port

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