-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (46 loc) · 1.31 KB
/
server.js
File metadata and controls
47 lines (46 loc) · 1.31 KB
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
var http = require('http'),
url = require('url'),
fs = require('fs');
var messages = ["testing"];
var clients = [];
http.createServer(function (req, res) {
// parse URL
var url_parts = url.parse(req.url);
console.log(url_parts);
if(url_parts.pathname == '/') {
// file serving
fs.readFile('./index.html', function(err, data) {
res.end(data);
});
} else if(url_parts.pathname.substr(0, 5) == '/poll') {
// polling code here
var count = url_parts.pathname.replace(/[^0-9]*/, '');
console.log(count);
console.log(messages.length);
console.log(messages);
if(messages.length > count) {
res.end(JSON.stringify( {
count: messages.length,
appendd: messages.slice(count).join("\n")+"\n"
}));
} else {
console.log(count);
console.log(messages.length);
clients.push(res);
}
}
else if(url_parts.pathname.substr(0, 5) == '/msg/') {
// message receiving
var msg = unescape(url_parts.pathname.substr(5));
messages.push(msg);
while(clients.length > 0) {
var client = clients.pop();
client.end(JSON.stringify( {
count: messages.length,
appendd: msg+"\n"
}));
}
res.end();
}
}).listen(8080, 'localhost');
console.log('Server running.');