Page 1 of 1

Socket_Close() ?

Posted: Fri Feb 04, 2005 3:09 pm
by RayonMazter
I have a php script connect to an app server, works fine, recieves data perfect.

but when the user closes the window, they remain connected, unless manualy disconnected by me (the server).

I have the server send a string every 10 seconds, and if the client doesnt send back then it disconnects them.....but it still stays connected. So the script is obviously still running, even with the window that the script was loaded on, was closed.

heres how the clients setup :

Code: Select all

do  { 
$data = socket_read($socket,8192,PHP_BINARY_READ);\


//switch this and case that

} while (true);

socket_shutdown($socket);
socket_close($socket);

Suggestions on having them disconnect when they are no longer running the script?


thanks

Posted: Fri Feb 04, 2005 3:26 pm
by feyd
the script runs independant of the browser looking at the page or not. I can only suggest switching from an infinite loop on the server which will eat the processors to a browser refreshed version and/or using an onunload event to some how trigger the script to stop. Something that can be done is to randomly generate a query on an image and use Javascript to generate a new image periodically.. the image is requested from a script that updates a database entry to tell the actual script to continue displaying data. Once the marker is 30 seconds stale, kill the script.

Posted: Fri Feb 04, 2005 4:56 pm
by RayonMazter
i'll try having a script loaded on the 'onunload' event through body, that will close it.


The server is an 'app' its not php, therefore i do not use a database. or a loop function for the server.

thanks

Posted: Fri Feb 04, 2005 5:49 pm
by RayonMazter
Thats a no go, lol.

Is there a function for php to get the curent browsers properties, like the url in the address bar.

because once u close or go to a different website, the string would change and i could then have it close the socket.

Posted: Fri Feb 04, 2005 5:57 pm
by feyd
the information available to php is only good as of the page request. If the user navigates away from the page you won't know any of that information..

you may be able to poll connection_aborted() or a sibling.. although I'm unsure if that'd work..

Posted: Fri Feb 04, 2005 7:56 pm
by RayonMazter
Solved:

incase anyone else falls into this problem.

Through the loop of recieving data.

set the code up like this (client)

Code: Select all

do {
$data = socket_read(їsocket],8192,PHP_BINARY_READ);



//whatever other code u got goin on
echo chr(13);

} while (true);
socket_close(їsocket]);
it doesnt echo any visible letters. and when the browser is changed or closed, the ' while (true);' mark becomes false, and goes to the socket_close function.


you dont need to call the echo chr(13); every second so you could use the sleep([amount of seconds]) command or make a counter variant to process the echo every interval.

Thanks feyd, i sorta got the idea with the javascript 'loading image' thing.