(onMessage)
| 28 | // plumbing and per-connection message parsing, so each server only has to supply how |
| 29 | // it replies to a parsed `{id, method, params}` message. |
| 30 | function startWsServer(onMessage) { |
| 31 | return new Promise((resolve) => { |
| 32 | const server = new WebSocketServer({ port: 0 }, () => { |
| 33 | const { port } = server.address() |
| 34 | resolve({ |
| 35 | server, |
| 36 | url: `ws://127.0.0.1:${port}`, |
| 37 | emitEvent(method, params) { |
| 38 | for (const client of server.clients) { |
| 39 | client.send(JSON.stringify({ method, params })) |
| 40 | } |
| 41 | }, |
| 42 | }) |
| 43 | }) |
| 44 | server.on('connection', (ws) => { |
| 45 | ws.on('message', (data) => onMessage(JSON.parse(data.toString()), ws)) |
| 46 | }) |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | function startEchoServer() { |
| 51 | return startWsServer(({ id }, ws) => ws.send(JSON.stringify({ id, result: {} }))) |
no test coverage detected