sjrmanning / Birdsong

:bird::musical_score: Swift WebSockets client for Phoenix Channels.
MIT License
127 stars 37 forks source link

Push not failing when socket is disconnected #16

Closed Daltron closed 7 years ago

Daltron commented 7 years ago

While the server is turned off intentionally (thus my socket being disconnected), I am running the following block of code:

_ = channel?.send("new:msg", payload: ["body" : body])?
.receive("error", callback: { (payload) in
        // This is never called    
})

I was expecting the error callback to be executed if trying to send a message while the socket was disconnected. I don't know wether this is the way the library is designed or if it is a bug. Should I be checking before I send messages to the channel wether or not the socket is connected?

Daltron commented 7 years ago

@sjrmanning, if you could chime in on this, that would be greatly appreciated! 😃

sjrmanning commented 7 years ago

I've had a look at this and I think the best solution is to catch this in Socket, checking if we're connected before we process the Push.

When I get some time I'd like to refactor a lot of this stuff so the errors are more formal — at least a way to distinguish Birdsong client errors from errors that might come back from the server with the same status key (currently "error").

For now I've fixed this and return a simple ["reason": "Not connected to socket."] payload when send is called when disconnected. Let me know if this is suitable for you or if you have any thoughts :)

Note: tested this with a disconnected socket with the example below.

self.channel = self.socket.channel("test")
self.channel?.send("new:msg", payload: ["body": "test"])?.receive("error", callback: { payload in
    debugPrint("Received error reason: ", payload)
})
Daltron commented 7 years ago

@sjrmanning This is actually exactly what I am doing right now so I think this is a great to handle it. Thanks! 👍

sjrmanning commented 7 years ago

You're welcome! I haven't had time lately to continue my side-project that I use Birdsong for so I really appreciate the reports & PRs in the mean-time :)