Last active
December 12, 2015 12:48
-
-
Save jberger/4774073 to your computer and use it in GitHub Desktop.
Mojo<->Mojo websocket problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojo::Base -strict; | |
use Mojo::UserAgent; | |
use Mojo::JSON 'j'; | |
my $ua = Mojo::UserAgent->new; | |
my $url = shift; | |
my $data = {count => 1}; | |
my $delay = Mojo::IOLoop->delay; | |
$delay->begin; | |
$ua->websocket( $url => sub { | |
my ($ua, $tx) = @_; | |
$tx->on( text => sub { | |
my ($ws, $message) = @_; | |
say "Client got ==> $message"; | |
$ws->finish; | |
}); | |
$tx->on( finish => sub { $delay->end }); | |
say "Client start"; | |
$tx->send({ text => j($data) }); | |
}); | |
$delay->wait unless $delay->ioloop->is_running; | |
__END__ | |
perl client.pl ws://localhost:3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use Mojo::JSON 'j'; | |
websocket '/' => sub { | |
my $self = shift; | |
$self->on( text => sub { | |
my ($ws, $text) = @_; | |
say "Server got ==> $text"; | |
my $data = j($text); | |
$data->{count}++; | |
$ws->send({ text => j( $data ) }); | |
}); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment