MCPcopy Index your code
hub / github.com/nodejs/node / main

Function main

benchmark/websocket/simple.js:44–99  ·  view source on GitHub ↗
(conf)

Source from the content-addressed store, hash-verified

42}
43
44function 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}

Callers

nothing calls this directly

Calls 15

addEventListenerMethod · 0.95
sendMethod · 0.95
fillMethod · 0.80
allocMethod · 0.80
listenMethod · 0.80
createFrameFunction · 0.70
updateMethod · 0.65
resumeMethod · 0.65
closeMethod · 0.65
addressMethod · 0.65
onMethod · 0.45
removeListenerMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…