From cc23c6be8c9e79b55f243948785ec00387c8dc3d Mon Sep 17 00:00:00 2001 From: ben hockey Date: Wed, 24 Sep 2014 16:45:02 -0500 Subject: [PATCH 1/2] update to work with ws instead of websocket-server --- ws-jsgi.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ws-jsgi.js b/ws-jsgi.js index 7d6c3ba..7a75b74 100644 --- a/ws-jsgi.js +++ b/ws-jsgi.js @@ -3,25 +3,24 @@ var when = require("./promise").when, module.exports = function(socketServer, jsgiApp){ - socketServer.on("connection", function(connection){ - connection._req.connection.setTimeout(0); + socketServer.on("connection", function(socket){ + var req = socket.upgradeReq; + req.setTimeout(0); function Request(){} - Request.prototype = new NodeRequest(connection._req); + Request.prototype = new NodeRequest(req); function Headers(){} Headers.prototype = Request.prototype.headers; - connection.on("message", function(data){ + socket.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); + socket.send(data); }); }) }); }); - connection.on("close", function(){ - }); }); -}; \ No newline at end of file +}; From ddb00340ef31f62dca7b90827d86392f4ffcb33a Mon Sep 17 00:00:00 2001 From: ben hockey Date: Thu, 25 Sep 2014 12:17:23 -0500 Subject: [PATCH 2/2] collect the chunks and send to over socket in one message it would be good if this could be made to stream from forEach but it seems that forEach doesn't accept backpressure. --- ws-jsgi.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ws-jsgi.js b/ws-jsgi.js index 7a75b74..b91c375 100644 --- a/ws-jsgi.js +++ b/ws-jsgi.js @@ -16,10 +16,20 @@ module.exports = function(socketServer, jsgiApp){ request.headers = new Headers(); when(jsgiApp(request), function(response){ when(response.body, function(body){ - body.forEach(function(data){ - socket.send(data); + var chunks = [], + done = false; + when(body.forEach(function (chunk) { + chunks.push(chunk); + }), function () { + done = true; }); - }) + socket.stream(function (err, send) { + if (!err && chunks.length) { + send(chunks.join(''), done); + chunks = []; + } + }); + }); }); }); });