Created
May 31, 2011 00:35
-
-
Save ktat/999684 to your computer and use it in GitHub Desktop.
WebSocket perl client(select)
This file contains hidden or 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/perl | |
use utf8; | |
use strict; | |
use warnings; | |
use Protocol::WebSocket::Handshake::Client; | |
use Protocol::WebSocket::Frame; | |
use IO::Socket; | |
use IO::Select; | |
use open ':utf8'; | |
use open ':std'; | |
$| = 1; | |
run(); | |
sub run { | |
my $select = IO::Select->new; | |
my $s = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => 50000, Proto => 'tcp'); | |
return unless $s->connected; | |
my $hc = Protocol::WebSocket::Handshake::Client->new(url => 'ws://127.0.0.1:50000'); | |
$select->add($s); | |
# handshare request | |
print $s $hc->to_string; | |
while (my (@sockets) = $select->can_read(1)) { | |
# recieving response | |
foreach my $s (@sockets) { | |
$s->sysread(my $buf, 1000); | |
print $buf; | |
} | |
} | |
print "\n\n--- finish handshare with chat server ---\n\n"; | |
$select->add(\*STDIN); | |
local @SIG{qw/INT TERM ALRM/} = (sub { client_exit($select); }) x 3; | |
my @messages; | |
LOOP: | |
while (1) { | |
foreach my $socket ($select->can_read(0.5)) { | |
if (ref $socket eq 'IO::Socket::INET') { | |
# read from websocket | |
$socket->sysread(my $buf, 1000); | |
print $buf; | |
} else { | |
my $msg = <STDIN>; | |
last LOOP unless $msg; | |
chomp($msg); | |
if ($msg) { | |
push @messages, Protocol::WebSocket::Frame->new($msg)->to_string; | |
} | |
} | |
foreach my $socket ($select->can_write(0.5)) { | |
if (ref $socket eq 'IO::Socket::INET' and my $msg = shift @messages) { | |
print $socket $msg;; | |
} | |
} | |
} | |
} | |
client_exit($select); | |
} | |
sub client_exit { | |
my ($select) = @_; | |
foreach my $s ($select->handles) { | |
$select->remove($s); | |
if (ref $s eq 'IO::Socket::INET') { | |
if ($s->connected) { | |
# close connection | |
print $s "\xFF\x00"; | |
close $s | |
} | |
} | |
} | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment