Netty:如何在Websocket中使用查询字符串?

编程入门 行业动态 更新时间:2024-10-24 16:24:33
本文介绍了Netty:如何在Websocket中使用查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的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())

它可以通过ws://mydomain/chat接受,现在我想解析这样的查询字符串ws://mydomain/chat?accesskey=hello

It can be accepted via ws://mydomain/chat, and now I want to parse query string like this ws://mydomain/chat?accesskey=hello

我查询了WebSocketServerProtocolHandler,但是找不到如何获取查询字符串.

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.

推荐答案

我创建了3个新类,并将它们复制了.

I have created 3 new classes, copied them.

WebSocketServerProtocolHandler WebSocketServerProtocolHandshakeHandler WebSocketProtocolHandler

WebSocketServerProtocolHandler WebSocketServerProtocolHandshakeHandler WebSocketProtocolHandler

在WebSocketServerProtocolHandshakeHandler的副本中,添加了这些代码

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

现在,我可以支持多个uri并很好地使用查询字符串. 我认为有人会需要这个答案.

Now I can accpet multiple uri and use query string well. I think somebody will need this answer.

更多推荐

Netty:如何在Websocket中使用查询字符串?

本文发布于:2023-10-27 02:56:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   Netty   Websocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!