-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
127 lines (103 loc) · 3.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var path = require('path');
var express = require('express');
var app = express();
var PORT = process.env.PORT || 8080;
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var Timer = require('./lib/timer.js');
var uuidv1 = require('uuid/v1');
// using webpack-dev-server and middleware in development environment
if (process.env.NODE_ENV !== 'production') {
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var webpack = require('webpack');
var config = require('./webpack.config');
var compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
app.use(webpackHotMiddleware(compiler));
}
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/', function(request, response) {
response.sendFile(__dirname + '/dist/index.html')
});
server.listen(PORT, function(error) {
if (error) {
console.error(error);
} else {
console.info('==> 🌎 Listening on port %s. Visit http://localhost:%s/ in your browser.', PORT, PORT);
}
});
// timer initiation
const t = new Timer([25, 0], [5, 0]);
t.startTimer();
t.on('tick', (type, str) => {
console.log('tick', type, str)
io.emit('timerUpdate', type, str);
})
// socket code
var userCount = 0;
var userColor = 0;
io.on('connection', (client) => {
// Test code
// client.on('test', (data) => {
// console.log('from client:', data)
// client.emit('test', 'server got your message')
// })
////
userCount += 1;
console.log('client connected! Client count', userCount.toString())
client.emit('colorAssign', userColor.toString())
console.log('assigning color:', userColor)
userColor = userColor === 3 ? 0 : userColor + 1;
const newUser ={
id: uuidv1(),
type: 'userCountChange',
content: 'A new user has connected',
userCount: userCount,
timestamp: Date.now()
}
io.emit('newNotification', JSON.stringify(newUser))
io.emit('userCountChange', userCount.toString())
client.emit('timer', 'welcome!');
client.on('disconnect', function() {
userCount -= 1;
console.log('client disconneted!');
io.emit('userCountChange', userCount.toString())
const departingUser ={
id: uuidv1(),
type: 'userCountChange',
content: 'A user has disconnected',
userCount: userCount,
timestamp: Date.now()
}
io.emit('newNotification', JSON.stringify(departingUser));
});
// timer events
client.on('requestStatus', function(){
console.log('status sent')
client.emit('timerStatus', t.timerInfo)
})
client.on('modifyTimer', function(command, newStudyTime, newBreakTime) {
console.log('command from client:', command);
t.accessTimer(command, newStudyTime, newBreakTime);
client.emit('timerStatus', t.timerInfo)
});
// chat events
client.on('messageSubmit', function(message){
const submittedMesage = JSON.parse(message);
submittedMesage.id = uuidv1();
submittedMesage.timestamp = Date.now();
io.emit('newMessage', JSON.stringify(submittedMesage));
})
client.on('nameChange', function(oldUsername, newUsername){
console.log('name chaange received')
console.log(oldUsername, newUsername)
const nameChangeUpdate = {
id: uuidv1(),
type: 'nameChange',
content: `${oldUsername} has changed their name ${newUsername}`,
timestamp: Date.now()
}
io.emit('newNotification', JSON.stringify(nameChangeUpdate));
})
});