Socket.io-client-swift: Connection handler won't be called

Created on 21 Nov 2017  路  10Comments  路  Source: socketio/socket.io-client-swift

Hey, my problem is that the _.connect_ handler on the client side won't be called.
On the server side the connection is recognized in io.on('connection', ...). Connecting to the server over the frontend with Javascript is working.

_Client: xCode 9.0, Swift 4.0, SocketIO 13.0.1
Server: NodeJS(Express), SocketIO 2.0.3_

Here is my code:

import Foundation
import SocketIO

class SocketIOManager {

    let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true)])

    func addHandlers(){

        manager.defaultSocket.on(clientEvent: .connect) {data, ack in
            print("\nSocket connected\n")
        }

        manager.defaultSocket.on(clientEvent: .disconnect) {data, ack in
            print("\nSocket disconnected\n")
        }

        manager.defaultSocket.on(clientEvent: .error) {data, ack in
            print("\nSocket error\n")
        }

        manager.defaultSocket.on(clientEvent: .statusChange) {data, ack in
            print("\nStatus change: \(data)\n")
        }

    }

    func establishConnection() {
        manager.defaultSocket.connect()
    }

    func closeConnection() {
        manager.defaultSocket.disconnect()
    }
}

I then do:

let manager = SocketIOManager()
manager.addHandlers()
manager.establishConnection()

What could be the problem here? I did consider your hint from the FAQ, so I don't think that the manager and socket are released by ARC. Also the .statusChange handler was called.

This is the log:

2017-11-21 14:42:46.650171+0100 Test[5750:140658] LOG SocketIOClient{/}: Adding handler for event: disconnect
2017-11-21 14:42:46.650503+0100 Test[5750:140658] LOG SocketIOClient{/}: Adding handler for event: error
2017-11-21 14:42:46.650577+0100 Test[5750:140658] LOG SocketIOClient{/}: Adding handler for event: statusChange
2017-11-21 14:42:46.650616+0100 Test[5750:140658] LOG SocketIOClient{/}: Adding handler for event: connect

2017-11-21 14:42:54.770977+0100 Test[5750:140658] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting]

Status change: [connecting]

2017-11-21 14:42:54.771118+0100 Test[5750:140658] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2017-11-21 14:42:54.771154+0100 Test[5750:140658] LOG SocketManager: Adding engine

