SwiftSocket icon indicating copy to clipboard operation
SwiftSocket copied to clipboard

Socket disconnect callback

Open Sandeep-Appster opened this issue 6 years ago • 8 comments

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

Sandeep-Appster avatar Mar 26 '18 04:03 Sandeep-Appster

Hi Sandeep,

Have you found the solution?

Thanks

vivek21feb avatar Apr 25 '18 04:04 vivek21feb

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?

nneuberger1 avatar Jun 14 '18 15:06 nneuberger1

Same here... Any solution would be appreciated!

saormart avatar Jun 20 '18 16:06 saormart

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...

saormart avatar Jun 22 '18 13:06 saormart

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 avatar Jun 22 '18 19:06 nneuberger1

@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!

saormart avatar Jun 22 '18 19:06 saormart

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.

nneuberger1 avatar Jul 02 '18 21:07 nneuberger1

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
    }
}

mostlyjason avatar Oct 17 '18 16:10 mostlyjason