Created
September 15, 2019 14:51
-
-
Save isaacgr/d0aa3c712130bb38b8d121675c283c99 to your computer and use it in GitHub Desktop.
Websocket configuration with express and Herkou
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
'use strict'; | |
const express = require('express'); | |
const SocketServer = require('ws').Server; | |
const path = require('path'); | |
const PORT = process.env.PORT || 3000; | |
const INDEX = path.join(__dirname, 'index.html'); | |
const server = express() | |
.use((req, res) => res.sendFile(INDEX) ) | |
.listen(PORT, () => console.log(`Listening on ${ PORT }`)); | |
const wss = new SocketServer({ server }); | |
wss.on('connection', (ws) => { | |
console.log('Client connected'); | |
ws.on('close', () => console.log('Client disconnected')); | |
}); | |
setInterval(() => { | |
wss.clients.forEach((client) => { | |
client.send(new Date().toTimeString()); | |
}); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment