From c62610e8e4d59e8ba4642370ff3fb933c6ddb4eb Mon Sep 17 00:00:00 2001 From: Jorge Leal Date: Thu, 11 Dec 2014 20:47:22 +0100 Subject: [PATCH] Deprecated proxySocket event in favor to open event. Maintained both proxySocket and open for backward compatibility. Conflicts: test/lib-http-proxy-test.js --- README.md | 7 ++++--- lib/http-proxy/passes/ws-incoming.js | 5 +++-- test/lib-http-proxy-test.js | 14 ++++++++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index aaf2f69bb..cdf970b63 100644 --- a/README.md +++ b/README.md @@ -195,8 +195,9 @@ http.createServer(function (req, res) { * `error`: The error event is emitted if the request to the target fail. * `proxyRes`: This event is emitted if the request to the target got a response. -* `proxySocket`: This event is emitted once the proxy websocket was created and piped into the target websocket. +* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket. * `close`: This event is emitted once the proxy websocket was closed. +* (DEPRECATED) `proxySocket`: Deprecated in favor to `open`. ```js var httpProxy = require('http-proxy'); @@ -228,9 +229,9 @@ proxy.on('proxyRes', function (proxyRes, req, res) { }); // -// Listen for the `proxySocket` event on `proxy`. +// Listen for the `open` event on `proxy`. // -proxy.on('proxySocket', function (proxySocket) { +proxy.on('open', function (proxySocket) { // listen for messages coming FROM the target here proxySocket.on('data', hybiParseAndLogMessage); }); diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 9e96fbdd2..b72e55702 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -118,8 +118,9 @@ var passes = exports; return i + ": " + proxyRes.headers[i]; }).join('\r\n') + '\r\n\r\n'); proxySocket.pipe(socket).pipe(proxySocket); - // Make sure server exists before we try to emit - server && server.emit('proxySocket', proxySocket); + + server.emit('open', proxySocket); + server.emit('proxySocket', proxySocket); //DEPRECATED. }); return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index e087ebdea..f482dd296 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -280,7 +280,7 @@ describe('lib/http-proxy.js', function() { client.on('open', function () { client.send('hello there'); }); - + var count = 0; function maybe_done () { count += 1; @@ -373,7 +373,7 @@ describe('lib/http-proxy.js', function() { }); - it('should emit close event when socket.io client disconnects', function (done) { + it('should emit open and close events when socket.io client connects and disconnects', function (done) { var ports = { source: gen.port, proxy: gen.port }; var proxy = httpProxy.createProxyServer({ target: 'ws://127.0.0.1:' + ports.source, @@ -389,11 +389,17 @@ describe('lib/http-proxy.js', function() { client.disconnect(); }); } - + var count = 0; + + proxyServer.on('open', function() { + count += 1; + + }); + proxyServer.on('close', function() { proxyServer.close(); server.close(); - done(); + if (count == 1) { done(); } }); server.listen(ports.source);