Here is my code snippets of ChannelInitializer#initChannel
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpServerCodec()
.addLast(new HttpObjectAggregator(65536))
.addLast( new LoggingHandler(LogLevel.INFO))
.addLast(new WebSocketServerProtocolHandler("/chat"))
.addLast(new TextWebSocketFrameToChatMessageDecoder())
.addLast(new UserAccessHandler())
It can be accepted via ws://mydomain/chat
, and now I want to parse query string like this ws://mydomain/chat?accesskey=hello
I have looked up WebSocketServerProtocolHandler
, but I couldn't find how to get query string.
How can I get(or parse) query string? Thanks for your help.
I have created 3 new classes, copied them.
WebSocketServerProtocolHandler
WebSocketServerProtocolHandshakeHandler
WebSocketProtocolHandler
And in copy of WebSocketServerProtocolHandshakeHandler, added these code
if(!req.getUri().matches(websocketPath)){
ctx.fireChannelRead(msg);
return;
}
String [] splittedUri = req.getUri().split("\\?");
HashMap<String, String> params = new HashMap<String, String>();
if(splittedUri.length > 1){
String queryString = splittedUri[1];
for(String param : queryString.split("&")){
String [] keyValue = param.split("=");
if(keyValue != null && keyValue.length >= 2){
logger.trace("key = {}, value = {}", keyValue[0], keyValue[1]);
params.put(keyValue[0], keyValue[1]);
}
}
}
ctx.channel().attr(AttrKeys.getInstance().params()).set(params);
Now I can accpet multiple uri and use query string well. I think somebody will need this answer.