pusher-websocket-swift: socket connection is not connected although pusher:connection_established event was fired

Hi,

I have a problem with Pusher that I need support. Whenever I use the app, then press power button to turn screen off, the socket connection will be closed. When I turn on again, I listen to connectionStateDelegate to make connection again. However, it cannot auto re-subscribe to existing channels because socket connection is not connected. Please check the code below:

    private func handleConnectionEstablishedEvent(json: PusherEventJSON) {
        if let data = json["data"] as? String {
            if let connectionData = getPusherEventJSONFromString(data), socketId = connectionData["socket_id"] as? String {
                updateConnectionState(.Connected)
                self.socketId = socketId

                print("socket: \(socketId) - connected: \(self.socket.isConnected)") // this shows connected: false

                for (_, channel) in self.channels.channels {
                    if !channel.subscribed {
                        if !self.authorize(channel) {
                            print("Unable to subscribe to channel: \(channel.name)")
                        }
                    }
                }
            }
        }
    }

As you can see, the log in the middle of the function is what I used to debug, it prints out connected: false. According to my debug, this code did enter the authorize(channel) but socket is not connected so it cannot call to pusher server (no debug log on pusher server as well).

Sometime this works fine, because socket can make connection before this method is called. But most of the time it won’t work… I don’t know why handleConnectionEstablishedEvent is called when event pusher:connection_established is fired, but still socket connection is not ready. Could this be pusher server problem or the WebSocket library problem (Starscream)?

In the meantime, I will switch back to libPusher (ObjC library) which didn’t have this problem in my other apps. But libPusher is kinda outdated so I would like to use this library more. Hope you guys can have this resolved soon. Thank you.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (12 by maintainers)

Most upvoted comments

I’ll try with reinstantiating my pusher object. Will let you know of the outcome 😃

Sorry i have been too swamped. I will check this soon tonight or tomorrow. On Jul 7, 2016 17:54, “Hamilton Chapman” notifications@github.com wrote:

@thanhn08 https://github.com/thanhn08 any updates or can I close the issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pusher/pusher-websocket-swift/issues/51#issuecomment-231046391, or mute the thread https://github.com/notifications/unsubscribe/ABLmIm6q1P3lQtrk75xibRXNlES-_URkks5qTNrcgaJpZM4IsYhA .