-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
34 lines (27 loc) · 917 Bytes
/
app.js
File metadata and controls
34 lines (27 loc) · 917 Bytes
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
30
31
32
33
34
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const SOCKET_LIST = {};
app.get('/', (req, res) => {
// forward the root over to client/index.html
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
io.sockets.on('connection', socket => {
console.log('New user!');
SOCKET_LIST[socket.id] = socket;
// when a message is sent, send that message to everyone else
socket.on('sendMsgToServer', data => {
console.log('Someone sent a message!');
for (var i = SOCKET_LIST.length - 1; i >= 0; i--) {
SOCKET_LIST[i].emit('addToChat', data);
}
});
// when disconnected, remove the reference
socket.on('disconnect', () => {
delete SOCKET_LIST[socket.id];
});
});
server.listen(4141);
console.log('Server started at http://localhost:4141/');