-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (62 loc) · 2.05 KB
/
server.js
File metadata and controls
72 lines (62 loc) · 2.05 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const socketio = require('socket.io');
const static = require('serve-static');
const bodyParser = require('body-parser');
const app = express();
app.use(static(__dirname + '/dist'));
app.use(bodyParser.json());
/* Setup CORS for localhost (dev environment) */
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
const io = socketio(server);
io.on('connection', (socket) => {
console.log(`[CONN] Socket connection established (${socket.id})`);
socket.broadcast.emit('arrival');
socket.on('disconnect', () => {
console.log(`[DISC] Socket disconnection (${socket.id})`);
});
socket.on('msg', (data) => {
console.log(`[MSG] Sender: ${data.sender} (${socket.id}) Content: ${data.content}`);
let emitData = {
type: data.type,
id: data.id,
sender: data.sender ? data.sender : `user(${socket.id.slice(0,4)})`,
content: data.content
};
socket.broadcast.emit('msg', emitData)
});
socket.on('nameChange', (data) => {
console.log(`[NAME] ${data.old} changed their name to ${data.new}`);
let emitData = {
old: data.old ? data.old : `user(${socket.id.slice(0,4)})`,
new: data.new
};
io.sockets.emit('nameChange', emitData);
});
socket.on('greet', (data) => {
io.sockets.emit('greet', {
sender: data.sender ? data.sender : `user(${socket.id.slice(0,4)})`
});
});
socket.on('vote', (data) => {
console.log(`[VOTE] User ${data.isUpvote ? 'upvoted' : 'downvoted'} msg with id ${data.id}`)
io.sockets.emit('vote', {
id: data.id,
isUpvote: data.isUpvote
});
});
});
app.get('/api/usercount', (req, res) => {
let size = Object.keys(io.sockets.connected).length;
res.status(200).send({count: size});
});
app.get('*', (req, res) => {
res.redirect('/');
});