benjam Posted May 4, 2007 Share Posted May 4, 2007 I have a couple of scripts where one is an AJAX socket 'client' and the other is a socket server, both in PHP. The nature of the AJAX beast is such that, it must reconnect every time it sends data to or from the socket server. If any data comes in while this reconnect is taking place, that data is lost to the AJAX socket client. I have found a way around this using buffers: the incoming data gets stored in a buffer, if the client is there, the buffer is sent and cleared. If the client is not there, the buffer continues to be saved until the client returns, at which time the buffer is sent and the process repeats. The problem I am having is, if the client disconnects, for whatever reason (but to return later), the server does not realize the client has disconnected until after it has sent the next line and loops back to try to read from the disconnected client, which fails, and it then disconnects the client from the local side, and begins to store the buffer contents. But the first line is already deleted from the buffer. I have tried testing FALSE against socket_read($client_sock, 0), socket_write($client_sock, ''), socket_getpeername($client_sock, $null), and all with no luck. The only way the server seems to notice that the client has disconnected is when it tries to read more than a zero length data stream (which I cannot test against, for there may be other clients sending data through those socket_reads, in which case, the valid incoming data will be lost). Does anybody have any suggestions on how I might test for a valid connection before sending my buffer contents, one which does not disrupt any other clients, and works every time? Thanks simple code samples avail upon request Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.