This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ws = new WebSocket('ws://example.org'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ws.addEventListener('open', () => { | |
// Send a message to the WebSocket server | |
ws.send('Hello!'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ws.addEventListener('message', event => { | |
// The `event` object is a typical DOM event object, and the message data sent | |
// by the server is stored in the `data` property | |
console.log('Received:', event.data); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Establish a WebSocket connection to the echo server | |
const ws = new WebSocket('wss://echo.websocket.org'); | |
// Add a listener that will be triggered when the WebSocket is ready to use | |
ws.addEventListener('open', () => { | |
const message = 'Hello!'; | |
console.log('Sending:', message); | |
// Send the message to the WebSocket server | |
ws.send(message); | |
}); | |
// Add a listener in order to process WebSocket messages received from the server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ws = new WebSocket('ws://localhost:3210', ['json', 'xml']); | |
ws.addEventListener('open', () => { | |
const data = { message: 'Hello from the client!' } | |
const json = JSON.stringify(data); | |
ws.send(json); | |
}); | |
ws.addEventListener('message', event => { | |
const data = JSON.parse(event.data); | |
console.log(data); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
socket.on('data', buffer => { | |
const message = parseMessage(buffer); | |
if (message) { | |
// For our convenience, so we can see what the client sent | |
console.log(message); | |
// We'll just send a hardcoded message in this example | |
socket.write(constructReply({ message: 'Hello from the server!' })); | |
} else if (message === null) { | |
console.log('WebSocket connection closed by the client.'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseMessage (buffer) { | |
const firstByte = buffer.readUInt8(0); | |
const isFinalFrame = Boolean((firstByte >>> 7) & 0×1); | |
const [reserved1, reserved2, reserved3] = [ Boolean((firstByte >>> 6) & 0×1), Boolean((firstByte >>> 5) & 0×1), Boolean((firstByte >>> 4) & 0×1) ]; | |
const opCode = firstByte & 0xF; | |
// We can return null to signify that this is a connection termination frame | |
if (opCode === 0×8) | |
return null; | |
// We only care about text frames from this point onward | |
if (opCode !== 0×1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maskingKey; | |
if (isMasked) { | |
maskingKey = buffer.readUInt32BE(currentOffset); | |
currentOffset += 4; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Allocate somewhere to store the final message data | |
const data = Buffer.alloc(payloadLength); | |
// Only unmask the data if the masking bit was set to 1 | |
if (isMasked) { | |
// Loop through the source buffer one byte at a time, keeping track of which | |
// byte in the masking key to use in the next XOR calculation | |
for (let i = 0, j = 0; i < payloadLength; ++i, j = i % 4) { | |
// Extract the correct byte mask from the masking key | |
const shift = j == 3 ? 0 : (3 - j) << 3; | |
const mask = (shift == 0 ? maskingKey : (maskingKey >>> shift)) & 0xFF; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseMessage (buffer) { | |
// …all of the above, then: | |
const json = data.toString('utf8'); return JSON.parse(json); | |
} |