nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

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

Open Valeriegard opened 2 years ago

Valeriegard commented 2 years ago

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?