-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
77 lines (56 loc) · 1.5 KB
/
server.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Support Chat
*/
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const path = require('path');
const util = require('util');
// Vars
const port = process.env.PORT || 8080;
// Express
const express = require('express'); // FIXME
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.resolve('public/index.html'));
});
app.get('/test', (req, res) => {
res.send('Hello World!')
})
// Websockets
http.listen(port, () => {
console.log(util.format('Socket.IO server running at http://localhost:%s', port));
});
// Main
let users = [];
io.on('connection', (socket) => {
users.push(socket.id);
console.log(util.format('+ User "%s" connected. Total users: %s', socket.id, users.length));
socket.emit('nick', {
nick: socket.id,
});
io.emit('users', { users });
socket.on('chat-everyone', (data) => {
io.emit('chat-everyone', {
from: socket.id,
msg: data.msg,
});
});
socket.on('chat-private', (data) => {
const payload = {
from: socket.id,
to: data.to,
msg: data.msg
};
io.to(data.to).emit('chat-private', payload);
socket.emit('chat-private', payload);
});
socket.on('disconnect', () => {
const index = users.indexOf(socket.id);
if (index > -1) {
users.splice(index, 1);
}
io.sockets.emit('users', { users });
console.log(util.format('- User "%s" disconnected. Total users: %s', socket.id, users.length));
});
});