| 1234567891011121314151617181920212223242526 |
- // Quick 'n dirty NodeJS websocket server
- const PORT = 8000;
- const WebsocketServer = require('ws').Server;
- const ws = new WebsocketServer({
- port: PORT,
- })
- ws.on('connection', function(socket) {
- console.log("incoming connection")
- socket.on('message', function(msg) {
- // echo
- const parsed = JSON.parse(msg);
- if (parsed.type === 'ping') {
- console.log('pong')
- socket.send(JSON.stringify({type:'pong'}));
- } else {
- console.log("echoing", parsed);
- socket.send(JSON.stringify(parsed));
- }
- })
- })
- console.log("listening on port", PORT);
|