-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (39 loc) · 1.12 KB
/
Copy pathindex.js
File metadata and controls
48 lines (39 loc) · 1.12 KB
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
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const uuidV1 = require('uuid/v1');
const state = {
users: {},
chats: {}
};
const createUserName = () => 'User' + Math.floor(Math.random() * 100000);
io.on('connection', (socket) => {
const username = createUserName();
console.log(`${username} connected with ID ${socket.id}.`);
logState();
state.users[socket.id] = username;
io.emit('state', state);
socket.on('send chat', chat => {
if (typeof state.chats[chat.storyId] === 'undefined') {
state.chats[chat.storyId] = [];
}
chat.userId = socket.id;
chat.uuid = uuidV1();
state.chats[chat.storyId].push(chat);
io.emit('state', state);
logState();
});
socket.on('select username', username => {
state.users[socket.id] = username;
io.emit('state', state);
});
socket.on('disconnect', () => {
});
});
http.listen(4000, function () {
console.log('listening on *:4000');
});
const logState = () => {
return;
console.dir(state);
};