Permalink
Browse files

Initial commit

working version, only tested in Firefox
  • Loading branch information...
Pfenniger Jonas Pfenniger Jonas
Pfenniger Jonas authored and Pfenniger Jonas committed Dec 25, 2009
0 parents commit d3151507ef2489a41033db439b7018a432c5b6f4
@@ -0,0 +1,20 @@
+= Node http websocket server =
+
+This is an implementation of the server-side of the WebSocket protocol,
+based on the node http server.
+
+One advantage over other implementations, is that you can still
+server normal HTTP requests over the same port of the WebSocket server.
+
+= About node patches =
+
+The current node HTTP server implementation closes the connection
+after a successful HTTP requests. The patch adds request.connection.hikack()
+to change that behavior in case of WebSocket connections.
+
+To apply the patches, go in the root of your nodejs code, and apply:
+ `patch -P0 < ../path-to/node-http-websocket/patches/*.patch`
+
+
+Cheers,
+ zimbatm
@@ -0,0 +1,11 @@
+= Examples
+
+Make sure to have a patched version of node.
+
+Run socketpolicy.pl as root. This is needed by the flash WebSocket implementation that lookups crossdomain policies on port 843 to ask for connection.
+
+Run `node ping.js` in another console
+
+Open http://localhost:8000/sample.html with Firefox (other browsers not yet tested)
+
+Some times, the page takes long to load (unknown bug). Try reloading.
@@ -0,0 +1,73 @@
+#!/usr/bin/env node
+
+// Devel mode
+require.paths.unshift('../lib');
+
+var http = require('http'),
+ ws = require('http/websocket'),
+ posix = require('posix'),
+ sys = require('sys');
+
+const publicDir = './public';
+
+function ping(msg, conn) {
+ sys.debug("PING: " + msg);
+ conn.send(msg);
+}
+
+function notFound(req, res) {
+ var msg = "<h1>File not found : " + req.uri.path + "</h1>";
+ res.sendHeader(404, {
+ "Content-Type": "text/html",
+ "Content-Length": msg.length
+ });
+ res.sendBody(msg);
+ res.finish();
+}
+
+var mimeReg = /\.[^\.]+$/
+mimeMap = {
+ ".html": "text/html",
+ ".js": "text/javascript",
+ ".xml": "application/xml",
+ ".swf": "application/x-shockwave-flash"
+}
+
+function sendFile(req, res, path) {
+ var data;
+ try {
+ data = posix.cat(path, "binary").wait();
+ if (data) {
+ var cType = mimeMap[mimeReg.exec(path)[0]] || "application/octet-stream";
+ res.sendHeader(200, {
+ "Content-Length": data.length,
+ "Content-Type": cType
+ });
+ res.sendBody(data, "binary");
+ res.finish();
+ return;
+ }
+ } catch(e) {
+ sys.debug(e);
+ }
+
+ sys.debug("File not found: " + path);
+ notFound(req, res);
+}
+
+function handler(req, res) {
+ sys.debug(req.method + ' ' + req.uri.path);
+ if (ws.askUpgrade(req)) {
+ sys.debug("Upgrading to WebSocket");
+ var conn = new ws.Connection(req, res);
+ conn.addListener("message", ping);
+ } else {
+ var filePath = publicDir + req.uri.path;
+ sendFile(req, res, filePath);
+ }
+}
+
+var srv = http.createServer();
+srv.addListener("request", handler);
+srv.listen(8000);
+sys.puts("Connected on port :8000");
Oops, something went wrong.

0 comments on commit d315150

Please sign in to comment.