Closed Doyuni closed 4 years ago
Thank you @Doyuni
- I want to handle websocket endpoint well. ex) URI: "/websocket" or "/websocket?encoding=text" or "/websocket/example" These endpoints all mean "request websocket"?
@Doyuni
Hi, I do not understand I want to handle endpoint well
- I want to add WebSocketServerProtocolHandler but It can not handle http message in Echo Test (https://www.websocket.org/echo.html). Because this handler replaces HttpServerCodec into WebSocketFrameCodec. If I use this handler, I don't need to write handshake method.
@Doyuni WebSocketServerProtocolHandler check if handshake conditions:
String websocketPath = serverConfig.websocketPath();
// serverConfig.checkStartsWith() ==> default value is false
// ==> your echo test page send url like websocket?encoding=text ==> skip handshake ==> return 404
return serverConfig.checkStartsWith() ? !req.uri().startsWith(websocketPath) : !req.uri().equals(websocketPath);
If want using WebSocketServerProtocolHandler, following the codes in DssWebSocketChannelInitializer
@Override
protected void initChannel(SocketChannel ch) throws Exception {
logger.info("Try to initChannel");
final ChannelPipeline p = ch.pipeline();
p.addLast(new HttpServerCodec());
p.addLast(new HttpObjectAggregator(65536));
p.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, true)); // set serverConfig.checkStartsWith() as true
p.addLast(new DssHttpRequestHandler(WEBSOCKET_PATH));
p.addLast(new DssWebSocketHandler());
}
At last modify DssHttpRequestHandler channelRead0, you can pass the tests.
Hi @Doyuni , After pass the coveralls test coverage, i will merge this pr. Thx.
- I want to handle websocket endpoint well. ex) URI: "/websocket" or "/websocket?encoding=text" or "/websocket/example" These endpoints all mean "request websocket"?
@Doyuni Hi, I do not understand
I want to handle endpoint well
It means how do I check websocket uri well.
I use this way.
int baseIndex = uri.indexOf("?");
boolean checkEndPoint = baseIndex == -1 ? WEBSOCKET_PATH.equals(uri) : WEBSOCKET_PATH.equals(uri.substring(0, baseIndex));
So, It recognizes follow uri : /websocket
or /websocket?encoding=text
or /websocket?userId=doyuni
not /websocketabc
or /websocket/hello
.
- I want to add WebSocketServerProtocolHandler but It can not handle http message in Echo Test (https://www.websocket.org/echo.html). Because this handler replaces HttpServerCodec into WebSocketFrameCodec. If I use this handler, I don't need to write handshake method.
@Doyuni WebSocketServerProtocolHandler check if handshake conditions:
String websocketPath = serverConfig.websocketPath(); // serverConfig.checkStartsWith() ==> default value is false // ==> your echo test page send url like websocket?encoding=text ==> skip handshake ==> return 404 return serverConfig.checkStartsWith() ? !req.uri().startsWith(websocketPath) : !req.uri().equals(websocketPath);
If want using WebSocketServerProtocolHandler, following the codes in DssWebSocketChannelInitializer
@Override protected void initChannel(SocketChannel ch) throws Exception { logger.info("Try to initChannel"); final ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(65536)); p.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, true)); // set serverConfig.checkStartsWith() as true p.addLast(new DssHttpRequestHandler(WEBSOCKET_PATH)); p.addLast(new DssWebSocketHandler()); }
At last modify DssHttpRequestHandler channelRead0, you can pass the tests.
Thank you for reviewing.
I'm wondering which one to choose. (use or not that handler)
If use this handler, It supports handshake and benefits performance a little bit.
But It recognizes follow uri too : /websocket/
or /websocketabcd
or/websocket?encoding=text
.
Because It checks only startWith.
Hi @Doyuni , After pass the coveralls test coverage, i will merge this pr. Thx.
Okay, I'm writing test code.
I added websocket handler first. I will add service interface later.
Websocket Process
DssHttpRequestHandler.java
Sending and Receiving Data Frame (Current: Echo message) : It occurs in
DssWebSocketHandler.java
Close connection
I tested it works fine using Jetty websocket client or Echo Test (https://www.websocket.org/echo.html)
Here is test logs.
Code Review
WebSocketServerProtocolHandler
but It can not handle http message in Echo Test (https://www.websocket.org/echo.html). Because this handler replaces HttpServerCodec into WebSocketFrameCodec. If I use this handler, I don't need to write handshake method.Thank you for reading.