4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
use Continuity; |
use Continuity; |
7 |
use Continuity::REPL; |
#use Continuity::REPL; |
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
|
|
10 |
use base 'Frey'; |
use base 'Frey'; |
17 |
|
|
18 |
#$repl = Continuity::REPL->new; |
#$repl = Continuity::REPL->new; |
19 |
$server = Continuity->new( |
$server = Continuity->new( |
20 |
port => 16001, |
port => 16001, |
21 |
path_session => 1, |
path_session => 1, |
22 |
cookie_session => 'sid', |
cookie_session => 'sid', |
23 |
callback => \&main, |
callback => \&main, |
24 |
|
debug_level => 1, |
25 |
|
staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ }, |
26 |
); |
); |
|
$server->debug_level( 2 ); |
|
|
sub run { |
|
27 |
|
|
28 |
|
sub run { |
29 |
$server->loop; |
$server->loop; |
30 |
} |
} |
31 |
|
|
|
my @callbacks; |
|
|
my $callback_count; |
|
|
|
|
|
sub gen_link { |
|
|
my ($text, $code) = @_; |
|
|
$callbacks[$callback_count++] = $code; |
|
|
return qq{<a href="?cb=$callback_count">$text</a>}; |
|
|
} |
|
|
|
|
|
sub process_links { |
|
|
my $request = shift; |
|
|
my $cb = $request->param('cb'); |
|
|
if (exists $callbacks[$cb]) { |
|
|
$callbacks[$cb]->($request); |
|
|
delete $callbacks[$cb]; |
|
|
} |
|
|
} |
|
|
|
|
32 |
# This is the main entrypoint. We are looking for one of three things -- a |
# This is the main entrypoint. We are looking for one of three things -- a |
33 |
# pushstream, a sent message, or a request for the main HTML. We delegate each |
# pushstream, a sent message, or a request for the main HTML. We delegate each |
34 |
# of these cases, none of which will return (they all loop forever). |
# of these cases, none of which will return (they all loop forever). |
35 |
sub main { |
sub main { |
36 |
my ($req) = @_; |
my ($req) = @_; |
37 |
|
|
38 |
my $path = $req->request->url->path; |
my $path = $req->request->url->path; |
39 |
warn "REQUEST: $path\n"; |
warn "REQUEST: $path\n"; |
40 |
|
|
41 |
warn $req->request->header('User_Agent'); |
warn $req->request->header('User_Agent'); |
42 |
#warn dump( $req ); |
#warn dump( $req ); |
43 |
|
|
44 |
# If this is a request for the pushtream, then give them that |
# If this is a request for the pushtream, then give them that |
45 |
if($path =~ /pushstream/) { |
if($path =~ /pushstream/) { |
46 |
pushstream($req); |
pushstream($req); |
47 |
} |
} |
48 |
|
|
49 |
# If they are sending us a message, we give them a thread for that too |
# If they are sending us a message, we give them a thread for that too |
50 |
if($path =~ /sendmessage/) { |
if($path =~ /sendmessage/) { |
51 |
send_message($req); |
send_message($req); |
52 |
} |
} |
53 |
|
|
54 |
# Otherwise, lets give them page |
# Otherwise, lets give them page |
55 |
send_page($req); |
send_page($req); |
56 |
} |
} |
57 |
|
|
58 |
# Here we accept a connection to the browser, and keep it open. Meanwhile we |
# Here we accept a connection to the browser, and keep it open. Meanwhile we |