Skip to content

feat: ability to subscribe on websocket client/server senders #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/http-proxy/ws/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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});
Expand Down