-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8.server.js
92 lines (74 loc) · 2.5 KB
/
8.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
//javscript was chosen for nodehigh
// because it is throughput low latency event model processing
// library - maps on papers. just a tool on the way
// framework - gps system - pretype location - it gives us the direction, opinion - what you need on the way
// platform - automatic driving google car - strong opinion where it is going, you have to say very little, you can just go with the flow
//https://stackoverflow.com/questions/9914816/what-is-an-example-of-the-simplest-possible-socket-io-example
function handleHTTP(req, res){
if(req.method === "GET"){
// if(req.url === "/"){
// res.writeHead(200,{"Content-type":"text/plain"});
//
// ASQ(function(done){
// setTimeout(function(){
// done(Math.random());
// },1000);
// })
// .then(function(done, num){
// setTimeout(function(){
// done("Hello World: " + num);
// },1000);
// })
// .val(function(msg){
// res.end(msg);
// });
// }
if(/^\/\d+(?=$|[\/?#])/.test(req.url)){
req.addListener("end", function(){
req.url = req.url.replace(/^\/(\d+).*$/,"/$1.html");
static_files.serve(req,res);
});
req.resume();
}
else{
res.writeHead(403); //Forbidden
res.end("Get outta here!");
}
}else{
res.writeHead(403); //Forbidden
res.end("Get outta here!");
}
}
function handleIO(socket){
function disconnect(){
clearInterval(intv);
console.log("client disconnected");
}
console.log("client connected");
socket.on("disconnect", disconnect);
var intv = setInterval(function(){
socket.emit("hello",Math.random());
},1000);
}
var host = "127.0.0.1";
var port = 8006;
var http = require("http");
var http_serv = http.createServer(handleHTTP).listen(port, host);
var ASQ = require("asynquence");
var node_static = require("node-static");
var static_files = new node_static.Server(__dirname);
var io = require("socket.io").listen(http_serv);
//io.listen(http_serv);
io.on("connection", handleIO);
//https://stackoverflow.com/questions/9914816/what-is-an-example-of-the-simplest-possible-socket-io-example
//configure socket.io -https://socket.io/docs/migrating-from-0-9/
// io.configure(function(){
// io.enable("browser client minification");
// io.enable("browser client etag");
// io.set("log level", 3);
// io.set("transports",[
// "websocket",
// "xhr-polling",
// "jsonp-polling"
// ]);
// });