Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created November 16, 2018 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Srushtika/0789878ac91f22c719d5705b4c079338 to your computer and use it in GitHub Desktop.
Save Srushtika/0789878ac91f22c719d5705b4c079338 to your computer and use it in GitHub Desktop.
WebSockets server tutorial
// Read the subprotocol from the client request headers:
const protocol = req.headers['sec-websocket-protocol'];
// If provided, they'll be formatted as a comma-delimited string of protocol
// names that the client supports; we'll need to parse the header value, if
// provided, and see what options the client is offering:
const protocols = !protocol ? [] : protocol.split(',').map(s => s.trim());
// To keep it simple, we'll just see if JSON was an option, and if so, include
// it in the HTTP response:
if (protocols.includes('json')) {
// Tell the client that we agree to communicate with JSON data
responseHeaders.push(`Sec-WebSocket-Protocol: json`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment