Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created November 16, 2018 14:45
Show Gist options
  • Save Srushtika/1972f98465e18b98b8124ce5bf25932e to your computer and use it in GitHub Desktop.
Save Srushtika/1972f98465e18b98b8124ce5bf25932e to your computer and use it in GitHub Desktop.
WebSockets server tutorial
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.');
}
});
function constructReply(data) {
// TODO: Construct a WebSocket frame Node.js socket buffer
}
function parseMessage(buffer) {
// TODO: Parse the WebSocket frame from the Node.js socket buffer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment