Skip to content

Instantly share code, notes, and snippets.

@clkao
Created March 6, 2010 01:51
Show Gist options
  • Save clkao/323425 to your computer and use it in GitHub Desktop.
Save clkao/323425 to your computer and use it in GitHub Desktop.
my $bus = AnyMQ->new;
builder {
enable "Static", path => sub { s!^/static/!! }, root => 'static';
enable "Hippie",
root => '/_hippie',
handler => sub {
my $env = shift;
my $room = $env->{'hippie.args'};
my $topic = $bus->topic($room);
if ($env->{PATH_INFO} eq '/message') {
my $msg = $env->{'hippie.message'};
$msg->{time} = time;
$msg->{address} = $env->{HTTP_REMOTE};
$topic->publish($msg);
}
else {
my $h = $env->{'hippie.handle'}
or return [ '400', [ 'Content-Type' => 'text/plain' ], [ "" ] ];
if ($env->{PATH_INFO} eq '/init') {
my $sub = $bus->new_listener($h->id);
$sub->subscribe( $topic );
$sub->poll(sub { $h->send_msg($_[0]) });
}
elsif ($env->{PATH_INFO} eq '/error') {
warn "==> disconnecting $env->{'hippie.handle'}";
}
else {
die "unknown hippie message";
}
}
return [ '200', [ 'Content-Type' => 'text/plain' ], [ "" ] ]
};
$app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment