• R/O
  • HTTP
  • SSH
  • HTTPS

keitairc: Commit

keitaircメインレポジトリ


Commit MetaInfo

Révision28c7c0d8c6b92ecd6ef9d56c9f95406cae352995 (tree)
l'heure2010-12-02 01:08:10
AuteurISHIKAWA Mutsumi <ishikawa@hanz...>
CommiterISHIKAWA Mutsumi

Message de Log

websocket based messaging support

Change Summary

Modification

--- a/data/public/webkit.js
+++ b/data/public/webkit.js
@@ -46,7 +46,7 @@ jQuery(document).ready(function($) {
4646 }
4747 el = null;
4848
49- var have_ws = ('WebSocket' in window);
49+ var has_ws = ('WebSocket' in window);
5050 var ws = false;
5151
5252 var map_load = true;
@@ -62,7 +62,7 @@ jQuery(document).ready(function($) {
6262 $('#index').load(web_root + session_id + '/index', reinit);
6363
6464 function _ws_ping() {
65- if (have_ws && ws) {
65+ if (has_ws && ws) {
6666 ws.send('ABC');
6767 alert('send ABC');
6868 }
@@ -123,7 +123,7 @@ jQuery(document).ready(function($) {
123123 ws = false;
124124 }
125125
126- if (have_ws && url.match(/all\/([0-9]+)/)) {
126+ if (has_ws && url.match(/all\/([0-9]+)/)) {
127127 var cid = RegExp.$1;
128128 ws = new WebSocket('ws://' + location.host + '/' + session_id + '/push/' + cid);
129129 ws.addEventListener("open",
@@ -158,7 +158,15 @@ jQuery(document).ready(function($) {
158158
159159 $('.internal_form').bind('submit', function () {
160160 $('#m').blur();
161- inner_reload($(this).attr('action'), '', $(this).serializeArray());
161+ if (has_ws && ws) {
162+ ws.send(JSON.stringify({url: $(this).attr('action'),
163+ param: $(this).serializeArray()}));
164+ $('#m', this).val('');
165+ $("input[name='stamp']", this).val(parseInt((new Date)/1000));
166+ $('li.messagenew').addClass('message').removeClass('messagenew');
167+ } else {
168+ inner_reload($(this).attr('action'), '', $(this).serializeArray());
169+ }
162170 return false;
163171 });
164172
--- a/keitairc
+++ b/keitairc
@@ -22,6 +22,7 @@ use HTML::Template;
2222 use HTTP::Response;
2323 use HTTP::Status;
2424 use Digest::MD5 qw(md5);
25+use JSON;
2526
2627 use FindBin;
2728 use lib ("$FindBin::Bin/lib", '/usr/share/keitairc/lib');
@@ -136,8 +137,8 @@ sub http_request {
136137 $kernel->yield('shutdown');
137138 }
138139 } else {
139- # FIXME:: should handle websocket message
140- warn $request;
140+ # handle websocket message
141+ dispatch_stream($request, $heap);
141142 }
142143 }
143144
@@ -202,6 +203,16 @@ sub dispatch {
202203 return action_public($request, $uri) || action_error($request, 404);
203204 }
204205
206+sub dispatch_stream {
207+ my ($request, $heap) = @_;
208+
209+ my $data = decode_json($request);
210+
211+ if ($data->{url} =~ m|^all/(.*)|) {
212+ send_message($data->{param}, $ib->cid2name($1));
213+ }
214+}
215+
205216 ################################################################
206217 # adds session id cookie to http response object
207218 sub add_cookie{
@@ -499,11 +510,31 @@ sub parse_message{
499510 return ($message, $timestamp);
500511 }
501512
502-sub send_message{
503- my $request = shift;
504- my $channel = shift;
513+sub parse_message_plain {
514+ my $plain_req = shift;
505515
506- my ($message, $timestamp) = parse_message($request);
516+ my ($message, $timestamp);
517+
518+ foreach my $req (@$plain_req) {
519+ if ($req->{name} eq 'm') {
520+ $message = $req->{value};
521+ } elsif ($req->{name} eq 'stamp') {
522+ $timestamp = $req->{value};
523+ }
524+ }
525+
526+ return ($message, $timestamp);
527+}
528+
529+sub send_message {
530+ my ($request, $channel) = @_;
531+ my ($message, $timestamp);
532+
533+ if (ref($request) eq 'HTTP::Request') {
534+ ($message, $timestamp) = parse_message($request);
535+ } else {
536+ ($message, $timestamp) = parse_message_plain($request);
537+ }
507538
508539 if(length($message) && length($channel)){
509540 if($ib->update_timestamp($timestamp)){
--- a/lib/Keitairc/IrcBuffer.pm
+++ b/lib/Keitairc/IrcBuffer.pm
@@ -264,7 +264,7 @@ sub compact_channel_name{
264264 ################################################################
265265 sub simple_escape{
266266 my $me = shift;
267- local($_) = shift;
267+ local($_) = shift;
268268 if(defined $_){
269269 s/&/&amp;/g;
270270 s/>/&gt;/g;
@@ -279,7 +279,7 @@ sub simple_escape{
279279 ################################################################
280280 sub colorize{
281281 my $me = shift;
282- local($_) = shift;
282+ local($_) = shift;
283283
284284 my %ct = (
285285 1 => 'Black',
@@ -396,8 +396,6 @@ sub send_stream {
396396 my $stream = $me->{stream}->{$cid};
397397 return if(keys %$stream < 1);
398398
399- warn 'send stream value = ' . $value;
400-
401399 foreach my $k (sort keys %$stream) {
402400 my $st = $me->{stream}->{$cid}->{$k};
403401 my $client = $st->{client};
@@ -408,6 +406,7 @@ sub send_stream {
408406 }
409407 $client->flush();
410408 }
409+
411410 }
412411
413412 1;
Afficher sur ancien navigateur de dépôt.