alionthego
alionthego

Reputation: 9743

how to check socket.io connection status using swift API

I am using socket.io with my swift iOS app. Occasionally my socket connection is dropped.

How can I check the status of the connection so that I may re-connect if it is not connected? I couldn't find any commands for checking the status of the connection in the client API documents.

I'm using iOS 11 with swift 4.

Upvotes: 2

Views: 6306

Answers (3)

Wahab Khan Jadon
Wahab Khan Jadon

Reputation: 1176

You can use it separately like the following ...

private var socket:SocketIOClient!



if socket.status == connecting || socket.status == connected {
    //socket is active 
    return 
}

or a better way is to use active parameter like follows...

func establishConnection(){
     if socket.status.active{
          return
      }
      socket.connect()
}

Upvotes: 0

John Bassos
John Bassos

Reputation: 308

Swift 4:

    let socketConnectionStatus = socket.status

    switch socketConnectionStatus {
    case SocketIOStatus.connected:
        print("socket connected")
    case SocketIOStatus.connecting:
        print("socket connecting")
    case SocketIOStatus.disconnected:
        print("socket disconnected")
    case SocketIOStatus.notConnected:
        print("socket not connected")
    }

Upvotes: 9

alionthego
alionthego

Reputation: 9743

Searched through the code and found simple solution:

let socketConnectionStatus = SocketIOManager.sharedInstance.socket.status

switch socketConnectionStatus {
case SocketIOClientStatus.connected:
   print("socket connected")
case SocketIOClientStatus.connecting:
   print("socket connecting")
case SocketIOClientStatus.disconnected:
   print("socket disconnected")
case SocketIOClientStatus.notConnected:
   print("socket not connected")
}

Upvotes: 4

Related Questions