-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (60 loc) · 1.47 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
var http = require('http'),
url = require('url'),
fs = require('fs'),
qs = require('querystring'),
io = require('socket.io');
var stat;
var server = http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
switch(path) {
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Index');
response.end();
break;
case '/client':
fs.readFile('./client.html', function (error, data) {
response.write(data);
if(request.method == 'POST') {
var formData = '';
request.on('data', function (data) {
formData += data;
});
request.on('end', function () {
var post = qs.parse(formData);
for(key in post) {
var command = post[key];
if(command.match('run')) {
stat = 'run';
} else if(command.match('up')) {
stat = 'up';
}
else if(command.match('right')) {
stat = 'right';
}
else if(command.match('left')) {
stat = 'left';
}
else if(command.match('down')) {
stat = 'down';
} else {
stat = 'Error: command not found';
}
}
});
}
response.end();
});
break;
default:
response.writeHead(404);
response.write("404 - Page not found.");
response.end();
break;
}
});
server.listen(8080);
var serv_io = io.listen(server);
serv_io.sockets.on('connection', function (socket) {
socket.emit('updateStatus', {'stat': stat});
});