Created
April 30, 2012 12:55
-
-
Save codedot/2558088 to your computer and use it in GitHub Desktop.
Uniweb Server Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var start = require("uniweb"); | |
//var read = require("fs").readFileSync; | |
function hello(socket) { | |
socket.on("message", function(msg) { | |
socket.send("alert(\"" + msg + "\");"); | |
}); | |
socket.send("socket.send(\"Hello World!\");"); | |
} | |
start({ | |
handler: hello, | |
port: 8080, | |
portssl: null, | |
// domain: "example.com", | |
// key: read("key.pem"), | |
// cert: read("cert.pem") | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"author": "Anton Salikhmetov", | |
"description": "Combined HTTP/HTTPS/WebSocket Server", | |
"name": "uniweb", | |
"version": "1.1.1", | |
"scripts": { | |
"test": "node hello.js" | |
}, | |
"dependencies": { | |
"ws": "*" | |
}, | |
"engines": { | |
"node": "*" | |
}, | |
"main": "uniweb" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(options) { | |
var param = { | |
domain: "localhost", | |
port: 80, | |
portssl: 443 | |
}; | |
function client(url) | |
{ | |
var html = "<!doctype html>"; | |
var meta = "<meta charset=\"utf-8\">"; | |
var title = "<title></title>"; | |
var script = "<script>(" + (function (url) { | |
var socket = new WebSocket(url); | |
socket.onmessage = function(msg) { | |
eval(msg.data); | |
}; | |
}).toString() + ")(\"" + url + "\");</script>"; | |
var text = html + meta + title + script; | |
return function(request, response) { | |
response.writeHead(200, { | |
"Content-Type": "text/html" | |
}); | |
response.end(text); | |
}; | |
} | |
function ws(port, secure) | |
{ | |
var proto = secure ? "wss" : "ws"; | |
var auto = secure ? 443 : 80; | |
port = (auto == port) ? "" : ":" + port; | |
return proto + "://" + param.domain + port + "/"; | |
} | |
function uniweb(host, secure) | |
{ | |
var port = secure ? param.portssl : param.port; | |
(new (require("ws").Server)({ | |
server: host | |
})).on("connection", param.handler); | |
host.on("request", client(ws(port, secure))); | |
host.listen(port); | |
} | |
for (var p in options) { | |
param[p] = options[p]; | |
} | |
if (param.handler) { | |
uniweb(require("http").createServer(), false); | |
if (param.portssl) { | |
uniweb(require("https").createServer({ | |
key: param.key, | |
cert: param.cert | |
}), true); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://search.npmjs.org/#/uniweb