pusher / NWWebSocket

A WebSocket client written in Swift, using the Network framework from Apple.
MIT License
123 stars 25 forks source link

Recreating connection instead of calling start again (it's not allowed) #17

Closed pfandrade closed 3 years ago

pfandrade commented 3 years ago

As per documentation a connection should only receiver a start call once.

danielrbrowne commented 3 years ago

Hi, can you point to the specific documentation you're referring to? I'm a little unclear on what scenario this pull request is attempting to resolve?

pfandrade commented 3 years ago
Screenshot 2020-11-12 at 16 27 54

It's actually in the code itself. Docs don't mention it but there's an exception logged in the Xcode console on a second call to reconnect. And it's not just the log message, apparently it actually doesn't reconnect. Creating a new connection works however.

danielrbrowne commented 3 years ago

Ah ok that makes sense now.

However, I think rather than making the change you've currently proposed I'd prefer that any calls to connect() should only initialise the connection and then configure it if the object is nil. This should work since the connection object is set to nil in the stopConnection(error:) method (which is called as a result of a either server or client-triggered disconnections):

open func connect() {
    if connection == nil {
        connection = NWConnection(to: endpoint, using: parameters)
        intentionalDisconnect = false
        connection?.stateUpdateHandler = stateDidChange(to:)
        listen()
        connection?.start(queue: connectionQueue)
    }
}
danielrbrowne commented 3 years ago

There's a related task here that the code comment docs for the connect() method of WebSocketConnection should probably reflect this behaviour.

danielrbrowne commented 3 years ago

Closing since there have been several reconnection improvements included in the 0.5.0 release.

Notably this includes ensuring that subsequent calls to connect() have no effect.