2017-11-21 14:42:54.772078+0100 Test[5750:140759] LOG SocketEngine: Starting engine. Server: http://localhost:8080
2017-11-21 14:42:54.772121+0100 Test[5750:140759] LOG SocketEngine: Handshaking
2017-11-21 14:42:54.777270+0100 Test[5750:140759] LOG SocketEnginePolling: Doing polling GET http://localhost:8080/socket.io/?transport=polling&b64=1
2017-11-21 14:42:54.834502+0100 Test[5750:140761] LOG SocketEnginePolling: Got polling response
2017-11-21 14:42:54.834608+0100 Test[5750:140761] LOG SocketEnginePolling: Got poll message: 97:0{"sid":"z80J_QGLJ50kP25WAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40
2017-11-21 14:42:54.834783+0100 Test[5750:140761] LOG SocketEngine: Got message: 0{"sid":"z80J_QGLJ50kP25WAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2017-11-21 14:42:54.836306+0100 Test[5750:140761] LOG SocketEngine: Got message: 40
2017-11-21 14:42:54.836488+0100 Test[5750:140761] LOG SocketEnginePolling: Doing polling GET http://localhost:8080/socket.io/?transport=polling&b64=1&sid=z80J_QGLJ50kP25WAAAA
2017-11-21 14:42:54.836724+0100 Test[5750:140761] LOG SocketEngine: Writing poll: has data: false
2017-11-21 14:42:54.836770+0100 Test[5750:140761] LOG SocketEnginePolling: Sending poll: as type: 2
2017-11-21 14:42:54.836863+0100 Test[5750:140761] LOG SocketEnginePolling: Created POST string: 1:2
2017-11-21 14:42:54.837311+0100 Test[5750:140761] LOG SocketEnginePolling: POSTing
2017-11-21 14:42:54.837377+0100 Test[5750:140761] LOG SocketEnginePolling: Doing polling POST http://localhost:8080/socket.io/?transport=polling&b64=1&sid=z80J_QGLJ50kP25WAAAA
2017-11-21 14:42:54.842526+0100 Test[5750:140779] LOG SocketEnginePolling: Got polling response
2017-11-21 14:42:54.842575+0100 Test[5750:140779] LOG SocketEnginePolling: Got poll message: 13:42["request"]
2017-11-21 14:42:54.842613+0100 Test[5750:140779] LOG SocketEngine: Got message: 42["request"]
2017-11-21 14:42:54.842740+0100 Test[5750:140779] LOG SocketEnginePolling: Doing polling GET http://localhost:8080/socket.io/?transport=polling&b64=1&sid=z80J_QGLJ50kP25WAAAA
2017-11-21 14:42:54.849207+0100 Test[5750:140762] LOG SocketEngineWebSocket: Sending ws: probe as type: 2
2017-11-21 14:42:54.851141+0100 Test[5750:140761] LOG SocketEnginePolling: Got polling response
2017-11-21 14:42:54.851203+0100 Test[5750:140761] LOG SocketEnginePolling: Got poll message: 1:3
2017-11-21 14:42:54.851264+0100 Test[5750:140761] LOG SocketEngine: Got message: 3
2017-11-21 14:42:54.851360+0100 Test[5750:140761] LOG SocketEnginePolling: Doing polling GET http://localhost:8080/socket.io/?transport=polling&b64=1&sid=z80J_QGLJ50kP25WAAAA
2017-11-21 14:42:54.852162+0100 Test[5750:140762] LOG SocketEngine: Got message: 3probe
2017-11-21 14:42:54.852260+0100 Test[5750:140762] LOG SocketEngine: Upgrading transport to WebSockets
2017-11-21 14:42:54.852304+0100 Test[5750:140762] LOG SocketEnginePolling: Sending poll: as type: 6
2017-11-21 14:42:54.852344+0100 Test[5750:140762] LOG SocketEnginePolling: Created POST string: 1:6
2017-11-21 14:42:54.852468+0100 Test[5750:140762] LOG SocketEnginePolling: POSTing
2017-11-21 14:42:54.959248+0100 Test[5750:140762] LOG SocketEnginePolling: Got polling response
2017-11-21 14:42:54.959341+0100 Test[5750:140762] LOG SocketEnginePolling: Got poll message: 1:6
2017-11-21 14:42:54.959430+0100 Test[5750:140762] LOG SocketEngine: Got message: 6
2017-11-21 14:42:54.959493+0100 Test[5750:140762] LOG SocketEngineWebSocket: Sending ws: as type: 5
2017-11-21 14:42:54.959594+0100 Test[5750:140762] LOG SocketEngine: Flushing probe wait
... Repeating every couple of seconds ...

question

Most helpful comment

@nuclearace
@kajz3reczka

I figure out the solution. It worked for me.. Just declare a variable in global.

let manager = SocketManager(socketURL: URL(string: "ws://127.0.0.1:3001")!, config: [.log(true)])

screen shot 2018-04-20 at 2 58 23 pm

All 10 comments

@nuclearace Any idea?

@YugoCode Not really, it doesn't even look like the parser is running. Are you blocking the handleQueue with something? That's the only thing I can think of that would stop messages reaching the parser.

https://github.com/socketio/socket.io-client-swift/blob/master/Source/SocketIO/Manager/SocketManager.swift#L339

@nuclearace I was not sure, if I'm blocking handleQueue in some way, so I created a new project/repo:
https://github.com/YugoCode/SocketIO_Swift/tree/master/Sources/Test

Here I'm only trying to connect to the server and again the connection handler won't be called.

Do you see any problems in my code? Could you maybe test, if it connects to a socketio server of yours?


I tried v13.0.1 and v12.1.3. I know the server works, because connecting with JavaScript on the same machine is working. So the problem is my code?

@YugoCode Well in that contrived example you aren't setting up a run loop. You need to call CFRunLoopRun

@nuclearace CFRunLoopRun work but when I call CFRunLoopRun after connect to socket (all in viewDidLoad) view controller doesn't show :( Any idea?

Having the same issue... Any solution for this?.. I also tried CFRunLoopRun but after the viewController not worked.

@kajz3reczka @muslimrao calling CFRunLoopRun in an iOS app is a no-no. You only need to call that when you鈥檙e writing a command-line app where you don鈥檛 have an event loop

@nuclearace so is any way to have 100% working socket connection without CFRunLoopRun? ofc in iOS app

@nuclearace
@kajz3reczka

I figure out the solution. It worked for me.. Just declare a variable in global.

let manager = SocketManager(socketURL: URL(string: "ws://127.0.0.1:3001")!, config: [.log(true)])

screen shot 2018-04-20 at 2 58 23 pm

.connect event not called...
how to solve this issue
Please help

below is my code...

//GLOBLE VAR
let manager = SocketManager(socketURL: URL(string: "ws://10.0.3.178/")!,
config: [.log(true)])

//SHARED CLASS FOR SOCKET
class SocketIOManager {
func addHandlers(){
manager.defaultSocket.on(clientEvent: .connect) {data, ack in
print("\nSocket connected\n")
}
manager.defaultSocket.on(clientEvent: .disconnect) {data, ack in
print("\nSocket disconnected\n")
}

    manager.defaultSocket.on(clientEvent: .error) {data, ack in
        print("\nSocket error\n")
    }

    manager.defaultSocket.on(clientEvent: .statusChange) {data, ack in
        print("\nStatus change: \(data)\n")
    }

}

func establishConnection() {
    manager.defaultSocket.connect()
    CFRunLoopRun()

}

func closeConnection() {
    manager.defaultSocket.disconnect()
}

}

//VIEW CONTROLLER WHER ESTABLISH CONNECTION
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()

    let manager = SocketIOManager()
    manager.addHandlers()
    manager.establishConnection()
}

}
//CONSOL LOG

