1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
# "HTTP Push" is not readily attainable, so instead we will simulate it using a |
# server of yet unnamed framework |
|
# long-pull, aka "Comet". The client browser simply opens an HTTP connection to |
|
|
# the server and waits for a response. The server doesn't respond until there |
|
|
# is some event (here a new message), giving the appearance of HTTP-push. |
|
|
# |
|
|
# Each user gets three continuations for these three cases: |
|
|
# |
|
|
# - Initial load or reload of the page |
|
|
# - Sending a message (uses AJAX on the client) |
|
|
# - Recieving messages (uses COMET on the client) |
|
4 |
|
|
5 |
|
use warnings; |
6 |
use strict; |
use strict; |
7 |
|
|
8 |
use lib 'lib'; |
use lib 'lib'; |
9 |
|
|
10 |
use Continuity; |
use Continuity; |
11 |
use Continuity::REPL; |
use Continuity::REPL; |
12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
|
|
14 |
use View; |
use HTML; |
|
|
|
|
use Template::Declare; |
|
|
use Template::Declare::Tags; # defaults to 'HTML' |
|
|
Template::Declare->init( roots => ['View']); |
|
|
warn "templates = ",dump( Template::Declare->templates ); |
|
15 |
|
|
16 |
my @messages; # Global (shared) list of messages |
my @messages; # Global (shared) list of messages |
17 |
my $got_message; # Flag to indicate that there is a new message to display |
my $got_message; # Flag to indicate that there is a new message to display |
18 |
|
|
19 |
use vars qw( $repl $server @messages ); |
use vars qw( $repl $server ); |
20 |
$repl = Continuity::REPL->new; |
$repl = Continuity::REPL->new; |
21 |
|
|
22 |
$server = Continuity->new( |
$server = Continuity->new( |
96 |
sub send_base_page { |
sub send_base_page { |
97 |
my ($req) = @_; |
my ($req) = @_; |
98 |
while(1) { |
while(1) { |
99 |
warn "base page"; |
warn "param = ",dump($req->param); |
100 |
$req->print( Template::Declare->show( 'user' ) ); |
|
101 |
|
my $id = $req->param('id'); |
102 |
|
# $id = 1 unless $id =~ m/^\d+$/; |
103 |
|
|
104 |
|
use Strix::User; |
105 |
|
my $user = Strix::User->new( id => $id ); |
106 |
|
|
107 |
|
if ( ! $user ) { |
108 |
|
$req->print( "Can't find user with id $id" ); |
109 |
|
} else { |
110 |
|
$req->print( HTML->view( 'user', $user ) ); |
111 |
|
} |
112 |
|
|
113 |
=for later |
=for later |
114 |
$req->print(qq{ |
$req->print(qq{ |
115 |
<html> |
<html> |