Skip to content

Commit f1a8b57

Browse files
indexzerodesaintmartin
authored andcommitted
[refactor] Listen for socket events since reverseProxy.socket is no longer set synchronously
1 parent bdd6352 commit f1a8b57

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

lib/node-http-proxy/http-proxy.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
663663
// If the reverseProxy connection has an underlying socket,
664664
// then execute the WebSocket handshake.
665665
//
666-
if (typeof reverseProxy.socket !== 'undefined') {
667-
reverseProxy.socket.on('data', function handshake (data) {
666+
reverseProxy.once('socket', function (revSocket) {
667+
revSocket.on('data', function handshake (data) {
668668
//
669669
// Ok, kind of harmfull part of code. Socket.IO sends a hash
670670
// at the end of handshake if protocol === 76, but we need
@@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
697697
socket.write(sdata);
698698
var flushed = socket.write(data);
699699
if (!flushed) {
700-
reverseProxy.socket.pause();
700+
revSocket.pause();
701701
socket.once('drain', function () {
702-
try { reverseProxy.socket.resume() }
702+
try { revSocket.resume() }
703703
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
704704
});
705-
705+
706706
//
707707
// Force the `drain` event in 100ms if it hasn't
708708
// happened on its own.
@@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
717717
// Remove data listener on socket error because the
718718
// 'handshake' has failed.
719719
//
720-
reverseProxy.socket.removeListener('data', handshake);
720+
revSocket.removeListener('data', handshake);
721721
return proxyError(ex);
722722
}
723723

@@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
727727
//
728728
// Remove data listener now that the 'handshake' is complete
729729
//
730-
reverseProxy.socket.removeListener('data', handshake);
730+
revSocket.removeListener('data', handshake);
731731
});
732-
}
732+
});
733733

734734
reverseProxy.on('error', proxyError);
735735

0 commit comments

Comments
 (0)