Skip to content

Commit

Permalink
Added support for WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Dec 8, 2010
1 parent e809d7e commit 481838d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -61,6 +61,23 @@ This package also includes an adapter for running Node HTTP apps on top of JSGI
)
);

## WebSocket with JSGI
JSGI middleware can be used to handle incoming WebSocket messages. While JSGI
is designed for HTTP, WebSocket includes HTTP elements and JSGI's streaming capabilities
are well-suited for socket communication. JSGI delegation can be achieved by using
the "ws-jsgi" module in conjunction with the node-websocket-server package.
This "ws-jsgi" module exports a function that can be called with a socket server and
a JSGI handler. For example:
<pre>
var http = require("http").createServer(
require("jsgi-node").Listener(jsgiApp)
);
http.listen(80);
require("jsgi-node/ws-jsgi")(ws.createServer({
server: http
}), jsgiApp);
</pre>

JSGI-Node is licensed under the AFL or BSD license.

Authors include Kris Zyp and Jed Schmidt.
14 changes: 9 additions & 5 deletions lib/jsgi-node.js
Expand Up @@ -222,15 +222,19 @@ function Listener( app ) {
});
}
}
start.Request = Request;
start.Listener = Listener;
start.start = start;

exports.Listener = Listener;

exports.start = function( app, options ) {
function start( app, options ) {
app = new Listener( app );
options = options || {};

var port = options.port || 8080;

require( "http" ).createServer( app ).listen( port );
var http = require( "http" ).createServer( app );
http.listen( port );
sys.puts( "Server running on port " + port );
};
return http;
};
module.exports = start;
26 changes: 26 additions & 0 deletions lib/ws-jsgi.js
@@ -0,0 +1,26 @@
var when = require("./promise").when,
NodeRequest = require("./jsgi-node").Request;


module.exports = function(socketServer, jsgiApp){
socketServer.on("connection", function(connection){
function Request(){}
Request.prototype = new NodeRequest(connection._req);
function Headers(){}
Headers.prototype = Request.prototype.headers;
connection.on("message", function(data){
var request = new Request();
request.body = [data];
request.headers = new Headers();
when(jsgiApp(request), function(response){
when(response.body, function(body){
body.forEach(function(data){
connection.send(data);
});
})
});
});
connection.on("close", function(){
});
});
};

0 comments on commit 481838d

Please sign in to comment.