Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active December 12, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/4774073 to your computer and use it in GitHub Desktop.
Save jberger/4774073 to your computer and use it in GitHub Desktop.
Mojo<->Mojo websocket problem
#!/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
#!/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