Skip to content

Instantly share code, notes, and snippets.

@vti
Created July 24, 2010 14:36
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 vti/25a565df562fbee7aedc to your computer and use it in GitHub Desktop.
Save vti/25a565df562fbee7aedc to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
# Disable epoll, kqueue and IPv6
BEGIN { $ENV{MOJO_POLL} = $ENV{MOJO_NO_IPV6} = 1 }
use Mojo::IOLoop;
use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 1;
use IO::Socket::INET;
use Mojolicious::Lite;
use Mojo::Client;
use Mojolicious::Lite;
# Silence
app->log->level('fatal');
# Avoid exception template
app->renderer->root(app->home->rel_dir('public'));
websocket '/' => sub {
my $self = shift;
#$self->res->code(101);
$self->client->async->websocket(
'/echo' => sub {
my $client = shift;
$client->receive_message(
sub {
my ($client, $message) = @_;
$self->send_message($message);
$client->finish;
$self->finish;
}
);
$client->send_message('test1');
}
)->process;
};
websocket '/echo' => sub {
my $self = shift;
$self->receive_message(
sub {
my ($self, $message) = @_;
$self->send_message($message);
}
);
};
my $client = Mojo::Client->singleton->app(app);
# WebSocket /
my $result;
$client->websocket(
'/' => sub {
my $self = shift;
$self->receive_message(
sub {
my ($self, $message) = @_;
$result = $message;
$self->finish;
}
);
}
)->process;
is($result, 'test1', 'right result');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment