This gist shows a basic implementation of a HTTP server that handles the request to upgrade connection to Websockets.
Last active
March 24, 2021 05:00
-
-
Save chainhead/0570a95f47032ca0b55295aa8b0d5544 to your computer and use it in GitHub Desktop.
HTTP server handling connection upgrade
This file contains 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 http = require('http'); | |
const WebSocket = require('ws'); | |
const server = http.createServer(); | |
const wss = new WebSocket.Server({ noServer: true }); | |
server.on('upgrade', (request, socket, head) => { | |
wss.handleUpgrade(request, socket, head, function done(w) { | |
wss.emit('connection', ws, request) | |
}) | |
}) | |
server.listen(8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment