1

I use Starscream. How can I check the socket is connecting or not? isConnecting is a private property.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Maiko Ohkawa
  • 853
  • 2
  • 11
  • 28

2 Answers2

1

You should use socket.isConnected:

if socket.isConnected {
    //socket is connected
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 1
    Thank you. I think `isConnected` says `the socket has connect once in a past`. It not says `the socket is connecting now`. Because it says `true` in offline. – Maiko Ohkawa Jan 18 '18 at 14:13
  • @MaikoOhkawa It might be that the socket is not properly disconnected when the app goes offline. Use [Reachability](https://github.com/ashleymills/Reachability.swift) to test the current connection status of your app. – Tamás Sengel Jan 18 '18 at 14:21
  • Thank you. I understood that I need to call `discCnnect()` manualy when offline. The `isConnected` property NOT change automaticaly. Hmm... I think, `Reachability` checks connection to host only when I call it. If I use `Reachability` every 5 seconds and offline time is very short, websocke actualy die but `Reachability` returns success, I think. Oh my god. – Maiko Ohkawa Jan 19 '18 at 07:20
0

In case if you want to check your socket is still alive (In case if you feel that it might be dead for example you are not receiving a packets stream you are suppose to get) then you can send ping, it will result in triggering websocketDidDisconnect method in case socket is gone. and Yes, You are right isConnected says that it was connected in the past so you can not rely on it.

func sendPing() {
    guard let socket = socket, socket.isConnected
        else { return }
    socket.write(ping: "PING")
}
Kraming
  • 217
  • 3
  • 8