Starscream icon indicating copy to clipboard operation
Starscream copied to clipboard

How to get the disconnected callback

Open Duoluoxjc opened this issue 5 years ago • 8 comments

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

Duoluoxjc avatar Jun 05 '20 07:06 Duoluoxjc

This works for me: // when connection fails case .disconnected(let reason, let code): is called for me. Make sure your WebSocketDelegate is set correctly.

  1. 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)
            
        }
    }

aaksh-acto avatar Jun 18 '20 18:06 aaksh-acto

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.

wesgood avatar Dec 14 '20 03:12 wesgood

some workaround?

LorenzoSimonelli avatar Jan 12 '21 23:01 LorenzoSimonelli

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 avatar Jan 13 '21 13:01 wesgood

@wesgood I went back to 3.0.6 due to the disconnect event not working well

AthleteInAction avatar Sep 29 '22 10:09 AthleteInAction

@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

xdien avatar Jan 31 '23 10:01 xdien

Came here just to mention that v4.0.6 still has this issue 😢

scoreyou avatar Oct 04 '23 15:10 scoreyou

Also #821 fixes that behaviour and everything works well.

scoreyou avatar Oct 04 '23 15:10 scoreyou