Permalink
Browse files

Merge pull request #10 from powerman/perl

Add a Perl implementation
  • Loading branch information...
2 parents 3006e81 + 777780c commit bf6b7a1f9c1ee637ee10d7facd8f094b9494d1cf @ericmoritz ericmoritz committed Jun 14, 2012
Showing with 74 additions and 0 deletions.
  1. +67 −0 competition/wsdemo.pl
  2. +7 −0 configure_ubuntu.sh
View
67 competition/wsdemo.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+use Socket;
+use Errno qw(EAGAIN);
+use Fcntl;
+use EV;
+use IO::Stream;
+use Protocol::WebSocket;
+use Protocol::WebSocket::Handshake::Server;
+use Protocol::WebSocket::Frame;
+
+
+my ($host, $port) = ('0.0.0.0', 8000);
+socket my $srv_sock, AF_INET, SOCK_STREAM, 0;
+setsockopt $srv_sock, SOL_SOCKET, SO_REUSEADDR, 1;
+bind $srv_sock, sockaddr_in($port, inet_aton($host));
+listen $srv_sock, SOMAXCONN;
+fcntl $srv_sock, F_SETFL, O_NONBLOCK;
+my $srv_w = EV::io($srv_sock, EV::READ, sub {
+ if (accept my $sock, $srv_sock) {
+ IO::Stream->new({
+ fh => $sock,
+ cb => \&server,
+ wait_for => IN|EOF,
+ HS => Protocol::WebSocket::Handshake::Server->new,
+ Frame => Protocol::WebSocket::Frame->new,
+ });
+ }
+ elsif ($! != EAGAIN) {
+ die "accept: $!";
+ }
+});
+
+EV::loop;
+
+sub server {
+ my ($io, $e, $err) = @_;
+ if ($err) {
+ $io->close();
+# warn $err;
+ }
+ elsif ($e & EOF) {
+ $io->close();
+ }
+ elsif (!$io->{HS}->is_done) {
+ $io->{HS}->parse($io->{in_buf});
+ $io->{in_buf} = q{};
+ my $hs_err = $io->{HS}->error;
+ if ($hs_err) {
+ $io->close();
+ warn $hs_err;
+ }
+ elsif ($io->{HS}->is_done) {
+ $io->write( $io->{HS}->to_string );
+ }
+ }
+ else {
+ $io->{Frame}->append( $io->{in_buf} );
+ $io->{in_buf} = q{};
+ while (my $msg = $io->{Frame}->next_bytes) {
+ $io->write( $io->{Frame}->new($msg)->to_bytes );
+ }
+ }
+}
+
View
7 configure_ubuntu.sh
@@ -20,3 +20,10 @@ sudo gem install em-websocket
sudo cabal update
sudo cabal install snap-server snap-core websockets websockets-snap
+
+sudo apt-get install -y libadns1-dev
+echo -e "y\ny\no conf prerequisites_policy follow\no conf commit" | sudo cpan
+sudo cpan Protocol::WebSocket
+sudo cpan EV::ADNS
+yes | sudo cpan IO::Stream
+

0 comments on commit bf6b7a1

Please sign in to comment.