Mojolicious::Lite 㧠WebSocket ã使ã£ããã£ãããä½ã
node.jsの衝撃とWebSocketが拓く未来 (1/2):WebSocketで目指せ! リアルタイムWeb(1) - @IT ã¨ããè¨äºãèªã¿ã¾ãããnode.js ã¨ãã V8 ãç¨ãããµã¼ãã¼ãµã¤ã JavaScript ãã¬ã¼ã ã¯ã¼ã¯ã使ãã¨ç°¡åã«ã¤ãã³ãé§åã®ãµã¼ããæ¸ãããnode-websocket-server.js ã使ã㨠node.js 㧠WebSocket ãµã¼ããå®è£ ã§ãããAjax ã«ãã polling ã Long Polling ãªã©ã¨ WebSocket ã®ã¢ã¼ããã¯ãã£æ¯è¼ã¨ãã£ãå 容ã§ããã
WebSocket ã使ãã¨æ軽ã«ãµã¼ãããã·ã¥çãªã¢ããªã±ã¼ã·ã§ã³ãä½ãã¦å¬ããã®ã§ãããç¾æç¹ã§ã¯ãHTTPãµã¼ãã¼å´ã§ WebSocket ãå¦çããä¸å°ã®å®è£ ãã©ãç¨æãããã¨ããã¨ãããã²ã¨ã¤èª²é¡ã§ããããnode.js ã¯ãã®åçã®ã²ã¨ã¤ã¨ãã¦ããªããªãé¢ç½ãã§ããã
Perl 㧠WebSocket ãµã¼ã ・・・ Mojolicious::Lite
ãã¦ãè¨äºã«è§¦çºããã¤ã¤ãµã¼ãã¼å´ã JavaScript ã§ã¯ãªã Perl ã§æ¸ããããããªã¨è¨ããã¨ã§ Mojolicious::Lite ã使ã£ããµã³ãã«ãä½ã£ã¦ã¿ã¾ãããMojoliciious ã«ã¯ WebSocket ãå¦çã§ãããµã¼ãã¼å®è£ ãå梱ããã¦ãã¦ã
use Mojolicious::Lite; websocket '/echo' => sub { my $self = shift; $self->receive_message(sub { my ($self, $message) = @_; $self->send_message("echo: $message"); }); }; app->start;
ã¨æ¸ãã¦
% perl app.pl --daemon
ã§ãµã¼ãã¼ãèµ·åã ws://localhost:3000/echo ã WebSocket ã®ã¨ã³ããã¤ã³ãã«ãªããreceive_message() ã«æ¸¡ããã³ã¼ã«ããã¯ããã¯ã©ã¤ã¢ã³ãå´ããã¡ãã»ã¼ã¸ãåä¿¡ãã度ã«ããã¯ãããããã«ãªãã¾ãããã³ãã·ã§ã¤ã¯ãã®ã»ã㯠Mojo ãé©å½ã«å¦çãã¦ããã¾ãããæ軽ã§ãã
ãµã³ãã«ã® WebSocket ãã£ããã®ã³ã¼ã
以ä¸ããµã³ãã«ã§ä½ã£ã¦ã¿ããã£ããã§ãã/ ã§ãã£ããç»é¢ãHTMLã§è¿ãã/echo ã WebSocket ã®ã¨ã³ããã¤ã³ããã¡ãã»ã¼ã¸ãåãåãã¨æå»ã¨åãåã£ãããã¹ãããæ¥ç¶ãã¦ãããã¹ã¦ã®ã¯ã©ã¤ã¢ã³ãã«éãã¾ãã
#!/usr/bin/env perl use utf8; use Mojolicious::Lite; use DateTime; use Mojo::JSON; get '/' => 'index'; my $clients = {}; websocket '/echo' => sub { my $self = shift; app->log->debug(sprintf 'Client connected: %s', $self->tx); my $id = sprintf "%s", $self->tx; $clients->{$id} = $self->tx; $self->receive_message( sub { my ($self, $msg) = @_; my $json = Mojo::JSON->new; my $dt = DateTime->now( time_zone => 'Asia/Tokyo'); for (keys %$clients) { $clients->{$_}->send_message( $json->encode({ hms => $dt->hms, text => $msg, }) ); } } ); $self->finished( sub { app->log->debug('Client disconnected'); delete $clients->{$id}; } ); }; app->start; __DATA__ @@ index.html.ep <html> <head> <title>WebSocket Client</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script> <script type="text/javascript" src="/js/ws.js"></script> <style type="text/css"> textarea { width: 40em; height:10em; } </style> </head> <body> <h1>Mojolicious + WebSocket</h1> <p><input type="text" id="msg" /></p> <textarea id="log" readonly></textarea> </body> </html>
ã¯ã©ã¤ã¢ã³ãã® js ã®ä¸èº«ãWebSocket API 㧠/echo ã«æ¥ç¶ãã¦å ¥åºåãé©å½ã«å¦çã
$(function () { $('#msg').focus(); var log = function (text) { $('#log').val( $('#log').val() + text + "\n"); }; var ws = new WebSocket('ws://localhost:3000/echo'); ws.onopen = function () { log('Connection opened'); }; ws.onmessage = function (msg) { var res = JSON.parse(msg.data); log('[' + res.hms + '] ' + res.text); }; $('#msg').keydown(function (e) { if (e.keyCode == 13 && $('#msg').val()) { ws.send($('#msg').val()); $('#msg').val(''); } }); });
Google Chrome 㧠localhost:3000 ã«ã¢ã¯ã»ã¹ãã¡ããã¨åãã¾ããã
ããã㦠WebSocket ã®ãµã¼ãã¼å´ã Perl ã§å®è£ ã§ãããã¨ãããã°å¾ã¯æ¢åã®è³ç£ãçããã¦ããããªãã¨ãèãããã¾ãããå ã®è¨äºã® Activity Monitor ã®ãããªãã®ãããµã¼ãã¼ã§ Perl ã§å®è£ ãã¦ãã¾ã£ã¦ããªã¢ã¼ãã®ãµã¼ãã¼ã¹ãã¼ã¿ã¹ãã°ã©ãã£ã«ã«ã«è¡¨ç¤ºããã¨ãã¢ã¤ãã¢ã¯å°½ãã¾ããã
ã»ã
- http://vti.showmetheco.de/articles/2010/05/more-mojolicious-websocket-examples.html ã«ããã¨ãMojolicious + WebSocket ã®ãã£ã¨è±ªè¯ãªãµã³ãã«ãããã¤ãè¦ããã¾ã
- Perl 㧠WebSocket ã®ãµã¼ãã¼å´ãå¦çãã¦ããä¾ã¯ã»ãã«ãªãããªãã¨æ¤ç´¢ãã¦ããã http://cpansearch.perl.org/src/MIYAGAWA/Twiggy-0.1007/eg/chat-websocket/chat.psgi ã« Twiggy (AnyEvent + PSGI) ãã¼ã¹ã®ãµã³ãã«ãè¦ã¤ããã¾ãããWebSocket ã Plack/PSGI ã§å¦çã§ããã®ã¯å¬ããã§ããã