Created
May 14, 2011 12:22
-
-
Save vividsnow/972171 to your computer and use it in GitHub Desktop.
SocketIO <=> PocketIO test app ( reqs: WebSocketMain.swf and socket.io.min.js from github.com/LearnBoost/Socket.IO )
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
<html> | |
<head> | |
<script type="application/x-javascript"> | |
WEB_SOCKET_SWF_LOCATION = 'WebSocketMain.swf'; | |
</script> | |
<script src="socket.io.min.js"></script> | |
<script src="//yandex.st/prototype/1.7.0.0/prototype.min.js"></script> | |
<script type="application/x-javascript"> | |
Event.observe(document, 'dom:loaded', function (){ | |
var socket = new io.Socket(window.location.hostname, { | |
transports: ['xhr-polling'], /* choose from ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling'] */ | |
rememberTransport: false | |
}); | |
socket.connect(); | |
socket.on('connect', function (json) { | |
document.body.insert(new Element('div').update(Object.toJSON(json))); | |
$('say').observe('keypress', function (e) { | |
if (e.keyCode == Event.KEY_RETURN) { | |
var el = e.findElement(); | |
this.send(el.getValue()); | |
el.value = ''; | |
} | |
}.bindAsEventListener(this)); | |
}.bind(socket)); | |
socket.on('disconnect', function (json) { | |
document.body.insert(new Element('div').update(Object.toJSON(json))); | |
}); | |
socket.on('message', function (json) { | |
document.body.insert(new Element('div').update(Object.toJSON(json))); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<input type="text" id="say" /> | |
</body> | |
</html> |
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
BEGIN { | |
use File::Basename (); | |
use File::Spec (); | |
my $root = File::Basename::dirname(__FILE__); | |
$root = File::Spec->rel2abs($root); | |
unshift @INC, "$root/../lib"; | |
} | |
use PocketIO; | |
use Plack::Builder; | |
use Plack::Middleware::Static; | |
builder { | |
mount '/socket.io' => PocketIO->new( | |
handler => sub { | |
my $self = shift; | |
$self->on_message( | |
sub { | |
my $self = shift; | |
my ($message) = @_; | |
$self->send_broadcast({message => [$self->id, $message]}); | |
$self->send_message({message => [$self->id, $message]}); | |
} | |
); | |
$self->on_disconnect( | |
sub { | |
my $self = shift; | |
$self->send_broadcast( | |
{announcement => $self->id . ' disconnected'}); | |
} | |
); | |
$self->send_message({buffer => ['hey']}); | |
$self->send_message({muffer => ['ha']}); | |
$self->send_message({guffer => ['ha','ha']}); | |
$self->send_broadcast({announcement => $self->id . ' connected'}); | |
} | |
); | |
mount '/' => builder { | |
enable "Static", | |
path => qr/\.(?:js|css|jpe?g|gif|png|html?|js|css|swf|ico)$/, | |
root => $root; | |
enable "SimpleLogger", level => 'debug'; | |
sub { | |
[ 200, | |
['Content-Type' => 'text/html'], | |
['Open <a href="/chat.html">chat</a>.'] | |
]; | |
}; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment