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

Method handleUpgrade

test/common/websocket-server.js:33–69  ·  view source on GitHub ↗
(req, socket, head)

Source from the content-addressed store, hash-verified

31 }
32
33 handleUpgrade(req, socket, head) {
34 const key = req.headers['sec-websocket-key'];
35 const acceptKey = this.generateAcceptValue(key);
36 const responseHeaders = [
37 'HTTP/1.1 101 Switching Protocols',
38 'Upgrade: websocket',
39 'Connection: Upgrade',
40 `Sec-WebSocket-Accept: ${acceptKey}`,
41 ...this.customHandleUpgradeHeaders,
42 ];
43
44 socket.write(responseHeaders.join('\r\n') + '\r\n\r\n');
45 this.clients.add(socket);
46
47 socket.on('data', (buffer) => {
48 const opcode = buffer[0] & 0x0f;
49
50 if (opcode === 0x8) {
51 // Send a minimal close frame in response:
52 socket.write(Buffer.from([0x88, 0x00]));
53 socket.end();
54 this.clients.delete(socket);
55 return;
56 }
57
58 socket.write(this.encodeMessage('Hello from server!'));
59 });
60
61 socket.on('close', () => {
62 this.clients.delete(socket);
63 });
64
65 socket.on('error', (err) => {
66 console.error('Socket error:', err);
67 this.clients.delete(socket);
68 });
69 }
70
71 generateAcceptValue(secWebSocketKey) {
72 return crypto

Callers

nothing calls this directly

Calls 10

generateAcceptValueMethod · 0.95
encodeMessageMethod · 0.95
addMethod · 0.65
deleteMethod · 0.65
writeMethod · 0.45
joinMethod · 0.45
onMethod · 0.45
fromMethod · 0.45
endMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected