-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (23 loc) · 941 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const express = require('express');
// Our HTTP server, serves as a 'hook' for the websocket server
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}'))
// Our WebSocket server, takes an HTTP server as an argument
const { Server } = require('ws');
const wss = new Server({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
// user sent a message
ws.on('message', function(message) {
//if (message.type === 'utf8') { // make sure the messages the server receives is a string
wss.clients.forEach((client) => {
client.send(message); // we are simply the messenger
});
//}
});
// user disconnected
ws.on('close', () => console.log('Client disconnected'));
});