-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server
executable file
·65 lines (49 loc) · 1.75 KB
/
test_server
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env perl
use warnings;
use strict;
use Mojolicious::Lite;
use Mojo::IOLoop;
use JSON::XS;
my $json = JSON::XS->new->utf8;
get '/' => 'index';
websocket '/plain_demo' => sub {
my $self = shift;
$self->app->log->debug("WebSocket opened.");
$self->send('Welcome to the /plain_demo');
#Mojo::IOLoop->stream($self->tx->connection)->timeout(300);
$self->on(message => sub {
my ($self, $msg) = @_;
$self->app->log->debug("Received message: ".$msg);
$self->send("echo: $msg");
});
my $id = Mojo::IOLoop->recurring(30 => sub {
$self->app->log->debug("Sending periodical message");
$self->send(sprintf "Periodical message from server [timestamp: %d]", time());
});
$self->on(finish => sub {
my ($self, $code, $reason) = @_;
$self->app->log->debug("WebSocket closed with status $code.");
Mojo::IOLoop->remove($id);
});
};
websocket '/json_demo' => sub {
my $self = shift;
$self->app->log->debug("WebSocket opened.");
$self->send($json->encode({msg => 'Welcome to the /json_demo'}));
#Mojo::IOLoop->stream($self->tx->connection)->timeout(300);
$self->on(message => sub {
my ($self, $msg) = @_;
$self->app->log->debug("Received message: ".$msg);
$self->send($json->encode({msg => "echo: $msg"}));
});
my $id = Mojo::IOLoop->recurring(30 => sub {
$self->app->log->debug("Sending periodical message");
$self->send($json->encode({msg => sprintf "Periodical message from server [timestamp: %d]", time()}));
});
$self->on(finish => sub {
my ($self, $code, $reason) = @_;
$self->app->log->debug("WebSocket closed with status $code.");
Mojo::IOLoop->remove($id);
});
};
app->start;