-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (85 loc) · 3.37 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
var express = require('express');
var port = 9487;
var path = require('path');
var app= express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var filename;
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
function finit() {
var t = new Date().Format("yyyyMMdd-hhmm");
filename = 'log/' + t;
fs.writeFile(filename, "created @ " + t + '\n', function(err){
if (err) {
console.error(err)
}
});
}
finit();
function fwrite(data) {
fs.appendFile( filename , data + '\n', function(err){
if (err) { console.error(err) }
});
}
var user_num=0;
app.use(express.static(path.join(__dirname, '')));
app.get('/1' , function(req, res){ res.sendFile(__dirname + '/clients/client1.html'); });
app.get('/2' , function(req, res){ res.sendFile(__dirname + '/clients/client2.html'); });
app.get('/3' , function(req, res){ res.sendFile(__dirname + '/clients/client3.html'); });
app.get('/4' , function(req, res){ res.sendFile(__dirname + '/clients/client4.html'); });
app.get('/5' , function(req, res){ res.sendFile(__dirname + '/clients/client5.html'); });
app.get('/6' , function(req, res){ res.sendFile(__dirname + '/clients/client6.html'); });
app.get('/7' , function(req, res){ res.sendFile(__dirname + '/clients/client7.html'); });
app.get('/8' , function(req, res){ res.sendFile(__dirname + '/clients/client8.html'); });
app.get('/9' , function(req, res){ res.sendFile(__dirname + '/clients/client9.html'); });
app.get('/server.html', function(req, res){ res.sendFile(__dirname + '/server.html');});
io.on('connection', function(socket){
console.log('user '+ user_num + ' connected');
var user = user_num++;
socket.on('disconnect', function(){
console.log('user '+user+' disconnectedQQ');
});
socket.on('start_s', function(dest, list){
console.log('user '+user+' press start !');
var t = new Date().Format("hh:mm:ss:S");
console.log( t+' list fade in @client' + dest);
fwrite( '\n' +t+' list fade in @client' + dest );
io.emit('start_c', dest, list);
});
socket.on('click_s', function(bool){
console.log('user '+user+' click '+bool+'!');
fwrite( bool );
io.emit('click_c');
});
socket.on('init', function(){
console.log('initialize cnt');
});
socket.on('fade_word', function(i) {
var t = new Date().Format("hh:mm:ss:S");
console.log( t+' word #'+i+' fade out.');
fwrite( t+' word #'+i+' fade out.' );
});
socket.on('distract', function(i) {
var t = new Date().Format("hh:mm:ss:S");
console.log( 'Distract QQ' );
fwrite( t+' Distract' );
});
});
http.listen(port, function(){
console.log('listening on *:'+port);
});