From d9c8904c14c151d9e935be8d53ff9d99ca368a2b Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 17 Jun 2015 19:07:23 +0200 Subject: [PATCH] Manage invitation responses in the signaling heartbeat Instead of the ping API call, which results in almost instantaneous responses And now the ping API is only used to update room activity timestamp, and will be able to get removed soon --- public/js/vroom.js | 11 ++++++++ vroom.pl | 76 ++++++++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/public/js/vroom.js b/public/js/vroom.js index 87029f6..2870fc8 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -1506,6 +1506,17 @@ function initVroom(room) { getRoomInfo('join'); }); + //Notification from the server + webrtc.connection.connection.on('notification', function(data) { + if (!data.payload.class || !data.payload.class.match(/^(success)|(info)|(warning)|(error)$/)){ + data.payload.class = 'info'; + } + $.notify(data.payload.msg, { + className: data.payload.class, + autoHide: false + }); + }); + // When we joined the room webrtc.on('joinedRoom', function(){ // Check if sound is detected and warn if it hasn't diff --git a/vroom.pl b/vroom.pl index c5998ee..d3ffdd4 100755 --- a/vroom.pl +++ b/vroom.pl @@ -135,7 +135,7 @@ helper valid_room_name => sub { helper valid_id => sub { my $self = shift; my ($id) = @_; - if ($id !~ m/^\d+$/){ + if (!$id || $id !~ m/^\d+$/){ return 0; } return 1; @@ -1019,6 +1019,7 @@ websocket '/socket.io/:ver/websocket/:id' => sub { return $self->send('Bad session'); } + $self->app->log->debug("Adding new peer $id to the global peer hash"); # We create the peer in the global hash $peers->{$id}->{socket} = $self->tx; # And set the initial "last seen" flag @@ -1026,6 +1027,9 @@ websocket '/socket.io/:ver/websocket/:id' => sub { # Associate the unique ID and name $peers->{$id}->{id} = $self->session('id'); $peers->{$id}->{name} = $self->session('name'); + # Register the i18n stash, for localization will be available in the main IOLoop + # Outside of Mojo controller + $peers->{$id}->{i18n} = $self->stash->{i18n}; # When we recive a message, lets parse it as e Socket.IO one $self->on('message' => sub { @@ -1166,22 +1170,49 @@ websocket '/socket.io/:ver/websocket/:id' => sub { # Send heartbeats to all websocket clients # Every 3 seconds Mojo::IOLoop->recurring( 3 => sub { - foreach my $peer ( keys %$peers ) { + foreach my $id (keys %{$peers}){ # This shouldn't happen, but better to log an error and fix it rather - # than looping indefinitly on a bogus entry if something went wrong - if (!$peers->{$peer}->{socket}){ - app->log->debug("Garbage found in peers (peer $peer)\n" . Dumper($peers->{$peer})); - delete $peers->{$peer}; + # than looping indefinitly on a bogus entry if something went wron + if (!$peers->{$id}->{socket}){ + app->log->debug("Garbage found in peers (peer $id has no socket)\n"); + delete $peers->{$id}; } # If we had no reply from this peer in the last 15 sec # (5 heartbeat without response), we consider it dead and remove it - elsif ($peers->{$peer}->{last} < time - 15){ - app->log->debug("Peer $peer didn't reply in 15 sec, disconnecting"); - $peers->{$peer}->{socket}->finish; - delete $peers->{$peer}; + elsif ($peers->{$id}->{last} < time - 15){ + app->log->debug("Peer $id didn't reply in 15 sec, disconnecting"); + $peers->{$id}->{socket}->finish; + delete $peers->{$id}; } else { - $peers->{$peer}->{socket}->send(Protocol::SocketIO::Message->new( type => 'heartbeat' )); + my $invitations = app->get_invitation_list($peers->{$id}->{id}); + foreach my $invit (keys %{$invitations}){ + my $msg = ''; + $msg .= sprintf($peers->{$id}->{i18n}->localize('INVITE_REPONSE_FROM_s'), $invitations->{$invit}->{email}) . "\n" ; + if ($invitations->{$invit}->{response} && $invitations->{$invit}->{response} eq 'later'){ + $msg .= $peers->{$id}->{i18n}->localize('HE_WILL_TRY_TO_JOIN_LATER'); + } + else{ + $msg .= $peers->{$id}->{i18n}->localize('HE_WONT_JOIN'); + } + if ($invitations->{$invit}->{message} && $invitations->{$invit}->{message} ne ''){ + $msg .= "\n" . $peers->{$id}->{i18n}->localize('MESSAGE') . ":\n" . $invitations->{$invit}->{message} . "\n"; + } + app->mark_invitation_processed($invitations->{$invit}->{token}); + $peers->{$id}->{socket}->send( + Protocol::SocketIO::Message->new( + type => 'event', + data => { + name => 'notification', + args => [{ + payload => {msg => $msg, class => 'success'} + }] + } + ) + ); + } + # Send the heartbeat + $peers->{$id}->{socket}->send(Protocol::SocketIO::Message->new( type => 'heartbeat' )) } } }); @@ -1572,28 +1603,7 @@ any '/api' => sub { # Handle activity pings sent every minute by each participant elsif ($req->{action} eq 'ping'){ $self->update_room_last_activity($room->{name}); - # Check if we got any invitation response to process - my $invitations = $self->get_invitation_list($self->session('id')); - my $msg = ''; - foreach my $invit (keys %{$invitations}){ - $msg .= sprintf($self->l('INVITE_REPONSE_FROM_s'), $invitations->{$invit}->{email}) . "\n" ; - if ($invitations->{$invit}->{response} && $invitations->{$invit}->{response} eq 'later'){ - $msg .= $self->l('HE_WILL_TRY_TO_JOIN_LATER'); - } - else{ - $msg .= $self->l('HE_WONT_JOIN'); - } - if ($invitations->{$invit}->{message} && $invitations->{$invit}->{message} ne ''){ - $msg .= "\n" . $self->l('MESSAGE') . ":\n" . $invitations->{$invit}->{message} . "\n"; - } - $msg .= "\n"; - $self->mark_invitation_processed($invitations->{$invit}->{token}); - } - return $self->render( - json => { - msg => $msg, - } - ); + return $self->render(json => {}); } # Update room configuration elsif ($req->{action} eq 'update_room_conf'){