SwiftSocket
SwiftSocket copied to clipboard
Socket disconnect callback
Hi,
We have implemented this library in our project and working fine.
But we are struggling to find out socket disconnect callback/delegate in the app.
Can anyone help how to detect socket disconnect OR is there way to to know what is the function gets called when socket get disconnected?
Thanks
Hi Sandeep,
Have you found the solution?
Thanks
I'm looking for this solution. When the device your connected to immediately disconnects, I would like to know when this happens so we can recover within the application from it. So far, I haven't found an immediate solution on when the socket immediately looses a connection with the other application.
Any ideas?
Same here... Any solution would be appreciated!
I may found a workaround on it by adding a if condition and return "-99" as code to know when you loose the connectivity!
I just edited the file: ytcpsocket.c , in the function: "int ytcpsocket_pull(int socketfd, char *data, int len, int timeout_sec) {" at line 107 there's a "do{ ... }" just after the comment: "// use loop to make sure receive all data"
so find that "do{ ... }" -> Line: 123 .... so Just after the "} while (readlen > 0);" I added:
if(readlen == 0){ return -99 }
and after that we have the "return datalen;"
Usually the readlen returns "-1" when it has connectivity, but zero (0) when has no connectivity!
I have a loop reading all the time anything after connected, so this was my workaround to know if I loose connectivity by server down or so...
Awesome. I will take a look at this solution as well.
We started working an internal "keep alive" type of state check as we're calling a card reader device and sending it a specific command, that the device knows as a "test" command. It either returns or doesn't return and externally from this API, we we're going to fail back and disconnect the socket. I'll give your solution a shot too. thanks!
@nneuberger1 what a such coincidence !? I just used this for my own RFID hardware device that I have designed ! I was using before a hybrid javascript solution, until I started migrating everything to Xcode! So, I'm able to communicate with my hardware with no problem, and some commands that I send like to get the access people list, after the device send me all data from TCP connection, it will drop the connectivity automatically, and with this workaround that I did, I'm able to capture that and let the user know that the connectivity has been terminated! Note: I designed my hardware to wait for the line feed/carriage return, so every command I sent I've added to the "String" the "\r\n", otherwise my device was rejecting it (I could change my device firmware, but due I have multiple software platform running, I just add those to my code and works fine!) Hope this works for you as well! If not, I didn't have time yet to check, but I have the CocoaAsyncSocket in my list to check out if works as well!
I tried your temp fix
if(readlen == 0){ return -99 }
But as of right now, I haven't got an immediate callback even though I'm polling in my code. I could be doing something slightly different so I'll keep working on it.
I may try and use CocoaAsyncSocket instead and test it's immediate disconnects. We're using it for other things as well, so I'll report back on that.
I haven't tried the readlen solution, but I just found another solution which is to make the accept call on a loop. If the client attempts to reconnect, the server will know the old one is disconnected and accept the new connection. It works best to dedicate a separate thread since it will block.
queue.async {
while(true) {
self.client = self.server.accept(timeout: -1)
// use the client
}
}