netty-socketio
netty-socketio copied to clipboard
when a client connect to the server, server's connectListener onConnect method will be called twice
netty-socketio version : 2.0.2 java version : 17
client connect by postman
have the same problem, how to fix it?
can use this temp pach https://github.com/lyjnew/netty-socketio-patch
same problem,wait for bugfix
@qjyoung @quanli2359 @malinGH Please take a look to see if this is helpful for you https://github.com/mrniko/netty-socketio/issues/903#issuecomment-1613191167
@quanli2359
Can you try 2.0.3 version?
@mrniko ,@lyjnew
I tried v2.0.3,connect() event still triggered twice,The client I use is "socket.io-client": "4.7.1",The configuration file is as follows:
const socket = io("ws://localhost:8081", { upgrade: false, //must be false autoConnect: false, reconnection: true, timeout: 20000, reconnectionDelay: 1000, reconnectionDelayMax: 5000, reconnectionAttempts: 2, path: "/socket.io/", transports: ["websocket"], query: {token: ""} });
In addition, I found that the upgrade must be false, otherwise it will not be connected。
i also have the same problem, i just found out when using v2 in postman it will only be executed once
@jofeltje "socket.io-client": "4.7.1" var chat1Socket = io.connect('ws://localhost:8080/chat',{ transports:["websocket"] //,autoConnect: false this error ,reconnection: true ,timeout: 20000 ,reconnectionDelay: 1000 ,reconnectionDelayMax: 5000 ,reconnectionAttempts: 2 ,query: { token:"" } });
same problem. Using v2 client helps
I have the same problem. netty-socketio: 2.0.3
netty-socketio: 2.0.3 the same problem,wait for bugfix.
Sorry, due to my poor English, please allow me to answer in Chinese.
如下示例,可得到答案: server: 2.0.6 client: 4.7.3
// server
// client1
// client2
// server 打印结果
通过上述代码可以知道,无论是使用默认命名空间还是自定义命名空间,onConnect 均会回调 2 次,回调的代码为
- com.corundumstudio.socketio.handler.AuthorizeHandler.connect(ClientHead client) 284 行
此处,"client1" 与 "client2" 均会执行 "server.addConnectListener 的回调"(通过方法内部可知,该回调是绑定在 “默认命名空间” 上的);
- com.corundumstudio.socketio.handler.PacketListener.onPacket(Packet packet, NamespaceClient client, Transport transport) 95 行
此处,会取到 "client" 真实的命名空间,并执行相应的回调,"client1" 执行 "server.addConnectListener 的回调","client2" 执行 "namespace.addConnectListener 的回调"
所以,问题出现在 "回调代码1",无论是 "client1" 还是 "client2" 均会添加到默认命名空间,并执行相应的回调(我没有理解作者的用意,命名空间应该是隔离的,若客户端指定了命名空间,是否不应该添加到默认命名空间);
如果切换为 Demo 中的 socket.io.js 文件,client1 回调 1 次,client2 回调 2 次;
至于为什么使用不同版本的客户端会产生不同的现象,可在源码中增加打印信息进行验证:
com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message)
使用 Demo 中的 socket.io.js 文件(低版本),打印结果
使用 4.7.3 版本 socket.io.js 文件(高版本),打印结果
结论: 低版本客户端
- 默认命名空间的连接,不会发起命名空间的请求
- 自定义命名空间的连接,会发起 "9:40[命名空间]" 请求 高版本客户端
- 默认命名空间的连接,会发起 "40" 请求(命名空间为空)
- 自定义命名空间的连接,会发起 "40[命名空间]" 请求 40:通过 com.corundumstudio.socketio.protocol.PacketDecoder 与 com.corundumstudio.socketio.protocol.PacketType 可以得知,"4" 代表 ”MESSAGE“ 协议,"0" 代表 "CONNECT" 子协议
最后,我有一个疑问,既然 "addConnectListener" 回调是挂载在命名空间上的,那么 @OnConnect
是否应该指定命名空间(目前是挂载到默认命名空间)?(@OnDisconnect
与 @OnEvent
也一样)
@mrniko
大家最终如何解决的?我现在使用的socket.io-client: 4.7.5,netty-socketio 2.0.9 还存在这个问题。