Hey. Tell me please, can I do reconnect this way:
socket.onDisconnect = { (error: error?) in {
self.socket.connect ()
}
At such attempt I have errors in the console and when I call connect () I do not get into onDisconnect {} any more. :( The main thing is that on swift 3 this method worked.
I ask you to give advice on how to improve my code. Thank you in advance for your cooperation.
It seems to me that I understood the essence of the problem:
If there is no internet connection and we send a request via webSocket, we do not receive any notifications. Also, if a request was sent without the Internet to the server (1), then another, separate object will not send a request to the server (2), because it will be occupied by the server (1). I tried on different threads and nothing happens: (
I rolled back to version 2.1.1 and everything works fine. Please correct in the new update.
I'm not sure what you are seeing and thus I can't correct it. There have been a lot of changes from 2.1.1 to the latest version, but all the versions have passed the Autobahn tests as expected. onDisconnect still works in all of my tests, so I would appreciate a sample project or some code to reproduce the problem with.
The code is simple:
socket.onDisconnect = {[weak self] (error: error?) in {
self? .socket.connect ()
}
In versions> 3 block socket.onDisconnect = {(error: error?) In {} it is called once, despite the fact that it contains self.socket.connect () and there is no connection to the server. And if a code call is higher and a connection is made to another socket on another server, then the other connector will not connect. It seems that the first socket does not do this and the second one can not complete the connection.
I did a bit of digging in this. The issue is the underlining socket isn't ready for reuse when you call connect again. The foundation stream object never becomes "ready" (hasSpaceAvaliable is never updated to true). This create a cycle of attempting to reconnect and the socket never being ready. I generally would avoid calling connect() straight from a disconnect call, as you normally would to check a few things to see if the socket isn't actually disconnected. Regradless, you can just add a bit of delay and all will work fine again (just use DispatchQueue.main.AsyncAfter(deadline: .now() + 0.5,{socket.connect()}).
P.S. 3.0.4 was also just released with some better disconnect logic.
as you normally would to check a few things to see if the socket isn't actually disconnected
What things should we check before calling connect()? Is the proper way to solve this in the host implementation or should this be fixed in Starscream?
Most helpful comment
What things should we check before calling
connect()? Is the proper way to solve this in the host implementation or should this be fixed in Starscream?