How to get the disconnected callback
If I use version 3.1.1. When the server is closed or the connection fails, use this method ( func websocketDidDisconnect) to receive a callback. But if I use 4.0.3 , There is no callback when the server is closed or the connection fails.
func didReceive(event: WebSocketEvent, client: WebSocket) { switch event { case .connected(let headers): isConnected = true print("websocket is connected: (headers)") case .disconnected(let reason, let code): isConnected = false print("websocket is disconnected: (reason) with code: (code)") case .text(let string): print("Received text: (string)") case .binary(let data): print("Received data: (data.count)") case .ping(): break case .pong(): break case .viablityChanged(): break case .reconnectSuggested(): break case .cancelled: isConnected = false case .error(let error): isConnected = false handleError(error) } } This fun no run ?How to get the disconnected callback in 4.0.3
This works for me: // when connection fails case .disconnected(let reason, let code): is called for me. Make sure your WebSocketDelegate is set correctly.
- Add "WebSocketDelegate" to your view controller class
e.g.
class ViewController: WebSocketDelegate{
var isSocketConnected = false
`socket = WebSocket(request: request)
socket.delegate = self
socket.connect() // to connect
socket.disconnect()` // in the did receive method .cancelled case will be called instead of .disconnect
//Implement the below function:
func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(let headers):
isSocketConnected = true
// socket is connected
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code): // this will called when connection fails
isSocketConnected = false
//Socket is disconnected
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let responseString):
print(responseString)
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
break
case .pong(_):
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
print("-------------------")
print("Websocket is Cancelled")
print("-------------------")
isSocketConnected = false
case .error(let error):
isSocketConnected = false
handleError(error)
}
}
Jumping in to mention that v4.0.4 is not receiving the callback either. I just upgraded from 3.0.6 and the same event (killing the socket server) does not produce a callback message. I also wish there was a clear note about what events send which delegate event.
some workaround?
My work around was to go to the last available 3.x.x version in CocoaPods. Doing that reverted to the earlier delegate structure and I haven't had any problems.
@wesgood I went back to 3.0.6 due to the disconnect event not working well
@wesgood I went back to 3.0.6 due to the disconnect event not working well
I also had the same problem with v4.0.4 I downgraded to 3.0.6 and the disconnect event worked fine
Came here just to mention that v4.0.6 still has this issue 😢
Also #821 fixes that behaviour and everything works well.