Skip to content

Instantly share code, notes, and snippets.

/open.diff Secret

Created April 8, 2015 14:56
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 anonymous/e199d5304349d7d91b41 to your computer and use it in GitHub Desktop.
Save anonymous/e199d5304349d7d91b41 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm
index 5cb366d..ec5f840 100644
--- a/lib/Mojo/Server/Daemon.pm
+++ b/lib/Mojo/Server/Daemon.pm
@@ -123,6 +123,7 @@ sub _finish {
if ($ws->res->code == 101) {
weaken $self;
$ws->on(resume => sub { $self->_write($id) });
+ $ws->server_open;
}
# Failed upgrade
diff --git a/lib/Mojo/Transaction/WebSocket.pm b/lib/Mojo/Transaction/WebSocket.pm
index 62765d7..1f60c5a 100644
--- a/lib/Mojo/Transaction/WebSocket.pm
+++ b/lib/Mojo/Transaction/WebSocket.pm
@@ -247,6 +247,8 @@ sub server_handshake {
_challenge($req_headers->sec_websocket_key));
}
+sub server_open { shift->emit('open') }
+
sub server_read {
my ($self, $chunk) = @_;
@@ -451,6 +453,15 @@ least one subscriber.
say "Message: $msg";
});
+=head2 open
+
+ $ws->on(open => sub {
+ my $ws = shift;
+ ...
+ });
+
+Emitted when the WebSocket connection has been opened.
+
=head2 text
$ws->on(text => sub {
@@ -682,6 +693,12 @@ Transaction closed server-side, used to implement web servers.
Perform WebSocket handshake server-side, used to implement web servers.
+=head2 server_open
+
+ $ws->server_open;
+
+WebSocket connection established server-side, used to implement web servers.
+
=head2 server_read
$ws->server_read($data);
diff --git a/lib/Mojolicious/Guides/Cookbook.pod b/lib/Mojolicious/Guides/Cookbook.pod
index b1fd0fa..e259b82 100644
--- a/lib/Mojolicious/Guides/Cookbook.pod
+++ b/lib/Mojolicious/Guides/Cookbook.pod
@@ -582,12 +582,18 @@ L<Mojolicious::Controller/"send">.
websocket '/echo' => sub {
my $c = shift;
- # Opened
- $c->app->log->debug('WebSocket opened');
+ # Handshake
+ $c->app->log->debug('WebSocket handshake');
# Increase inactivity timeout for connection a bit
$c->inactivity_timeout(300);
+ # Opened
+ $c->on(open => sub {
+ my $c = shift;
+ $c->app->log->debug('WebSocket opened');
+ });
+
# Incoming message
$c->on(message => sub {
my ($c, $msg) = @_;
diff --git a/t/mojolicious/websocket_lite_app.t b/t/mojolicious/websocket_lite_app.t
index 66bb32e..11fe73b 100644
--- a/t/mojolicious/websocket_lite_app.t
+++ b/t/mojolicious/websocket_lite_app.t
@@ -84,6 +84,10 @@ websocket '/once' => sub {
);
};
+websocket '/close' => sub {
+ shift->on(open => sub { shift->finish(1001) });
+};
+
under '/nested';
websocket sub {
@@ -290,6 +294,9 @@ $t->websocket_ok('/once')->send_ok('hello')
->send_ok('hello')->message_ok->message_is('ONE: hello')->send_ok('hello')
->message_ok->message_is('ONE: hello')->finish_ok;
+# WebSocket gets closed right away
+$t->websocket_ok('/close')->finished_ok(1001);
+
# Nested WebSocket
$t->websocket_ok('/nested')->send_ok('hello')
->message_ok->message_is('nested echo: hello')->finished_ok(1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment