客户端的两个具有不同路径的套接字连接与第二个连接重叠

发布于 2025-01-12 02:24:26 字数 1480 浏览 0 评论 0原文

根据列表的条件,我在 flutter 中有两个客户端连接, Server 有两个服务器套接字,具有不同的端口和不同的握手路径,以便客户端轻松连接。

服务器:

Name     | Port | Handshake path 
Socket A | 3001 | /one
Socket B | 3002 | /two

因此,在从 flutter 应用程序连接时,无论我首先打开哪个 listItem,都会建立与指定握手路径的连接。

Items | HandshakePath
Item1 | /one
Item2 | /two
Item3 | /one

对于上述场景,当我单击 Item 1 时,它会与服务器上的 /one 套接字建立连接,并且一切正常,但之后当我单击 Item2 时,它仍然会在 上创建连接/一个路径,反之亦然,无论先建立哪个连接,都将保持连接并与第二个连接重叠。

连接类 1

Class One{
 IO.Socket _socket;
 connect(){
 _socket = IO.io(
        deployment ? _serverIP : SERVER_ONE,
        IO.OptionBuilder()
            .setTransports([
              'websocket'
            ]) 
            .setQuery({
              "info": _fromUser,
            })
            .setPath(deployment ? "/one" : "/socket.io")
            .disableAutoConnect()
            .build());
 }
}

连接类 2

Class Two{
 IO.Socket _socket;
 connect(){
 _socket = IO.io(
        deployment ? _serverIP : SERVER_TWO,
        IO.OptionBuilder()
            .setTransports([
              'websocket'
            ]) 
            .setQuery({
              "user1": _fromUser1,
              "user2": _fromUser2,
            })
            .setPath(deployment ? "/two" : "/socket.io")
            .disableAutoConnect()
            .build());
 }
}

与本地服务器的连接工作得很好,我猜这是因为 ENV 变量中提到的不同端口,但在服务器上,我已经根据路径设置了路由转发到不同的端口。

I'm having two client connections in flutter based on the condition of the list,
and Server has two server sockets with different ports and different Handshake paths for clients to easily connect.

Server :

Name     | Port | Handshake path 
Socket A | 3001 | /one
Socket B | 3002 | /two

so while connecting from the flutter application whichever the listItem I open first, connection with specified handshake path is established.

Items | HandshakePath
Item1 | /one
Item2 | /two
Item3 | /one

For the above scenario when I Click on Item 1 it makes a connection with /one socket on the server and everything work fine, but after that when I click on Item2 it still creates a connection on /one path, and this same for Vice versa, whichever connection is made first, stays connected and overlaps the second connection.

Connection Class 1

Class One{
 IO.Socket _socket;
 connect(){
 _socket = IO.io(
        deployment ? _serverIP : SERVER_ONE,
        IO.OptionBuilder()
            .setTransports([
              'websocket'
            ]) 
            .setQuery({
              "info": _fromUser,
            })
            .setPath(deployment ? "/one" : "/socket.io")
            .disableAutoConnect()
            .build());
 }
}

Connection Class 2

Class Two{
 IO.Socket _socket;
 connect(){
 _socket = IO.io(
        deployment ? _serverIP : SERVER_TWO,
        IO.OptionBuilder()
            .setTransports([
              'websocket'
            ]) 
            .setQuery({
              "user1": _fromUser1,
              "user2": _fromUser2,
            })
            .setPath(deployment ? "/two" : "/socket.io")
            .disableAutoConnect()
            .build());
 }
}

connection with the local server is working perfectly fine, I guess it's because of the different ports mentioned in ENV variables, but on the server, I've set up routing forwarding to different ports based on the path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2025-01-19 02:24:26

在社区对不和谐的帮助之后,我找到了可行的解决方案。

当我们使用 socket_io_client 在 flutter 中创建第一个套接字客户端连接时,我们提供的任何 Option Builder 值都将在我们将创建的每个实例中使用,即使我们提供不同的值,它也只会使用以前的值。在这种情况下,

我们必须使用 enableForceNew() 方法来提供不同的值

  _socket = IO.io(
            deployment ? _serverIP : SERVER_TWO,
            IO.OptionBuilder()
                .enableForceNew() // <--- this method
                .setTransports([
                  'websocket'
                ]) 
                .setQuery({
                  "user1": _fromUser1,
                  "user2": _fromUser2,
                })
                .setPath(deployment ? "/two" : "/socket.io")
                .disableAutoConnect()
                .build());

After help from the community on discord, I found the working solution.

when we are creating a first socket client connection in flutter using socket_io_client whatever Option Builder values we are providing, will be used in every instance that we will create even though we are providing different values it will use the previous value only. in that case,

we have to use the enableForceNew() method to provide a different value

  _socket = IO.io(
            deployment ? _serverIP : SERVER_TWO,
            IO.OptionBuilder()
                .enableForceNew() // <--- this method
                .setTransports([
                  'websocket'
                ]) 
                .setQuery({
                  "user1": _fromUser1,
                  "user2": _fromUser2,
                })
                .setPath(deployment ? "/two" : "/socket.io")
                .disableAutoConnect()
                .build());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文