-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
65 lines (49 loc) · 1.72 KB
/
app.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
var app = require('http').createServer(handler)
var io = require('socket.io')(app)
var url = require('url')
var fs = require('fs')
//This will open a server at localhost:5000. Navigate to this in your browser.
app.listen(5000);
// Http handler function
function handler (req, res) {
// Using URL to parse the requested URL
var path = url.parse(req.url).pathname;
// Managing the root route
if (path == '/') {
index = fs.readFile(__dirname+'/public/index.html',
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load index.html");
}
res.writeHead(200,{'Content-Type': 'text/html'});
res.end(data);
});
// Managing the route for the javascript files
} else if( /\.(js)$/.test(path) ) {
index = fs.readFile(__dirname+'/public'+path,
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load " + path);
}
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end(data);
});
} else {
res.writeHead(404);
res.end("Error: 404 - File not found.");
}
}
// Web Socket Connection
io.sockets.on('connection', function (socket) {
// If we recieved a command from a client to start watering lets do so
socket.on('example-ping', function(data) {
console.log("ping");
delay = data["duration"];
// Set a timer for when we should stop watering
setTimeout(function(){
socket.emit("example-pong");
}, delay*1000);
});
});