mikes1024 has asked for the wisdom of the Perl Monks concerning the following question:

My task: Write a chat server that neither uses plugins/applets nor refreshes constantly at interval. My solution: Exploit HTTP keep-alive to maintain a connection with the browser, printing to the browser as I recieve new data. My problem: How to tell when the user has closed the browser or changed pages. What I've tried: I've tried trapping for signals, but it seems like no signals are being thrown...even when I print to a dead socket. It seems as if printing to a dead socket throws no errors. My question(s): 1)Is the browser really closing the connection when the user switches pages? My guess is a yes.; 2)How do I tell when the user has disconnected?; 3)If the browser isn't closing the connection when the user has left the page, then how do I force it to? Blessed are those who follow the ways of Perl. ~Michael D. Stemle, Jr.
  • Comment on How to tell when a browser disconnects...

Replies are listed 'Best First'.
Re: How to tell when a browser disconnects...
by Aristotle (Chancellor) on Nov 18, 2002 at 17:52 UTC
Re: How to tell when a browser disconnects...
by nothingmuch (Priest) on Nov 18, 2002 at 17:32 UTC
    You really ought to use <br> tags...

    If the browser closes or not is the browser's problem. Normally I would guess it closes, unless some prefetching to offline copies is made.

    I would guess the following code should tell you wether you're still interested in the socket:
    my $rout = ''; vec ($rout,fileno(SOCKET),1) = 1; while (select(undef,$rout,undef,0.5)){ syswrite SOCKET, $lines; } close SOCKET;

    The four arg select function tests for readiness on a number of file descriptors. You have to construct vectors with the bits set at the indices which are the file desc numbers you want to test. The first vector will test for read, the second for write, and the third for err (on 3 way pipes). The fourth argument is the timeout, and can be a float.

    I'm not sure this will work on the browser side, as every browser will behave differently with an unfinished source.

    -nuffin
    zz zZ Z Z #!perl
Re: How to tell when a browser disconnects...
by PodMaster (Abbot) on Nov 19, 2002 at 04:06 UTC
Re: How to tell when a browser disconnects...
by waswas-fng (Curate) on Nov 18, 2002 at 17:29 UTC
    1: Use meta refresh have it hit your cgi every x seconds. 2:Keep a session db, if the browser has not hit the CGI in a timeout period, log out and force relogin on next hit.

    -Waswas

    oops misread the nor in nor refreshes against the server ignore this.