由于在做mqtt设备模拟,在做websocket协议支持时遇到了问题,ws握手能够成功,但后续的消息编解码发送数据那边解析失败了,说明我这边的数据编码不正确。基于Netty做的客户端实现,尝试更改过几个handler都没效果。网上能找到的mqtt的ws协议支持做的大部分是js版本的,但不符合项目要求。想问下有大神指导问题所在吗?
下图是获取websocket协议的bootstrap代码段,也是增加的主要handler
public static Bootstrap getWebSocketServer(NettyMqttParams nettyMqttParams){
if(!wsBootstrap.containsKey(nettyMqttParams.getHost())) {
synchronized(NettyUtil.class) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.option(ChannelOption.SO_KEEPALIVE,true)
.group(group)
.channel(NioSocketChannel.class)
.handler(new com.example.mqttclientsimulator.mqtt.handler.ChannelInitializer())
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder());
pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder());
pipeline.addLast("httpResponseDecoder", new HttpResponseDecoder());
pipeline.addLast("decoder", new MqttDecoder());
pipeline.addLast("encoder", MqttEncoder.INSTANCE);
pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(65536));
pipeline.addLast("http-chunked", new ChunkedWriteHandler());
}
});
bootstrap.remoteAddress(nettyMqttParams.getHost(), nettyMqttParams.getPort());
return bootstrap;
}catch (Exception e){
LOG.error(e.getMessage(),e);
}
return null;
}
}else{
return wsBootstrap.get(nettyMqttParams.getHost());
}
}