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

Hi. I'm currently working on building a HTML based multi-user chat room on a UNIX server. The problem I am facing at the moment is finding a way how to make the chat script know when a user is no longer chatting. Lets say they don't bother log off and just leave. The user's username will still appear on the chatters list even though he or she has not been using the room for ages. Does anyone know of any techniques that can enable the perl script to determine if a user has not been chatting for x amount of time? I've got a vague idea of comparing the current time to the time the last post the chatter has made, but I don't even know how to do that since gmtime outputs a letters and numbers. Somebody please help me on this. Thanks. Anh

Replies are listed 'Best First'.
Re: CGI chat room problem
by rdfield (Priest) on Mar 12, 2002 at 13:45 UTC
    Have you had a look at Date::Calc or Date::Manip? They provide a lot of useful function for manipulating dates (eg deltas).

    rdfield

Re: CGI chat room problem
by ChOas (Curate) on Mar 12, 2002 at 13:50 UTC
    Hi,

    I've done the same thing (build a chat, that is)...

    You could do a couple of things, run a chat daemon (A
    process that just runs, and gets information from your
    chat script, and processes this), good thing here is that you
    can have a loop in this program which just compares the
    current time (as from "time()" (Seconds sinds the Epoch))
    with the last time a user typed something (The way I did it
    was have a hash, based on username, and that would store
    amongst other things the last time a user typed something)
    if the difference is bigger than, say, 5 minutes, it is
    safe to delete this user from the hash as being an active user.

    Another option is to have the script keep it`s state in
    either a plain text file, or a database, whatever, whenever
    an event occurs which calls your script (user
    loging in, someone talking, whatever) it processes the file,
    or database, and takes out idle users doing the same
    compare I explained before.

    Does this help ?

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
(crazyinsomniac: File::Cache) Re: CGI chat room problem
by crazyinsomniac (Prior) on May 04, 2002 at 01:46 UTC
Re: CGI chat room problem
by dirthurts (Hermit) on Mar 12, 2002 at 17:44 UTC
    Hi Anh, assuming that you are using a meta-refresh to refresh the chat page every few seconds/minutes, you can have a data file for each user currently in the chat. Every time the page gets refreshed (which means their chat window is still open) you can touch that file. Then run a cronjob every couple minutes to take out chatters who haven't touched their data file in x minutes or something.

    Jay

Re: CGI chat room problem
by Anh (Initiate) on Mar 13, 2002 at 11:41 UTC
    Thanks very much for your help, guys. I've been experimenting with DBM hashes with the use of foreach loops to cycle through all keys which would be the usernames, and the values will be the epoch time of their last post. I'm confident that will work best. The chat room I am creating basically requires users to refresh by clicking a form button.
      If you are indeed using a hash to hold times, this should do more or less what you want (this is what I use for something similar. The time distance is 3 minutes, so 3 minutes after they leave their name is removed.):

      use Data::Dumper; #Next 2 lines make sure only one HTTP request deals with database at o +nce open LOCKFILE, ">>lock.lck" or die "Could not open the lock file!"; flock LOCKFILE, LOCK_EX or die "Could not flock the lock file!"; #database.dat holds the physical hash #a 'do' executes the file (rebuilds your time hash) do "database.dat"; #Get the current date values @date = localtime(); #loop through the %userTimes hash to find old entries foreach $user (keys(%userTimes)) { $last_hour = $userTimes{$user}{'hour'}; $last_min = $userTimes{$user}{'min'}; $cur_hour = $date[2]; $cur_min = $date[1]; $expire = 0; #Yes, if 3 minutes old $expire = 1 if ($last_hour == $cur_hour && ($cur_min - $last_min) >= 3 +); #there are some exceptions to the 3 minute deal. #the next lines cover this! if ($last_hour != $cur_hour) { $expire = 1 if $last_hour > $cur_hour; $expire = 1 if $last_min <= 57 && $cur_min >= 0; $expire = 1 if $last_min == 58 && $cur_min >= 1; $expire = 1 if $last_min == 59 && $cur_min >= 2; } if ($expire == 1) { delete $userTimes{$user}; #yep, delete them! } } #Output the hash to the database file thingy $Data::Dumper::Purity = 1; #Need this $Data::Dumper::Indent = 0; #saves some disk space open FILE, ">database.dat"; print FILE Data::Dumper->Dump([\%userTimes], ['*userTimes']); close FILE;
      That's about it! The only thing you really need to concern yourself with is that subtracting the minutes is not good enough. There are some 'race conditions' you need to look at. :)