-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_server.js
93 lines (85 loc) · 3.46 KB
/
socket_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
const net = require('net');
// simple HTTP server using TCP sockets
var fs = require('fs');
var locations = {}; // current latitude, current longitude, destination latitude, destination longitude
function parsePOSTRequest(req) { // returns the request's body as a JSON
const bodyIndex = r.indexOf("\n\r");
const reqBody = r.substring(bodyIndex+3, r.length);
let keyIndex = 0;
let equalIndex = 0;
let ampersandIndex = 0;
let isDone = false;
let parsedRespJSON = {};
while(!isDone) {
equalIndex = reqBody.indexOf('=', equalIndex+1);
ampersandIndex = reqBody.indexOf('&', ampersandIndex+1);
let key = reqBody.substring(keyIndex, equalIndex);
let value;
if (ampersandIndex == -1) {
// take from last '=' character till end of string if last one, do not use ampersand index
value = reqBody.substring(equalIndex+1, req.length)
isDone = true; // exit the loop since we are done parsing
}
else {
key = reqBody.substring(keyIndex, equalIndex) // bet. prev ampersand and equal we have the key
keyIndex = ampersandIndex + 1;
value = reqBody.substring(equalIndex+1, ampersandIndex) // bet. equal and ampersand we have value
}
parsedRespJSON[key] = value;
}
return parsedRespJSON;
}
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
//console.log('Received: ' + data);
r = data.toString();
// console.log(r);
if(r.substring(0,12) == "GET /compass") {
console.log('OK');
socket.write("HTTP/1.1 200 OK\n");
fs.readFile('compass.html', 'utf8', function(err, contents) {
socket.write("Content-Length:"+contents.length);
socket.write("\n\n"); // two carriage returns
socket.write(contents);
})
}
else if(r.substring(0,14) == "GET /locations") {
console.log('sending locations');
socket.write("HTTP/1.1 200 OK\n");
let JSONContents = JSON.stringify(locations);
socket.write("Content-Length:"+JSONContents.length);
socket.write("\n\n"); // two carriage returns
socket.write(JSONContents);
console.log(JSONContents);
}
else if(r.substring(0,3) =="GET"){
console.log('OK');
socket.write("HTTP/1.1 200 OK\n");
fs.readFile('leaflet_test.html', 'utf8', function(err, contents) {
socket.write("Content-Length:"+contents.length);
socket.write("\n\n"); // two carriage returns
socket.write(contents);
})
}
else if(r.substring(0,4)=="POST") {
console.log('recieved POST');
locations = parsePOSTRequest(r); // parses body of the request to get the current and destination locations
console.log(locations);
socket.write('HTTP/1.1 200 OK\n');
socket.write("Content-Length:" + 0);
socket.write("\n\n");
}
});
socket.on('close', function() {
console.log('Connection closed');
});
socket.on('end', function() {
console.log('client disconnected');
});
socket.on('error', function() {
console.log('client disconnected');
});
});
server.listen(8080, function() {
console.log('server is listening on port 8080');
});