]>
Commit | Line | Data |
---|---|---|
1 | const WebSocketServer = require('ws').WebSocketServer | |
2 | ||
3 | async function register ({ | |
4 | registerWebSocketRoute | |
5 | }) { | |
6 | const wss = new WebSocketServer({ noServer: true }) | |
7 | ||
8 | wss.on('connection', function connection(ws) { | |
9 | ws.on('message', function message(data) { | |
10 | if (data.toString() === 'ping') { | |
11 | ws.send('pong') | |
12 | } | |
13 | }) | |
14 | }) | |
15 | ||
16 | registerWebSocketRoute({ | |
17 | route: '/toto', | |
18 | ||
19 | handler: (request, socket, head) => { | |
20 | wss.handleUpgrade(request, socket, head, ws => { | |
21 | wss.emit('connection', ws, request) | |
22 | }) | |
23 | } | |
24 | }) | |
25 | } | |
26 | ||
27 | async function unregister () { | |
28 | return | |
29 | } | |
30 | ||
31 | module.exports = { | |
32 | register, | |
33 | unregister | |
34 | } | |
35 | ||
36 | // ########################################################################### |