Last active
March 22, 2019 22:29
-
-
Save evanshortiss/83e63dc9cab83c7e7aba6780da3704bd to your computer and use it in GitHub Desktop.
A quick web socket server setup in Node.js
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
/** | |
* Quick setup: | |
* | |
* $ mkdir /tmp/wss | |
* $ cd /tmp/wss | |
* $ npm init -f | |
* $ curl https://gist.githubusercontent.com/evanshortiss/83e63dc9cab83c7e7aba6780da3704bd/raw/ae5d532a371ef383d896f1663a454102cc1931c3/wss.js > wss.js | |
* $ node wss.js | |
*/ | |
const WebSocket = require('ws'); | |
const host = '0.0.0.0' | |
const port = 8080 | |
const wss = new WebSocket.Server({ | |
host, | |
port | |
}); | |
wss.on('connection', function connection(ws) { | |
console.log('a client connected') | |
ws.on('message', function incoming(message) { | |
console.log('received payload: %s', message); | |
}); | |
}); | |
wss.on('listening', function listening() { | |
console.log(`started listening on ${host}:${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment