Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: chatterbox like server app

by bduggan (Pilgrim)
on Sep 17, 2008 at 19:02 UTC ( [id://712100]=note: print w/replies, xml ) Need Help??


in reply to chatterbox like server app

Here's a little ajax one I just put together. Enjoy!
#!/usr/bin/perl -w use strict; use CGI; use CGI::Ajax; use Storable qw/lock_store lock_retrieve/; our $file = '/tmp/messages'; our $MAX_MESSAGES = 10; our $table = eval { lock_retrieve($file) } || {}; my $cgi = new CGI; my $pjx = new CGI::Ajax( 'do_chat' => \&do_chat ); print $pjx->build_html( $cgi, \&show_page); sub do_chat { my ( $name, $msg ) = @_; if ( $name && $msg ) { push @{ $table->{messages} }, [ $name, $msg ]; shift @{ $table->{messages} } while @{ $table->{messages} } > $MAX_MESSAGES; lock_store $table, $file; } return join "\n", map "[" . $cgi->escapeHTML($_->[0]) . "] : " . $cgi->escapeHTML( $_->[1] ), @{ $table->{messages} }; } sub show_page { return $cgi->start_html( -onload => q{setInterval( 'do_chat( [],[\\'result\\'] ) +',1000 );} ) . $cgi->pre( { id => 'result' }, " " ) . $cgi->textfield( -name => 'name', -size => 10, -value => 'name', -id => 'name' ) . $cgi->textfield( -name => 'msg', -size => 50, -value => 'message', -id => 'msg' ) . $cgi->submit( -name => 'speak', -value => 'speak', -onclick => q{do_chat( ['name','msg'],['result'] );} ) . $cgi->end_html; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://712100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found