Created
November 25, 2010 12:24
-
-
Save Mons/715308 to your computer and use it in GitHub Desktop.
AnyEvent::HTTP::Server WS Echo sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use uni::perl; | |
use lib::abs '../../lib'; | |
use AnyEvent::Impl::Perl; # Better to use Impl::Perl for tests | |
use AE; | |
use AnyEvent::HTTP::Server; | |
AnyEvent::HTTP::Server->new( | |
host => '89.208.136.108', | |
port => 8088, | |
request => sub { | |
my $r = shift; | |
my $rpath = $r->{uri}->path; | |
warn "Request $rpath\n"; | |
if ($r->wants_websocket) { | |
$r->upgrade('websocket', sub { # Respond with upgrade | |
if (my $ws = shift) { # Receive websocket object | |
$ws->onmessage(sub { # Setup WebSocket on_message | |
$ws->send("re: @_"); | |
}); | |
$ws->send("Hello from sample!"); # Send initial "Hello!" from server | |
} else { | |
warn "Upgrade failed: @_"; | |
} | |
}); | |
return 1; | |
} else { | |
$r->error(400); | |
return 1; | |
} | |
}, | |
)->start; | |
AE::cv->recv; # Start loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment