-
-
Save jberger/67f795b8c367c5a83c70 to your computer and use it in GitHub Desktop.
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
use Mojo::Base -base; | |
use 5.20.0; | |
use Mojo::UserAgent; | |
my $ua = Mojo::UserAgent->new; | |
$ua->websocket("ws://localhost:3000/code.json" => sub { | |
my ($ua, $tx) = @_; | |
return unless $tx->is_websocket; | |
$tx->on(finish => sub { | |
my ($tx, $code, $reason) = @_; | |
say "WebSocket closed with status $code."; | |
}); | |
$tx->on(message => sub { | |
my ($tx, $msg) = @_; | |
say "WebSocket message: $msg"; | |
}); | |
$tx->send({json => {msg => "Hello"}}); | |
}); | |
Mojo::IOLoop->start; | |
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
use Mojolicious::Lite; | |
use 5.20.0; | |
use experimental 'signatures'; | |
websocket '/code.json' => sub ($c) { | |
$c->app->log->debug('WebSocket opened'); | |
$c->inactivity_timeout(300); | |
$c->on(message => sub ($c, $msg) { | |
$c->app->log->debug("msg: $msg"); | |
}); | |
$c->on(json => sub ($tx, $hash) { | |
$c->app->log->debug("WebSocket message via JSON: $$hash{msg}"); | |
}); | |
$c->on(finish => sub ($c, $code) { | |
$c->app->log->debug("WebSocket closed with status $code"); | |
}); | |
}; | |
app->start; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment