netty-socketio icon indicating copy to clipboard operation
netty-socketio copied to clipboard

when a client connect to the server, server's connectListener onConnect method will be called twice

Open quanli2359 opened this issue 1 year ago • 13 comments

netty-socketio version : 2.0.2 java version : 17

client connect by postman

quanli2359 avatar May 31 '23 08:05 quanli2359

have the same problem, how to fix it?

qjyoung avatar Jun 16 '23 12:06 qjyoung

can use this temp pach https://github.com/lyjnew/netty-socketio-patch image

lyjnew avatar Jun 28 '23 16:06 lyjnew

same problem,wait for bugfix

malinGH avatar Jun 29 '23 08:06 malinGH

@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

lyjnew avatar Jun 29 '23 16:06 lyjnew

@quanli2359

Can you try 2.0.3 version?

mrniko avatar Jul 01 '23 04:07 mrniko

@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。

xdev965 avatar Jul 03 '23 08:07 xdev965

i also have the same problem, i just found out when using v2 in postman it will only be executed once

jofeltje avatar Jul 21 '23 13:07 jofeltje

@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:"" } });

image

lyjnew avatar Aug 15 '23 11:08 lyjnew

same problem. Using v2 client helps

yirelav avatar Aug 15 '23 18:08 yirelav

I have the same problem. netty-socketio: 2.0.3

deppan avatar Sep 15 '23 09:09 deppan

netty-socketio: 2.0.3 the same problem,wait for bugfix.

yejinmiao avatar Oct 10 '23 10:10 yejinmiao

Sorry, due to my poor English, please allow me to answer in Chinese.

如下示例,可得到答案: server: 2.0.6 client: 4.7.3

// server image

// client1 image

// client2 image

// server 打印结果 image

通过上述代码可以知道,无论是使用默认命名空间还是自定义命名空间,onConnect 均会回调 2 次,回调的代码为

  1. com.corundumstudio.socketio.handler.AuthorizeHandler.connect(ClientHead client) 284 行 此处,"client1" 与 "client2" 均会执行 "server.addConnectListener 的回调"(通过方法内部可知,该回调是绑定在 “默认命名空间” 上的); image
  2. com.corundumstudio.socketio.handler.PacketListener.onPacket(Packet packet, NamespaceClient client, Transport transport) 95 行 此处,会取到 "client" 真实的命名空间,并执行相应的回调,"client1" 执行 "server.addConnectListener 的回调","client2" 执行 "namespace.addConnectListener 的回调" image

所以,问题出现在 "回调代码1",无论是 "client1" 还是 "client2" 均会添加到默认命名空间,并执行相应的回调(我没有理解作者的用意,命名空间应该是隔离的,若客户端指定了命名空间,是否不应该添加到默认命名空间);

如果切换为 Demo 中的 socket.io.js 文件,client1 回调 1 次,client2 回调 2 次; 至于为什么使用不同版本的客户端会产生不同的现象,可在源码中增加打印信息进行验证: com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message) image

使用 Demo 中的 socket.io.js 文件(低版本),打印结果 image

使用 4.7.3 版本 socket.io.js 文件(高版本),打印结果 image

结论: 低版本客户端

  • 默认命名空间的连接,不会发起命名空间的请求
  • 自定义命名空间的连接,会发起 "9:40[命名空间]" 请求 高版本客户端
  • 默认命名空间的连接,会发起 "40" 请求(命名空间为空)
  • 自定义命名空间的连接,会发起 "40[命名空间]" 请求 40:通过 com.corundumstudio.socketio.protocol.PacketDecoder 与 com.corundumstudio.socketio.protocol.PacketType 可以得知,"4" 代表 ”MESSAGE“ 协议,"0" 代表 "CONNECT" 子协议

最后,我有一个疑问,既然 "addConnectListener" 回调是挂载在命名空间上的,那么 @OnConnect 是否应该指定命名空间(目前是挂载到默认命名空间)?(@OnDisconnect@OnEvent 也一样) @mrniko

bw-CN avatar Jan 12 '24 08:01 bw-CN

大家最终如何解决的?我现在使用的socket.io-client: 4.7.5,netty-socketio 2.0.9 还存在这个问题。

6LWa6ZKx avatar May 28 '24 08:05 6LWa6ZKx