+
 新版

Netty 作为 websocket 客户端连接多个地址,如何处理不同 url

bage兔斯基 发布于 2020/06/01 15:30
阅读 3K+
收藏 0
//现在要在客户端连接多个 websocket 连接
Bootstrap bootstrap = new Bootstrap()
        .group(workGroup)
        .channel(NioSocketChannel.class)
        .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
        .handler(initializer);
for (URI url : urls) {
    ChannelFuture channelFuture = bootstrap.connect(url.getHost(), getPort(url)).sync();
    //market of channel
    String channelId = channelFuture.channel().id().asLongText();
    Monitor.getStatus(channelId).setUrl(url.toString());
    channelFuture.channel().closeFuture().sync();
}
//在设置websocket握手处理器时 无法获取上面的 url remoteAddress  报空指针
InetSocketAddress remoteAddress = socketChannel.remoteAddress();
URI uri = URIUtil.of(remoteAddress);
WebSocketClientProtocolHandler webSocketClientProtocolHandler = new WebSocketClientProtocolHandler(
        WebSocketClientHandshakerFactory.newHandshaker(
                uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()
        )
);

哪位大佬有好的解决方案呀

加载中
0
o
osc_37590934
channelActive的时候握手就可以了

 

bage兔斯基
bage兔斯基
感谢,已解决,背景是客户端需要连接不同地址的Websocket 服务端,每个服务端的数据处理逻辑不同,所以需要获取连接的服务器的url 解决方案是利用channel的 attr(AttributeKey<T> key); 保存一下url就行
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部