Hi, i have a trouble to close websocket connection during handshake. How i 
must to close socket properly?

I want to have ability to make this things:
1) When websocket open connection i check some data and close connection if 
needed.
2) When websocket open connection send data and immediately close 
connection.

So, first 2 example /ws, /ws2 - this is what i want.

#!/usr/bin/perl


use strict;
use warnings;
use v5.10;
use Mojolicious::Lite;


# not working, in html should appear "qqqq" and warn 333 not output, error 
of handshake
websocket '/ws' => sub { shift->send('qqqq' => sub { warn 333; shift->finish
}) } => 'ws';


# not working, in html should appear "close", error of handshake
websocket '/ws2' => sub { shift->finish } => 'ws2';


# may be work good, but i don't want to write this urgly code
websocket '/ws3' => sub {
  my $c = shift;
  $c->on(qwe => sub {});
  $c->send('qqqq' => sub { warn 444; shift->finish});
} => 'ws3';

# not working, not call
websocket '/ws4' => sub {
  shift->on(connection => sub {
    warn 555;
    shift->send('qqqq' => sub { warn 666; shift->finish });
  });
} => 'ws4';


get '/' => sub { shift->render(template => 'index') } => 'index';


app->start;
__DATA__


@@ index.html.ep
<div id="msg"></div>
<script>
  ws = new WebSocket('<%= url_for('ws')->to_abs %>');
  ws.onmessage = function(event) {
    document.getElementById('msg').innerHTML += event.data + '<br>';
  };
  ws.onclose = function(event) {
    document.getElementById('msg').innerHTML += 'close' + '<br>';
  };
</script>


Whats wrong i do? Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to