Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the disconnected callback #787

Open
Duoluoxjc opened this issue Jun 5, 2020 · 8 comments
Open

How to get the disconnected callback #787

Duoluoxjc opened this issue Jun 5, 2020 · 8 comments
Labels

Comments

@Duoluoxjc
Copy link

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

@aaksh-acto
Copy link

aaksh-acto commented Jun 18, 2020

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

@wesgood
Copy link

wesgood commented Dec 14, 2020

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.

@LorenzoSimonelli
Copy link

some workaround?

@wesgood
Copy link

wesgood commented Jan 13, 2021

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.

@AthleteInAction
Copy link

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

@xdien
Copy link

xdien commented Jan 31, 2023

@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

@scoreyou
Copy link

scoreyou commented Oct 4, 2023

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

@scoreyou
Copy link

scoreyou commented Oct 4, 2023

Also #821 fixes that behaviour and everything works well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants