Commit f1a8b573 authored by indexzero's avatar indexzero Committed by Cédric de Saint Martin

[refactor] Listen for `socket` events since reverseProxy.socket is no longer set synchronously

parent bdd6352d
...@@ -663,8 +663,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -663,8 +663,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// If the reverseProxy connection has an underlying socket, // If the reverseProxy connection has an underlying socket,
// then execute the WebSocket handshake. // then execute the WebSocket handshake.
// //
if (typeof reverseProxy.socket !== 'undefined') { reverseProxy.once('socket', function (revSocket) {
reverseProxy.socket.on('data', function handshake (data) { revSocket.on('data', function handshake (data) {
// //
// Ok, kind of harmfull part of code. Socket.IO sends a hash // Ok, kind of harmfull part of code. Socket.IO sends a hash
// at the end of handshake if protocol === 76, but we need // at the end of handshake if protocol === 76, but we need
...@@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
socket.write(sdata); socket.write(sdata);
var flushed = socket.write(data); var flushed = socket.write(data);
if (!flushed) { if (!flushed) {
reverseProxy.socket.pause(); revSocket.pause();
socket.once('drain', function () { socket.once('drain', function () {
try { reverseProxy.socket.resume() } try { revSocket.resume() }
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) } catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
}); });
// //
// Force the `drain` event in 100ms if it hasn't // Force the `drain` event in 100ms if it hasn't
// happened on its own. // happened on its own.
...@@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Remove data listener on socket error because the // Remove data listener on socket error because the
// 'handshake' has failed. // 'handshake' has failed.
// //
reverseProxy.socket.removeListener('data', handshake); revSocket.removeListener('data', handshake);
return proxyError(ex); return proxyError(ex);
} }
...@@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// //
// Remove data listener now that the 'handshake' is complete // Remove data listener now that the 'handshake' is complete
// //
reverseProxy.socket.removeListener('data', handshake); revSocket.removeListener('data', handshake);
}); });
} });
reverseProxy.on('error', proxyError); reverseProxy.on('error', proxyError);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment