Last active
March 30, 2020 19:10
-
-
Save jamigibbs/a0a5a8488ac7e07f203e6870014a7ab0 to your computer and use it in GitHub Desktop.
Node Websocket Server Base
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 socketIO = require('socket.io'); | |
const PORT = process.env.PORT || 3000; | |
const INDEX = '/index.html'; | |
const server = express() | |
.use((req, res) => res.sendFile(INDEX, { root: __dirname })) | |
.listen(PORT, () => console.log(`Listening on ${PORT}`)); | |
const io = socketIO(server); | |
io.on('connection', (socket) => { | |
console.log('Client connected'); | |
socket.on('disconnect', () => console.log('Client disconnected')); | |
}); | |
setInterval(() => io.emit('time', new Date().toTimeString()), 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment