Have you had a look at Date::Calc or Date::Manip? They provide a lot of useful function for manipulating dates (eg deltas).rdfield | [reply] |
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!,
print "profeth still\n" if /bird|devil/; | [reply] |
The easiest way, that I use quite often to cache/expire, data is using File::Cache, or its more modern cousin, Cache::Cache. They're both relatively easy to use.
It'd be also fairly easy to do it using DB_File (or the like), but you'd have to do the math yourself (File::Cache is a nice ready-made solution that works very well, while Cache::Cache is also good, but has some portability issues yet to be worked out).
Enjoy.
| [reply] |
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 | [reply] |
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. | [reply] |
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. :) | [reply] [d/l] |