pengwang
pengwang

Reputation: 19956

How to reconnect socket.io iOS client when wifi is switched off then enabled again?

I'm writing an iOS app that uses socket.io to send message to server and receive message from clent, all is ok. But I have a problem: when I enter my app and connect and send message all is Ok, then I press Home Key and enter system settings app, choose the airplane mode to close the wifi, then switch off the airplane mode and connect the wifi, I enter back to my app, but socket cannot reconnect. If I do other internet request as download pic, the progress dialog is always shown.

I don't know my mistake, I am [going] crazy.

I use https://github.com/pkyeck/socket.IO-objc

Upvotes: 0

Views: 4190

Answers (1)

djromero
djromero

Reputation: 19641

Are you detecting when the wifi is on/off? You should. Have a look here: Apple Reachability sample.

When you get a disconnection notification you can disconnect your socket. Connect again when the wifi is back. You should disconnect too when the app is going to background.

I won't expect the socket to reconnect magically. Implement all socket-io delegates to know when it has been disconnected or there was an error. In those cases change your socket-io object, disconnect, etc. Whatever makes sense. You may need a flag to know the current socket status (connecting, disconnecting, etc.). When you activate the app back after reconnecting the wifi you should know what to do depending on your status flag and the actual socket-io object.

Upvotes: 1

Related Questions