-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
57 lines (37 loc) · 1.19 KB
/
main.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const config = require('./config.json');
const roomNames = config.ROOMS;
const messages = {};
for(let i in roomNames)
messages[roomNames[i]] = ["🐙 Connected to "+roomNames[i]+""];
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('a user connected');
socket.emit('roomUpdate', roomNames);
socket.emit('messages', messages);
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('message', (msg) => {
if((roomNames.indexOf(msg.room) === -1))
return;
if(!(msg.room in messages))
messages[msg.room] = [];
messages[msg.room].push(msg.message);
cleanMessages();
io.emit('message', msg);
});
});
function cleanMessages(){
// for(let room in messages)
// while(messages[room].length > 8)
// messages[room] = messages[room].shift(1);
}
server.listen(config.PORT, () => {
console.log('listening on *:'+config.PORT);
});