-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-canvas.pl
executable file
·42 lines (36 loc) · 1.03 KB
/
test-canvas.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl
use Mojolicious::Lite;
use Data::Dump 'pp';
app->secret('1Y8+yLpkVo+VIRm/SSEsl0seCa840gOs');
my %clients = ();
get '/' => sub {
my $self = shift;
$self->render_static('test-canvas.html');
} => 'index';
websocket '/d' => sub {
my $self = shift;
my $id = sprintf("%s:%s", $self->tx->remote_address, $self->tx->remote_port);
app->log->debug("got client $id");
$clients{$id} = $self->tx;
my $json = new Mojo::JSON;
$self->on(message => sub {
my ($self, $data) = @_;
if ($data eq 'ping') {
$self->send('pong');
return;
}
app->log->debug("in: $data");
$data = $json->decode($data);
for my $cl (keys %clients) {
next if $cl eq $id;
app->log->debug("sending to $cl");
$clients{$cl}->send($json->encode($data));
}
});
$self->on(finish => sub {
app->log->debug("disconnect: $id");
delete $clients{$id};
});
};
$ENV{MOJO_INACTIVITY_TIMEOUT} = 0;
app->start;