Skip to content

Instantly share code, notes, and snippets.

@codedot
Created May 2, 2012 12:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codedot/2576191 to your computer and use it in GitHub Desktop.
Save codedot/2576191 to your computer and use it in GitHub Desktop.
Uniweb Server Merged
var orig = "<!doctype html>";
orig = orig.concat("<meta charset=\"utf-8\">");
orig = orig.concat("<title></title>");
orig = orig.concat("<script>");
orig = orig.concat("var socket = new WebSocket(\"url\");");
orig = orig.concat("socket.onmessage = function (msg) {");
orig = orig.concat("eval(msg.data);");
orig = orig.concat("};");
orig = orig.concat("</script>");
function client(url)
{
var text = orig.replace("url", url);
var head = {
"Content-Type": "text/html",
"Content-Length": text.length
};
function handler(request, response)
{
if ("GET" == request.method) {
response.writeHead(200, head);
response.end(text);
} else {
response.writeHead(400);
response.end();
}
}
return handler;
}
module.exports = client;
var start = require("uniweb");
//var read = require("fs").readFileSync;
function hello(socket, data)
{
if (data) {
socket.run(function (data) {
window.alert(data);
}, data);
} else {
socket.run(function () {
socket.say("Hello World!");
});
}
}
start(hello, {
// domain: "example.com",
// key: read("key.pem"),
// cert: read("cert.pem"),
port: 8080,
portssl: null,
init: true
});
function init(server)
{
function handler(socket)
{
socket.on("message", function (data) {
try {
data = JSON.parse(data);
} catch (error) {
data = undefined;
}
server(socket, data);
});
socket.run = function (f, x) {
f = f.toString();
x = JSON.stringify(x);
this.send("(" + f + ")(" + x + ")");
};
socket.run(function () {
socket.say = function (data) {
data = JSON.stringify(data);
this.send(data);
};
});
server(socket);
}
return handler;
}
module.exports = init;
{
"author": "Anton Salikhmetov",
"description": "Uniweb Server Library",
"name": "uniweb",
"version": "0.0.0",
"scripts": {
"test": "node hello.js"
},
"dependencies": {
"ws": "*"
},
"engines": {
"node": "*"
},
"main": "uniweb"
}
var http = require("http");
var https = require("https");
var ws = require("ws");
var init = require("./init.js");
var client = require("./client.js");
function start(server, options)
{
var param = {
domain: "localhost",
port: 80,
portssl: 443
};
function url(secure)
{
var proto = secure ? "wss" : "ws";
var domain = param.domain;
var port = secure ? param.portssl : param.port;
var defport = secure ? 443 : 80;
port = (defport == port) ? "" : ":" + port;
return proto + "://" + domain + port + "/";
}
function uniweb(host, handler, port)
{
var wss = new ws.Server({
server: host
});
wss.on("connection", server);
host.on("request", handler);
host.listen(port);
}
for (var p in options)
param[p] = options[p];
if (param.init)
server = init(server);
if (param.port) {
var handler = client(url(false));
uniweb(http.createServer(), handler, param.port);
}
if (param.portssl) {
var handler = client(url(true));
uniweb(https.createServer({
key: param.key,
cert: param.cert
}), handler, param.portssl);
}
}
module.exports = start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment