(conf)
| 42 | } |
| 43 | |
| 44 | function main(conf) { |
| 45 | const frame = createFrame(Buffer.alloc(conf.size).fill('.'), conf.useBinary === 'true' ? 2 : 1); |
| 46 | const server = http.createServer(); |
| 47 | server.on('upgrade', (req, socket) => { |
| 48 | const key = crypto |
| 49 | .createHash('sha1') |
| 50 | .update(req.headers['sec-websocket-key'] + GUID) |
| 51 | .digest('base64'); |
| 52 | |
| 53 | let bytesReceived = 0; |
| 54 | let roundtrip = 0; |
| 55 | |
| 56 | socket.on('data', function onData(chunk) { |
| 57 | bytesReceived += chunk.length; |
| 58 | |
| 59 | if (bytesReceived === frame.length + 4) { // +4 for the mask. |
| 60 | // Message completely received. |
| 61 | bytesReceived = 0; |
| 62 | |
| 63 | if (++roundtrip === conf.roundtrips) { |
| 64 | socket.removeListener('data', onData); |
| 65 | socket.resume(); |
| 66 | socket.end(); |
| 67 | server.close(); |
| 68 | |
| 69 | bench.end(conf.roundtrips); |
| 70 | } else { |
| 71 | socket.write(frame); |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | socket.write( |
| 77 | [ |
| 78 | 'HTTP/1.1 101 Switching Protocols', |
| 79 | 'Upgrade: websocket', |
| 80 | 'Connection: Upgrade', |
| 81 | `Sec-WebSocket-Accept: ${key}`, |
| 82 | '\r\n', |
| 83 | ].join('\r\n'), |
| 84 | ); |
| 85 | |
| 86 | socket.write(frame); |
| 87 | }); |
| 88 | |
| 89 | server.listen(0, () => { |
| 90 | const ws = new WebSocket(`ws://localhost:${server.address().port}`); |
| 91 | ws.addEventListener('open', () => { |
| 92 | bench.start(); |
| 93 | }); |
| 94 | |
| 95 | ws.addEventListener('message', (event) => { |
| 96 | ws.send(event.data); |
| 97 | }); |
| 98 | }); |
| 99 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…