-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (48 loc) · 1.44 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
var http = require('http');//Include http server.
var url = require('url');//Include url parse library.
var fs = require('fs');//Include file system library.
var socketio = require('socket.io');//Include socket.io library.
//Create http server
var server = http.createServer(function (request, response) {
console.log('Server running');//Show message on terminal
//Filter url path.
var path = url.parse(request.url).pathname + 'client.html';
//Set root path to return ex01-client.html.
if (path === '/client.html') {
fs.readFile(__dirname + path, function(error, data) {
if (error) {
response.writeHead(404);
response.write("opps this doesn't exist - 404");
response.end();
} else {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(data, "utf8");
response.end();
} //fi
});
} else {
response.writeHead(404, {
'Content-Type': 'text/html'
});
response.write('opps this doesnt exist - 404');
response.end();
}//fi
});//End of server configure.
server.listen(8000); //Listen port from browser.
var io = socketio.listen(server);//Binding socket and server.
io.sockets.on('connection', function(socket) {
//Post message to client(Wait 10 sec.)
setInterval(function() {
socket.emit('date', {
'date': new Date()
});
}, 10000);
//Receive message from client.
socket.on('clientData', function(data) {
for (index in data) {
process.stdout.write(data[index]);
}//endfor
});
});