socketio / socket.io-client-swift

Other
5.21k stars 841 forks source link

Doing Polling issue from swift client when using v4.5.2 socket.IO in a node.js server #1428

Open rouzbeh-abadi opened 1 year ago

rouzbeh-abadi commented 1 year ago

Hi, to make a connection to v4.5.2 socket.IO in a node.js server, I updated swift client from version 15.2.0 to version 16.0.1 The new v4.5.2 socket.IO works perfectly well with the java and Android client, but there are two issues on swift client that I'm dealing with them now:

1- The transport is always set to polling. In order to fix it, I set.forceWebsockets(true), but it is not possible to make an establish a connection after it. It's our first priority to fix this issue. The transport should be set to webcocket Here is the logs

2022-09-16 18:05:40.275708+0200 app-name[1597:395115] LOG OnAckCallback: OnAckCallback for 0 being released
2022-09-16 18:05:54.825999+0200 app-name[1597:395444] LOG SocketEnginePolling: Got polling response
2022-09-16 18:05:54.826233+0200 app-name[1597:395444] LOG SocketEnginePolling: Got poll message: 2
2022-09-16 18:05:54.826520+0200 app-name[1597:395444] LOG SocketEngine: Got message: 2
2022-09-16 18:05:54.826844+0200 app-name[1597:395115] LOG SocketIOClient: Handling event: ping with data: []
2022-09-16 18:05:54.827029+0200 app-name[1597:395444] LOG SocketEnginePolling: Doing polling GET https://<our-socket-url>/socket.io/?transport=polling&b64=1&EIO=4&sid=MmDRjwTzgXduf3x0AAAE
2022-09-16 18:05:54.827843+0200 app-name[1597:395444] LOG SocketEngine: Writing poll:  has data: false
2022-09-16 18:05:54.828001+0200 app-name[1597:395444] LOG SocketEnginePolling: Sending poll:  as type: 3
2022-09-16 18:05:54.828180+0200 app-name[1597:395444] LOG SocketEnginePolling: Created POST string: 3
2022-09-16 18:05:54.828575+0200 app-name[1597:395444] LOG SocketEnginePolling: POSTing
2022-09-16 18:05:54.828736+0200 app-name[1597:395444] LOG SocketEnginePolling: Doing polling POST https://<our-socket-url>/socket.io/?transport=polling&b64=1&EIO=4&sid=MmDRjwTzgXduf3x0AAAE

2- The second issue is, if more than one iOS devices makes a connection, all devices will be disconnected.

Here the swift code:

  public init() {
      let url = URL(string: DefaultURL.base)
      manager = SocketManager(socketURL: url!, config: [.log(true),
                                                        .reconnects(true),
                                                        .reconnectAttempts(3)])

      socket = manager.socket(forNamespace: DefaultURL.namespace)

      socketConnectEvent()
      socketDisconnectEvent()
      connect()
  }

  private func socketConnectEvent() {

        socket.on(clientEvent: .connect) { [weak self]  data, ack in
            guard let this = self else { return }
            this.isConnected = true
            this.observer.onConnect()

        }
  }

  private func socketDisconnectEvent() {
         socket.on(clientEvent: .disconnect) { [weak self] data, ack in
             guard let this = self else { return }

             this.isConnected = false
             this.observer.onDisconnect()

             this.loginCallback?(false, "Socket disconnect")
             this.loginCallback = nil
       }
  }

  public func connect() {
        Logger.logInfo("Connecting socket…")
        socket.connect(timeoutAfter: 1, withHandler: { [weak self] in
            guard let this = self else { return }
            Logger.logError("Failed to connect")

            this.eventHandler?(.connectionDidFailed)
       })
   }

Your help will be appreciated

Myshny commented 1 year ago

Hello) Not sure what it is but still - in our project we try to use Socket.IO on servers and client sides. Server on c++ and we had as I remember similar issues in IOS/Swift - can't connect / connection broker. Android connected ok, IOS not. We migrate to uwebsocket library - https://github.com/uNetworking/uWebSockets/blob/master/misc/READMORE.md?ysclid=l874d105io565424393

and use in Swift native WebSockets/NSURLSession and all works fine

udaysurya-e9 commented 1 year ago

Hello,

In logs, it was using transport polling as you can see below, we need to enable the websockets instead:

Doing polling POST https:///socket.io/?transport=polling&b64=1&EIO=4&sid=MmDRjwTzgXduf3x0AAAE

So, to resolve the issue, please add .forceWebsockets(true) to config like this :

manager = SocketManager(socketURL: url!, config: [.forceWebsockets(true), .log(true), .reconnects(true), .reconnectAttempts(3)])

If it doesn't work, then try using server socket.IO version 3.x.x instead of the new v4.5.2 socket.IO and client version 16.0.1. Then it should work.

Hope it will fix the issue. 😀