Skip to content

Commit

Permalink
add ping pong
Browse files Browse the repository at this point in the history
javascript sample(in connection.js)
```
_ping('ping', 'your_nice_token')
```
this code will send ping event with token.

yancha will sendback pong event with token.
  • Loading branch information
uzulla committed Feb 14, 2014
1 parent eceb8f8 commit 154cc94
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/Yancha/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sub dispatch {
$socket->on( 'join tag' => sub { $self->join_tag( @_ ); } );
$socket->on( 'disconnect' => sub { $self->disconnect( @_ ); } );
$socket->on( 'fukumotosan' => sub { $self->fukumotosan( @_ ); } );
$socket->on( 'ping' => sub { $self->ping( @_ ); } );
$socket->on( 'delete user message' => sub { $self->delete_user_message( @_ ); } );
$self->sys->call_hook( 'connected', $socket, $env );
};
Expand Down Expand Up @@ -181,6 +182,21 @@ sub plusplus {
$self->sys->send_post_to_tag_joined( $post => $post->{ tags } );
}

sub ping {
my ( $self, $socket, $token ) = @_;

$socket->get('user_data' => sub {
my ($socket, $err, $user) = @_;

if(!defined($user)){
$socket->emit('no session');
return;
}

$socket->emit('pong', $token);
});
}

sub fukumotosan {
my ( $self, $socket ) = @_;

Expand Down
8 changes: 8 additions & 0 deletions static/js/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ socket.on('announcement', function (msg) {
announcement(msg)
});

//ping pong
socket.on('pong', function (msg) {
console.log(msg);
});
function _ping(token){
socket.emit('ping', token);
console.log("sent ping :"+token);
}

//サーバから、参加ニックネームリストの更新
socket.on('nicknames', function (nicknames) {
Expand Down
83 changes: 83 additions & 0 deletions t/504_pingpong.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use strict;
use warnings;
use PocketIO::Test;
use t::Utils;
use AnyEvent;
use Yancha;
use Yancha::Client;
use Yancha::DataStorage::DBI;
use Plack::Builder;

BEGIN {
use Test::More;
plan skip_all => 'PocketIO::Client::IO are required to run this test'
unless eval { require PocketIO::Client::IO; 1 };
}

my $testdb = t::Utils->setup_testdb( schema => './db/init.sql' );
my $config = {
'database' => { connect_info => [ $testdb->dsn ] },
'server_info' => {
default_tag => 'PUBLIC',
auth_endpoint => {
'/login' => [ 'Yancha::Auth::Simple' => { name_field => 'nick' } => {} ],
}
},

'plugins' => [
[ 'Yancha::Plugin::NoRec' ],
],
};
my $data_storage = Yancha::DataStorage::DBI->connect(
connect_info => $config->{ database }->{ connect_info } );

my $sys = Yancha->new( config => $config, data_storage => $data_storage );
my $server = builder {
enable 'Session';
$sys->build_psgi_endpoint_from_server_info('auth');
mount '/socket.io' => PocketIO->new(
socketio => $config->{ socketio },
instance => $sys,
);
};

my $client = sub {
my ( $port ) = shift;

my $cv = AnyEvent->condvar;
my $w = AnyEvent->timer( after => 10, cb => sub {
fail("Time out.");
$cv->send;
} );

my $on_connect = sub {
my ( $client ) = @_;
my $count = 0;

$client->socket->on('pong' => sub {
my $post = $_[1];

is( $post, 'my_uuid' );

$cv->send;
});

};

my ( $client ) = t::Utils->create_clients_and_set_tags(
$port,
{ nickname => 'user', on_connect => $on_connect },
);

$client->socket->emit('ping', "my_uuid");


$cv->wait;
};

test_pocketio $server, $client;

ok(1, 'test done');

done_testing;

0 comments on commit 154cc94

Please sign in to comment.