socketio / socket.io-client-swift

Other
5.22k stars 842 forks source link

Socket.io - Maintaining socket connection (Swift) #845

Open kinza88 opened 7 years ago

kinza88 commented 7 years ago

I am making an iOS application in which I am using socket.io. The scenario is very simple, a person books an adventure i.e sending data using emit command. After that, i have used socket.on in order to get the response back from the server. This was working perfectly for sometime but now I am facing problems. Neither the data is reaching the server, nor response is coming back. I searched about this problem and the understanding I got is:

There is some point in which socket is getting disconnected and therefore the it stops working. I even used socket.reconnect before emitting data but it didn't work.

I found a parameter of forceNew for node.js but didn't find any of its alternative in swift.

Here is my code (related to socket.io):

In AppDelegate:

AppDelegate.socket = SocketIOClient(socketURL: URL(string: "######")!, config: [.log(true), .compress])
AppDelegate.socket.connect ()

In ViewController:

override func viewDidLoad() {
        super.viewDidLoad()

        socket = AppDelegate.socket

          self.socket.on("book-adventure-seeker") {[weak self] data, ack in
              print (data)
         }
    }
    @IBAction func bookAdventureTapped(_ sender: Any) {

        let jwtToken = UserDefaults.standard.string(forKey: "jwtToken")

        let data = ["adventure_id": adventureId, "jwt": jwtToken] as [String : Any]
        self.socket.emit("book-adventure", data)
    }

And the log is:

2017-10-20 09:45:04.665 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws:  has data: false
2017-10-20 09:45:08.541 CuufyPrototype[2591:93618] LOG SocketIOClient: Emitting: 2["book-adventure",{"jwt":"######","adventure_id":"######"}]
2017-10-20 09:45:08.541 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws:  as type: 2
SocketIOClientStatus
2017-10-20 09:45:22.804 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: 2["book-adventure",{"jwt":"######","adventure_id":"#####"}] has data: false
2017-10-20 09:45:22.805 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: 2["book-adventure",{"jwt":"#######","adventure_id":"######"}] as type: 4
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws:  has data: false
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws:  as type: 2
2017-10-20 09:45:22.810 CuufyPrototype[2591:93661] ERROR SocketEngine: 
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] ERROR SocketIOClient: 
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: error with data: [""]
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Starting reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnect with data: [""]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Trying to reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnectAttempt with data: [-1]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]

Even i tried to use socket.on in AppDelegate like this: (as in example from official docs)

AppDelegate.socket = SocketIOClient(socketURL: URL(string: "#######")!, config: [.log(true), .compress])

        AppDelegate.socket.on("book-adventure-seeker") {[weak self] data, ack in
            print (data)
        }
  AppDelegate.socket.connect ()

But still it didn't work for me.

Update: Sometimes while on emit command, Engine is being closed appears in log but I am not getting any clue how to solve this issue.

kinza88 commented 7 years ago

@nuclearace

nuclearace commented 7 years ago

Are you using your api correctly? Have you checked the server logs to see why it's disconnecting?

kinza88 commented 6 years ago

Hey I am sorry I got late to respond to your email. I have tried that code again. At this very point, the there is no disconnection error. But the handler is not listening the response. Request is sent, response comes back but it doesn't get listened. It gets printed in the logs only. However i could see this error:

2017-11-28 17:24:50.448986+0500 TestApiProject[8166:383893] LOG SocketEngine: Got message: {"code":1,"message":"Session ID unknown"} 2017-11-28 17:24:50.449257+0500 TestApiProject[8166:383893] ERROR SocketEngine: Session ID unknown

Can you help me regarding this? I have gone through other related issues but didn't find any appropriate help.

vincedev commented 6 years ago

@kinza88 @nuclearace

Hi, I'm facing a very similar issue.
In my case this happens on macOS (didn't see similar behavior on iOS).

Although reconnection works for me, the connection with the server keeps being closed because the server didn't get the PING in time. Logging produces this output :

2017-12-01 10:16:35.897967+0100 Application[19936:973300] LOG SocketEngine: Got message: 3 2017-12-01 10:16:37.882025+0100 Application[19936:973300] LOG SocketEngine: Writing ws: has data: false 2017-12-01 10:16:37.882136+0100 Application[19936:973300] LOG SocketEngineWebSocket: Sending ws: as type: 2 2017-12-01 10:16:37.897835+0100 Application[19936:973300] LOG SocketEngine: Got message: 3 2017-12-01 10:16:39.882151+0100 Application[19936:965577] LOG SocketEngine: Writing ws: has data: false 2017-12-01 10:16:39.882520+0100 Application[19936:965577] LOG SocketEngineWebSocket: Sending ws: as type: 2 2017-12-01 10:16:39.898686+0100 Application[19936:965577] LOG SocketEngine: Got message: 3 2017-12-01 10:16:41.882477+0100 Application[19936:965577] LOG SocketEngine: Writing ws: has data: false 2017-12-01 10:16:41.883000+0100 Application[19936:965577] LOG SocketEngineWebSocket: Sending ws: as type: 2 2017-12-01 10:16:41.907961+0100 Application[19936:973300] LOG SocketEngine: Got message: 3 2017-12-01 10:16:48.909072+0100 Application[19936:965578] ERROR SocketEngine: 2017-12-01 10:16:48.909941+0100 Application[19936:959424] ERROR SocketIOClient: 2017-12-01 10:16:48.910204+0100 Application[19936:959424] LOG SocketIOClient: Handling event: error with data: [""] 2017-12-01 10:16:48.911094+0100 Application[19936:959424] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus] 2017-12-01 10:16:48.911310+0100 Application[19936:959424] LOG SocketIOClient: Starting reconnect 2017-12-01 10:16:48.911715+0100 Application[19936:959424] LOG SocketIOClient: Handling event: reconnect with data: [""] 2017-12-01 10:16:48.912023+0100 Application[19936:959424] LOG SocketIOClient: Trying to reconnect 2017-12-01 10:16:48.912507+0100 Application[19936:959424] LOG SocketIOClient: Handling event: reconnectAttempt with data: [-1] 2017-12-01 10:16:48.915810+0100 Application[19936:959424] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus] 2017-12-01 10:16:48.920342+0100 Application[19936:974381] LOG SocketEngine: Starting engine. Server: http://host.fr.office:57570 2017-12-01 10:16:48.920816+0100 Application[19936:974381] LOG SocketEngine: Handshaking // etc

The log says at some point an error is being triggered, but associated data contains an empty string, so I can't understand what's happening :

Handling event: error with data: [""]

I'm using version 12.1.3 of socket.io-client-swift, originally upgraded from 12.1.2 (same issue). I'm using default ping timeout options, ping every 2 seconds, timeout after 5 seconds, and the server promptly closes the connection. This is highly reproducible, in experiments, within 1-2 minutes the error happens.

Any clue on this ? Can it be a timer somewhere that doesn't fire or something ?

vincedev commented 6 years ago

Further experiment shows the issue doesn't raise on OSX 10.10. Didn't tried on OSX 10.11, but see it on macOS 10.12 and 10.13.