2019-07-05 13:06:10.694949+0530 realtime[1139:477284] LOG SocketIOClient{/}: Adding handler for event: connect
2019-07-05 13:06:10.695392+0530 realtime[1139:477284] LOG SocketIOClient{/}: Adding handler for event: disconnect
2019-07-05 13:06:10.695454+0530 realtime[1139:477284] LOG SocketIOClient{/}: Adding handler for event: error
2019-07-05 13:06:10.695487+0530 realtime[1139:477284] LOG SocketIOClient{/}: Adding handler for event: statusChange
2019-07-05 13:06:10.696508+0530 realtime[1139:477284] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting]

Status change: [connecting]

2019-07-05 13:06:10.696881+0530 realtime[1139:477284] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2019-07-05 13:06:10.696974+0530 realtime[1139:477284] LOG SocketManager: Adding engine
2019-07-05 13:06:10.701376+0530 realtime[1139:477354] LOG SocketEngine: Starting engine. Server: ws://10.0.3.178/
2019-07-05 13:06:10.701429+0530 realtime[1139:477354] LOG SocketEngine: Handshaking
2019-07-05 13:06:10.702843+0530 realtime[1139:477354] LOG SocketEnginePolling: Doing polling GET http://10.0.3.178/socket.io/?transport=polling&b64=1
2019-07-05 13:06:13.549678+0530 realtime[1139:477355] LOG SocketEnginePolling: Got polling response
2019-07-05 13:06:13.557455+0530 realtime[1139:477355] LOG SocketEnginePolling: Got poll message:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heisian picture heisian  路  4Comments

MihaelIsaev picture MihaelIsaev  路  6Comments

roma86 picture roma86  路  4Comments

Yelles picture Yelles  路  5Comments

janiduw picture janiduw  路  5Comments