Skip to content

Commit 3828616

Browse files
committed
[refactor] Listen for socket events since reverseProxy.socket is no longer set synchronously
1 parent ea7fea6 commit 3828616

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
647647
// If the reverseProxy connection has an underlying socket,
648648
// then execute the WebSocket handshake.
649649
//
650-
if (typeof reverseProxy.socket !== 'undefined') {
651-
reverseProxy.socket.on('data', function handshake (data) {
650+
reverseProxy.once('socket', function (revSocket) {
651+
revSocket.on('data', function handshake (data) {
652652
//
653653
// Ok, kind of harmfull part of code. Socket.IO sends a hash
654654
// at the end of handshake if protocol === 76, but we need
@@ -681,12 +681,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
681681
socket.write(sdata);
682682
var flushed = socket.write(data);
683683
if (!flushed) {
684-
reverseProxy.socket.pause();
684+
revSocket.pause();
685685
socket.once('drain', function () {
686-
try { reverseProxy.socket.resume() }
686+
try { revSocket.resume() }
687687
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
688688
});
689-
689+
690690
//
691691
// Force the `drain` event in 100ms if it hasn't
692692
// happened on its own.
@@ -701,7 +701,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
701701
// Remove data listener on socket error because the
702702
// 'handshake' has failed.
703703
//
704-
reverseProxy.socket.removeListener('data', handshake);
704+
revSocket.removeListener('data', handshake);
705705
return proxyError(ex);
706706
}
707707

@@ -711,9 +711,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
711711
//
712712
// Remove data listener now that the 'handshake' is complete
713713
//
714-
reverseProxy.socket.removeListener('data', handshake);
714+
revSocket.removeListener('data', handshake);
715715
});
716-
}
716+
});
717717

718718
reverseProxy.on('error', proxyError);
719719

0 commit comments

Comments
 (0)