-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_echo.praia
More file actions
45 lines (39 loc) · 1.35 KB
/
websocket_echo.praia
File metadata and controls
45 lines (39 loc) · 1.35 KB
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
#!/usr/bin/env praia
use "socket"
let server = http.createServer(lam{ req in
if (req.path == "/ws") {
return socket.upgrade(req, lam{ ws in
print("[%{ws.id}] connected")
ws.onMessage(lam{ msg in
print("[%{ws.id}] received: %{msg}")
ws.send("echo: %{msg}")
})
ws.onClose(lam{ in
print("[%{ws.id}] disconnected")
})
ws.send("Welcome to the echo server!")
})
}
// Serve a simple test page
return http.html("<!DOCTYPE html><html><body>
<h2>WebSocket Echo Test</h2>
<input id='msg' placeholder='Type a message...' />
<button onclick='send()'>Send</button>
<pre id='log'></pre>
<script>
const ws = new WebSocket('ws://' + location.host + '/ws');
const log = document.getElementById('log');
ws.onopen = () => log.textContent += 'Connected\\n';
ws.onmessage = e => log.textContent += '< ' + e.data + '\\n';
ws.onclose = () => log.textContent += 'Disconnected\\n';
function send() {
const msg = document.getElementById('msg').value;
ws.send(msg);
log.textContent += '> ' + msg + '\\n';
document.getElementById('msg').value = '';
}
document.getElementById('msg').onkeydown = e => { if (e.key === 'Enter') send(); };
</script></body></html>")
})
print("WebSocket echo server on http://localhost:9090")
server.listen(9090)