Mojoã§WebSocketããã¨ãã®ã¾ã¨ã
Mojolicious::Liteã§WebSocketã使ãã¾ã§ã«ãã£ãããããªãã¨ã
ããã¦ãã©è½ã¡çãã¦ããã®ã§ããã£ã¨ã¾ã¨ãã¦ã¿ãã
- Mojoå´ã§ãããã¨
- Clientå´ã§ãããã¨
- Varnishã®è¨å®ãå¤ããå¿ è¦ããã
- Androidã§ã¯å¯¾å¿ãã©ã¦ã¶ãå°ãªã
- SoftBankã¯80çªãã¼ã以å¤ãWifiã§ã¤ãªãã¾ããã
Mojoå´ã§ãããã¨
webscoketã¡ã½ããã使ã£ã¦ãwebsocketéä¿¡ãåãä»ããURLãç¨æã
å種ã¤ãã³ãã«å¯¾ããåä½ãè¨å®ããã
$self->onã®ç¬¬1å¼æ°ã«ã¤ãã³ãã第2å¼æ°ã«åä½ããµãã«ã¼ãã³ãªãã¡ã¬ã³ã¹ã§æ¸¡ãã°ããã
$self->on(message => sub {...}); $self->on(finish => sub {...});
ãã¹ã段éã§ã®å®è£ ã¯ã¨ãããããããªæãã
my $clients = {}; websocket '/websocket/:rid' => {rid => undef}, sub { my $self = shift; $self->app->log->debug('socket open'); my $id = sprintf "%s", $self->tx; my $rid = $self->param('rid'); $clients->{$rid}{$id} = $self->tx; $self->on(message => sub { my ($self, $msg) = @_; $self->app->log->debug($msg); #my $json = Mojo::JSON->new->encode($msg); for (keys %{$clients->{$rid}}) { $clients->{$rid}{$_}->send_message($msg); } }); $self->on(finish => sub { my $self = shift; $self->app->log->debug('WebSocket closed'); }); };
clientå´ã§ãããã¨
WebSocketã®ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¨ãå¼æ°ã«ãã¦ãããURLã«å¯¾ãã¦WebSocketã®ã³ãã¯ã·ã§ã³ãè²¼ã£ã¦ãããã
var ws = new WebSocket("ws://"+location.host+"/websocket/"+rid);
åã¤ãã³ãã«æåãè¨å®ãã
// socket openæã®å¦ç ws.onopen = function(){ ... }; // messageãåä¿¡ããéã®å¦ç ws.onmessage = function(msg){ ... }; // éä¿¡çµäºæã»åææã®å¦ç ws.onclose = function(){...}; // ãã¿ã³ãããããã¡ãã»ã¼ã¸éä¿¡ $("#btn").click(function(){... ws.send(message); };
Varnishã®è¨å®ãå¤ããå¿ è¦ããã
詳細ã¯ä»¥åæ¸ããè¨äºã«ã¦
http://mizuki-r.hatenablog.com/entry/2012/02/18/212913
è¦ããã«ãVarnishã¯WebSocketã¨æå®ã¨ããã®ã«å¿ è¦ãªHeaderãéä¸ã§ããè½ã¨ãã¡ããã®ã§ãã¡ããã¨ããã¯ã¨ã³ãã«å±ããããã«ãã¾ãããããã¨ãã話ã
Androidã§ã¯å¯¾å¿ãã©ã¦ã¶ãå°ãªã
åã使ã£ã¦ããAndroidBlowser
- Blowser
- Dolphin Blowser
- Firefox
ç¾ç¶WebSocketãå®è£ ããã¦ããã®ã¯
ããããã¨ããããFirefoxã§åãã®ã§ã¾ããããã¨ãããã¨ã§Operaã¯è©¦ãã¦ããªãã®ããã
ã¾ããFirefoxã§åä½ãããããã«ã¯ãWebSocketã§ã¯ãªããMozWebSocketã使ãå¿
è¦ãããã
Firefox11ã§WebSocketã§ä½¿ããããã«ãªãã¨ããåãããããã©ããã¾ã¯ã¾ã 10ãªã®ã§ãFirefoxã§åãããããã°MozWebSocketã使ãã¾ãããã
var socket; if (typeof WebSocket != 'undefined') socket = new WebSocket(ws); else if (typeof MozWebSocket != 'undefined') socket = new MozWebSocket(ws); else { alert('WebSocketé対å¿ã§ã'); return false; }
SoftBankã¯80çªãã¼ã以å¤ãç¡ç·LANã§ã¤ãªãã¾ããã
ãããéå»ã®è¨äºã«ã¦ã
http://mizuki-r.hatenablog.com/entry/2012/02/25/094947
SoftBankã®Smartphoneã¯3Gãããªãã¦ç¡ç·LANã§æ¥ç¶ããããWebsocketã®éä¿¡ã«ã¯80çªãã¼ã以å¤ã使ãã¾ãããã¨ããã話ã§ãã
WebSocketã®Timeoutæéã¯300ç§ããããã
ã»ã£ã¨ãã¨ããTimeoutãã¡ããã¾ãã
æ確ã«ãã®Timeoutæéãå¶å¾¡ããæ¹æ³ããªãã£ããããã®ã§ãã¨ããããå®æçã«pingãéã£ã¦ãã¾ããã
var connection_interval = setInterval(function(){socket.send('ping')}, 200000);
ãããã«
ã¨ããããGitHubã«ããã¨ããããã
https://github.com/rymizuki/SlidePlus/tree/websocket