Socket.IO-Client-Swift icon indicating copy to clipboard operation
Socket.IO-Client-Swift copied to clipboard

Socket "disconnect" listener doesn't work when the server disconnects.

Open Valeriegard opened this issue 3 years ago • 0 comments

So the desired behavior is that socket could get "disconnect" message from the server when the server app gets closed. It works with javascript apps but doesn't work with swift, can't get why. It changes status to "connection error" only after the ping timeout but not when the server disconnects.

All other listeners and connection itself work fine

var manager: SocketManager?
var socket: SocketIOClient?
var socketData = SocketIOData()


 func initializeSocketIO(ip:String, port: String, name: String) {
     
     socketData.stationName = name
     
     manager = SocketManager(socketURL: URL(string: "http://\(ip):\(port)")!, config: [.log(false)])
    
     socket = manager?.socket(forNamespace: "/")
     
 }
 func establishConnection(completionHandler: @escaping () -> Void) {

     socket?.connect(withPayload: socketData.items, timeoutAfter: 2) {
         completionHandler()
         print("Could not connect")
     }
 }

func statusListener() {

        //tried this, didn't work

        socket?.on("disconnect", callback: { (data, ack) in
            print("disconnect", data)
        })

       //this didn't work either
         socket?.on(clientEvent: .disconnect) {data, ack in
                debugPrint("socket disconnected")

            } 


            socket?.on(clientEvent: .error) {data, ack in
                debugPrint("socket error")
            }

            socket?.on(clientEvent: .reconnect) {data, ack in
                debugPrint("socket reconnecting")
            }

               

        socket?.on(clientEvent: .statusChange) {data, ack in
            debugPrint("STATUS CHAGE")

        }   
        
    }

What am I doing wrong?

Valeriegard avatar Feb 22 '22 12:02 Valeriegard