1 var sys
= require("sys")
3 , Path
= require("path")
4 , http
= require("http")
5 , crypto
= require("crypto")
6 , ws
= require('../lib/ws/server');
8 console
.log(process
.pid
);
10 /*-----------------------------------------------
12 -----------------------------------------------*/
13 var months
= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
16 return n
< 10 ? '0' + n
.toString(10) : n
.toString(10);
19 function timestamp() {
27 , (d
.getTime() + "").substr( - 4, 4)
33 sys
.puts(timestamp() + ' - ' + msg
.toString());
37 function readPage(path
, callback
){
39 callback(cache
[path
]);
43 fs
.createReadStream( Path
.normalize(Path
.join(__dirname
, path
)), {
47 'bufferSize': 4 * 1024
48 }).addListener("data", function(chunk
){
49 cache
[path
].push(chunk
);
50 }).on("end", function(){
51 callback(cache
[path
]);
56 readPage("client.html", function(){});
59 function serveFile(req
, res
){
60 if(req
.method
== "GET"){
61 if( req
.url
.indexOf("favicon") > -1 ){
62 log("HTTP: inbound request, served nothing, (favicon)");
64 res
.writeHead(200, {'Content-Type': 'image/x-icon'});
67 log("HTTP: inbound request, served client.html");
68 res
.writeHead(200, {'Content-Type': 'text/html', 'Connection': 'close'});
69 readPage("client.html", function(data
){
70 data
.forEach(function(datum
){
78 res
.writeHead(200, {'Content-Type': 'text/html'});
83 /*-----------------------------------------------
85 -----------------------------------------------*/
87 var certPem
= fs
.readFileSync(Path
.normalize(Path
.join(__dirname
, "ssl/cert.pem")), 'ascii');
88 var keyPem
= fs
.readFileSync(Path
.normalize(Path
.join(__dirname
, "ssl/cert.key")), 'ascii');
90 var credentials
= crypto
.createCredentials({key
:keyPem
, cert
:certPem
});
93 var httpServer
= http
.createServer(serveFile
);
96 var server
= ws
.createServer({
101 server
.setSecure(credentials
);
104 server
.addListener("listening", function(){
105 log("Listening for connections.");
108 // Handle WebSocket Requests
109 server
.addListener("connection", function(conn
){
110 conn
.send("Connection: "+conn
.id
);
111 conn
.broadcast("<"+conn
.id
+"> connected");
112 conn
.addListener("message", function(message
){
113 conn
.broadcast("<"+conn
.id
+"> "+message
);
114 // conn.storage.incr("messages");
118 server
.addListener("close", function(conn
){
119 // sys.puts(conn.id+" sent "+conn.storage.get("messages"));
120 server
.broadcast("<"+conn
.id
+"> disconnected");