Created
November 16, 2018 14:36
-
-
Save Srushtika/0789878ac91f22c719d5705b4c079338 to your computer and use it in GitHub Desktop.
WebSockets server tutorial
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
// 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