Created
April 19, 2023 20:35
-
-
Save TakesTheBiscuit/bba8839b24477523cc66c5a8d28f636c to your computer and use it in GitHub Desktop.
websocket.js using node and ws package
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
'use strict'; | |
import { WebSocketServer } from 'ws'; | |
const wss = new WebSocketServer({ port: 8080 }); | |
wss.on('connection', function connection(ws) { | |
ws.on('error', console.error); | |
ws.on('message', function message(data) { | |
console.log('received: %s', data); | |
}); | |
ws.send('something'); | |
setInterval(() => { | |
const dateNow = new Date(); | |
ws.send(`something @ ${dateNow.toISOString()}`); | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment