pusher / NWWebSocket

A WebSocket client written in Swift, using the Network framework from Apple.
MIT License
123 stars 25 forks source link

How to make socket is always connected? #32

Open Ewg777 opened 2 years ago

Ewg777 commented 2 years ago

Hey @danielrbrowne! Thanks for a great lib! How to make it always connected to the websocket? Os there any autoreconnection feature?

benjamin-tang-pusher commented 2 years ago

Hi, you can use the .ping() method (https://github.com/pusher/NWWebSocket#ping-and-pong) to tell the server that the connection should be maintained. The WebSocketConnectionDelegate protocol has the func webSocketDidReceivePong(connection: WebSocketConnection) so you can handle the pong response from the server. If this fails then you could try reconnecting.

There's also the func webSocketDidDisconnect, where you can try reconnecting here as well: https://github.com/pusher/NWWebSocket#receiving-messages-and-connection-updates

I don't think there's a specific auto-reconnection feature.

Ewg777 commented 2 years ago

I do use the ping method and the connect after webSocketDidDisconnect, but there are many network errors that blocks the next connect. That's why I questioned here @benjamin-tang-pusher

benjamin-tang-pusher commented 2 years ago

What are the network errors you are receiving? Could you paste them here?