I am trying to write a servlet that uses org.apache.catalina.websocket.WebSocketServlet. I found an example websocket chat, but I canβt understand how I can specify the listening port for the websocket server (which is implemented in this servlet)? For example, I need to listen for clients connecting to a port 11337. But how can I put it?
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 the listening port. The official websocket documentation is also not very helpful.
So, I think it can be installed somewhere in the servlet settings, but cannot find where.
Does anyone have any ideas?
source
share