diff --git a/README.md b/README.md index f8d38ae58..f6bda9932 100644 --- a/README.md +++ b/README.md @@ -435,8 +435,6 @@ If you are using the `proxyServer.listen` method, the following options are also * `proxyReq`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "web" connections * `proxyReqWs`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "websocket" connections * `proxyRes`: This event is emitted if the request to the target got a response. -* `wsClientMsg`: This event is emitted after webscoket mesage is sended from the client to the server. -* `wsServerMsg`: This event is emitted after websocket message is sended from the server to the client. * `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 of `open`. @@ -489,6 +487,39 @@ proxy.on('close', function (res, socket, head) { **[Back to top](#table-of-contents)** +### Listening for websocket proxy request events + +* `clientSenderInited`: This event is emitted after websocket sender from client to server is initialized. +* `serverSenderInited`: This event is emitted after websocket sender from server to client is initialized. +* `wsClientMsg`: This event is emitted after webscoket message is sended from the client to the server. +* `wsServerMsg`: This event is emitted after websocket message is sended from the server to the client. + +```js +httpProxy.createServer({ + target: 'ws://localhost:9014', + ws: true +}).listen(8014); + +proxyServer.on('upgrade', function (req, socket, head) { + proxy.ws(req, socket, head); +}); + +proxyServer.on('proxyReqWs', (proxyReq, req, socket, options, head) => { + proxyReq.on('clientSenderInited', (sender) => { + sender.send('hello from client'); + }); + + proxyReq.on('serverSenderInited', (sender) => { + sender.send('hello from server'); + }); + + proxyReq.on('wsClientMsg', console.log); + proxyReq.on('wsServerMsg', console.log); +}); +``` + +**[Back to top](#table-of-contents)** + ### Shutdown * When testing or running server within another program it may be necessary to close the proxy. diff --git a/lib/http-proxy/ws/interceptor.js b/lib/http-proxy/ws/interceptor.js index 01b60ddf2..fcf20cab9 100644 --- a/lib/http-proxy/ws/interceptor.js +++ b/lib/http-proxy/ws/interceptor.js @@ -77,6 +77,7 @@ module.exports = class Interceptor { _interceptClientMessages() { const receiver = new Receiver(this._clientExtensions); const sender = new Sender(this._proxySocket, this._serverExtensions); + this._proxyReq.emit('clientSenderInited', sender); // frame must be masked when send from client to server - https://tools.ietf.org/html/rfc6455#section-5.3 const options = {mask: true}; @@ -92,6 +93,7 @@ module.exports = class Interceptor { _interceptServerMessages() { const receiver = new Receiver(this._serverExtensions); const sender = new Sender(this._socket, this._clientExtensions); + this._proxyReq.emit('serverSenderInited', sender); const options = {mask: false}; const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options});