/
chat.pl6
105 lines (97 loc) · 3 KB
/
chat.pl6
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use v6;
use HTTP::Server::Tiny;
use WebSocket::P6W;
sub MAIN(Int :$port=80) {
my $html = $=finish;
$html ~~ s:g/'<<<PORT>>>'/$port/;
my $supplier = Supplier.new;
my $supply = $supplier.Supply;
my $s = HTTP::Server::Tiny.new(port => $port);
$s.run(-> %env {
say "request: %env<PATH_INFO>";
given %env<PATH_INFO> {
when '/' {
200, [], [$html]
}
when '/echo2' {
my $s;
ws-psgi(%env,
on-ready => -> $ws {
$s = $supply.tap(-> $got {
$ws.send-text("GOT: $got");
});
say 'ready';
},
on-text => -> $ws, $txt {
$supplier.emit($txt);
say 'sent.';
if $txt eq 'quit' {
say 'close it!';
$ws.send-close();
}
},
on-binary => -> $ws, $binary {
$ws.send-binary($binary);
},
on-close => -> $ws {
say "closing socket";
$s.close if $s;
},
);
}
default {
404, [], ['not found']
}
}
});
}
=finish
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WS</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<header><h1>WS</h1></header>
<section class="row">
<form id="form">
<input type="text" name="message" id="message">
<input type="submit">
</form>
<pre id="log"></pre>
</section>
<footer>Powered by <a href="http://perl6.org/">Perl6</a></footer>
</div>
<script type="text/javascript">
function log(msg) {
$('#log').text($('#log').text() + msg + "\n");
}
$(function () {
var ws = new WebSocket('ws://localhost:<<<PORT>>>/echo2');
ws.onopen = function () {
log('connected');
};
ws.onclose = function (ev) {
log('closed');
};
ws.onmessage = function (ev) {
log('received: ' + ev.data);
$('#message').val('');
};
ws.onerror = function (ev) {
console.log(ev);
log('error: ' + ev.data);
};
$('#form').submit(function () {
ws.send($('#message').val());
return false;
});
});
</script>
</body>
</html>