Maiko Ohkawa
Maiko Ohkawa

Reputation: 933

How can I check if the socket is connecting or not using Starscream?

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

Upvotes: 1

Views: 2460

Answers (2)

Kraming
Kraming

Reputation: 257

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")
}

Upvotes: 0

glyvox
glyvox

Reputation: 58129

You should use socket.isConnected:

if socket.isConnected {
    //socket is connected
}

Upvotes: 1

Related Questions