Created
April 6, 2010 19:48
-
-
Save horatio-sans-serif/358003 to your computer and use it in GitHub Desktop.
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
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
<script> | |
var ws = new WebSocket("ws://localhost:8080"); | |
ws.onopen = function(evt) { alert("Connection open ..."); }; | |
ws.onmessage = function(evt) { alert( "Received Message: " + evt.data); }; | |
ws.onclose = function(evt) { alert("Connection closed."); }; | |
ws.send("Hello Web Socket!"); | |
ws.close(); | |
</script> |
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
$ node test.js | |
DEBUG: connect: / | |
DEBUG: SENDING:{"time":"Tue Apr 06 2010 15:46:20 GMT-0400 (EDT)"} <-- Crashes Chrome on OS X! | |
DEBUG: close | |
^C | |
$ node -v | |
0.1.33 | |
(This all works fine in Webkit Nighty so I don't care for now, it's Chrome's fault not this code). |
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 sys = require("sys"), | |
ws = require("./ws"); | |
ws.createServer(function (websocket) { | |
websocket.addListener("connect", function (resource) { | |
sys.debug("connect: " + resource); | |
websocket.write(JSON.stringify({time:(new Date()).toString()})); | |
}).addListener("data", function (data) { | |
sys.debug(data); | |
}).addListener("close", function () { | |
sys.debug("close"); | |
}); | |
}).listen(8080); |
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
// Github: http://github.com/ncr/node.ws.js | |
// Author: Jacek Becela | |
// License: MIT | |
// Based on: http://github.com/Guille/node.websocket.js | |
function nano(template, data) { | |
return template.replace(/\{([\w\.]*)}/g, function (str, key) { | |
var keys = key.split("."), value = data[keys.shift()]; | |
keys.forEach(function (key) { value = value[key] }); | |
return value; | |
}); | |
} | |
var sys = require("sys"), | |
tcp = require("tcp"), | |
headerExpressions = [ | |
/^GET (\/[^\s]*) HTTP\/1\.1$/, | |
/^Upgrade: WebSocket$/, | |
/^Connection: Upgrade$/, | |
/^Host: (.+)$/, | |
/^Origin: (.+)$/ | |
], | |
handshakeTemplate = [ | |
'HTTP/1.1 101 Web Socket Protocol Handshake', | |
'Upgrade: WebSocket', | |
'Connection: Upgrade', | |
'WebSocket-Origin: {origin}', | |
'WebSocket-Location: ws://{host}{resource}', | |
'', | |
'' | |
].join("\r\n"), | |
policy_file = '<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>'; | |
exports.createServer = function (websocketListener) { | |
return tcp.createServer(function (socket) { | |
socket.setTimeout(0); | |
socket.setNoDelay(true); | |
socket.setEncoding("utf8"); | |
var emitter = new process.EventEmitter(), | |
handshaked = false, | |
buffer = ""; | |
function handle(data) { | |
buffer += data; | |
var chunks = buffer.split("\ufffd"), | |
count = chunks.length - 1; // last is "" or a partial packet | |
for(var i = 0; i < count; i++) { | |
var chunk = chunks[i]; | |
if(chunk[0] == "\u0000") { | |
emitter.emit("data", chunk.slice(1)); | |
} else { | |
socket.close(); | |
return; | |
} | |
} | |
buffer = chunks[count]; | |
} | |
function handshake(data) { | |
var headers = data.split("\r\n"); | |
if(/<policy-file-request.*>/.exec(headers[0])) { | |
socket.write(policy_file); | |
socket.close(); | |
return; | |
} | |
var matches = [], match; | |
for (var i = 0, l = headerExpressions.length; i < l; i++) { | |
match = headerExpressions[i].exec(headers[i]); | |
if (match) { | |
if(match.length > 1) { | |
matches.push(match[1]); | |
} | |
} else { | |
socket.close(); | |
return; | |
} | |
} | |
socket.write(nano(handshakeTemplate, { | |
resource: matches[0], | |
host: matches[1], | |
origin: matches[2], | |
})); | |
handshaked = true; | |
emitter.emit("connect", matches[0]); | |
} | |
socket.addListener("data", function (data) { | |
if(handshaked) { | |
handle(data); | |
} else { | |
handshake(data); | |
} | |
}).addListener("end", function () { | |
socket.close(); | |
}).addListener("close", function () { | |
if (handshaked) { // don't emit close from policy-requests | |
emitter.emit("close"); | |
} | |
}); | |
emitter.remoteAddress = socket.remoteAddress; | |
emitter.write = function (data) { | |
try { | |
sys.debug("SENDING:"+data); | |
socket.write('\u0000' + data + '\uffff'); | |
} catch(e) { | |
// Socket not open for writing, | |
// should get "close" event just before. | |
socket.close(); | |
sys.error("FAILED to write!"); | |
} | |
} | |
emitter.close = function () { | |
socket.close(); | |
} | |
websocketListener(emitter); // emits: "connect", "data", "close", provides: write(data), close() | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment