From 38286168161d4f4ad24d2ad95ccd8335e9ed08a4 Mon Sep 17 00:00:00 2001 From: indexzero Date: Fri, 23 Dec 2011 01:33:24 -0500 Subject: [PATCH] [refactor] Listen for `socket` events since reverseProxy.socket is no longer set synchronously --- lib/node-http-proxy/http-proxy.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 476b16c..486c2e6 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -647,8 +647,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) // If the reverseProxy connection has an underlying socket, // then execute the WebSocket handshake. // - if (typeof reverseProxy.socket !== 'undefined') { - reverseProxy.socket.on('data', function handshake (data) { + reverseProxy.once('socket', function (revSocket) { + revSocket.on('data', function handshake (data) { // // Ok, kind of harmfull part of code. Socket.IO sends a hash // at the end of handshake if protocol === 76, but we need @@ -681,12 +681,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) socket.write(sdata); var flushed = socket.write(data); if (!flushed) { - reverseProxy.socket.pause(); + revSocket.pause(); socket.once('drain', function () { - try { reverseProxy.socket.resume() } + try { revSocket.resume() } catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) } }); - + // // Force the `drain` event in 100ms if it hasn't // happened on its own. @@ -701,7 +701,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) // Remove data listener on socket error because the // 'handshake' has failed. // - reverseProxy.socket.removeListener('data', handshake); + revSocket.removeListener('data', handshake); return proxyError(ex); } @@ -711,9 +711,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) // // Remove data listener now that the 'handshake' is complete // - reverseProxy.socket.removeListener('data', handshake); + revSocket.removeListener('data', handshake); }); - } + }); reverseProxy.on('error', proxyError);