-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
116 lines (100 loc) · 3.15 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
var app = require('http').createServer(handler),
io = require('socket.io').listen(app), //require('socket.io').listen(80);
fs = require('fs');
app.listen(9900);
var seq =0;
var obj ={};
//preload all the question from questions_bodypart_trial_10.txt
// full - questions_bodypart_trial_10
// 24 - questions_bodypart_24
// 10 - questions_bodypart_10
var questionLists;
readTextFile("bodypart_10/questions_bodypart_10.txt",function (d){
var fixedResponse = d.split('\n');
//var jsonObj = JSON.parse(fixedResponse);
questionLists = fixedResponse;
});
console.log("listen to port 9900");
function handler(req, res) {
console.log('handler')
fs.readFile(__dirname + '/index.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// Manage connections
io.sockets.on('connection', function(socket) {
console.log('handle connection');
var periodInMilliseconds = 400;
var timeoutId = -1;
/**
* Handle "disconnect" events.
*/
var handleDisconnect = function() {
console.log('handle disconnect');
clearTimeout(timeoutId);
};
/**
* Generate a request to be sent to the client.
*/
function generateServerRequest(seq) {
// execute program with parameter [1:0]
// read the JSON
// read the question
console.log('generate server request');
//submit tree JSON from 0000.json - 0302.json
loadJSON(seq,function(response){
//sublmit an object
socket.emit('server request', {
date: new Date(),
value: response
});
});
//timeoutId = setTimeout(generateServerRequest, periodInMilliseconds);
}
//read JSON function
function loadJSON(seq, callback) {
var tem_seq;
if(seq<10)
tem_seq = "000"+seq+".json";
else if(seq<100)
tem_seq = "00"+seq+".json";
else
tem_seq = "0"+seq+".json";
fs.readFile('bodypart_10/body_part_json_10/'+tem_seq, 'utf8', function (err, data) {
if (err) throw err;
obj.tree = JSON.parse(data);//read the jason tree
obj.question = questionLists[seq].split(",");//read the question
callback(obj);
});
}
socket.on('disconnect', handleDisconnect);
socket.on("answer_button_clicked", function(data){
console.log(data);
io.sockets.emit("update_after_answer",{value: data+"GG"});
generateServerRequest(seq);
if(seq<301)seq++;
});
socket.on("restart", function(data){
generateServerRequest(0);
seq=0;
});
//init
generateServerRequest(0);
seq++;
// setInterval(function(){
// generateServerRequest(seq);
// if(seq<301)seq++;},
// periodInMilliseconds);
});
function readTextFile(file, callback)
{
fs.readFile(file, function(error, data) {
callback(data.toString());
});
}