ioswebsocketkeep-alivestarscream

StarScream websocketDidReceivePong is not getting called for writePing


I would like to send periodic pings to server to keep the connection alive.

This is the function to send ping

socket.writePing(NSData())

But I am not getting the pong back

func websocketDidReceivePong(socket: WebSocket){
    wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket received pong")
}

the above delegate will get fired when server sends back the pong.

FYI: I am able to successfully establish web socket connect send message and close connection and I have implemented and I have given socket.delegate=self

WebSocketPongDelegate

Here is the web socket url that I am using for testing purposes

ws://echo.websocket.org/

I am wondering why I am not getting the pong back from web socket server.


Solution

  • Is your socket a property of your class to make sure it sticks around? If you allocate it just on a function stack it will fall out of scope and the delegates will never get called. Also the pongDelegate is separate from the regular delegate, so you need to set that to self as well:

    socket.pongDelegate = self