Skip to content

Commit 3f009b0

Browse files
authored
Merge pull request #4 from gemini-testing/dd.emit_ws_senders
feat: ability to subscribe on websocket client/server senders
2 parents 6a0824b + 136f4a5 commit 3f009b0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,6 @@ If you are using the `proxyServer.listen` method, the following options are also
435435
* `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
436436
* `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
437437
* `proxyRes`: This event is emitted if the request to the target got a response.
438-
* `wsClientMsg`: This event is emitted after webscoket mesage is sended from the client to the server.
439-
* `wsServerMsg`: This event is emitted after websocket message is sended from the server to the client.
440438
* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
441439
* `close`: This event is emitted once the proxy websocket was closed.
442440
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
@@ -489,6 +487,39 @@ proxy.on('close', function (res, socket, head) {
489487

490488
**[Back to top](#table-of-contents)**
491489

490+
### Listening for websocket proxy request events
491+
492+
* `clientSenderInited`: This event is emitted after websocket sender from client to server is initialized.
493+
* `serverSenderInited`: This event is emitted after websocket sender from server to client is initialized.
494+
* `wsClientMsg`: This event is emitted after webscoket message is sended from the client to the server.
495+
* `wsServerMsg`: This event is emitted after websocket message is sended from the server to the client.
496+
497+
```js
498+
httpProxy.createServer({
499+
target: 'ws://localhost:9014',
500+
ws: true
501+
}).listen(8014);
502+
503+
proxyServer.on('upgrade', function (req, socket, head) {
504+
proxy.ws(req, socket, head);
505+
});
506+
507+
proxyServer.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
508+
proxyReq.on('clientSenderInited', (sender) => {
509+
sender.send('hello from client');
510+
});
511+
512+
proxyReq.on('serverSenderInited', (sender) => {
513+
sender.send('hello from server');
514+
});
515+
516+
proxyReq.on('wsClientMsg', console.log);
517+
proxyReq.on('wsServerMsg', console.log);
518+
});
519+
```
520+
521+
**[Back to top](#table-of-contents)**
522+
492523
### Shutdown
493524

494525
* When testing or running server within another program it may be necessary to close the proxy.

lib/http-proxy/ws/interceptor.js

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = class Interceptor {
7777
_interceptClientMessages() {
7878
const receiver = new Receiver(this._clientExtensions);
7979
const sender = new Sender(this._proxySocket, this._serverExtensions);
80+
this._proxyReq.emit('clientSenderInited', sender);
8081

8182
// frame must be masked when send from client to server - https://tools.ietf.org/html/rfc6455#section-5.3
8283
const options = {mask: true};
@@ -92,6 +93,7 @@ module.exports = class Interceptor {
9293
_interceptServerMessages() {
9394
const receiver = new Receiver(this._serverExtensions);
9495
const sender = new Sender(this._socket, this._clientExtensions);
96+
this._proxyReq.emit('serverSenderInited', sender);
9597

9698
const options = {mask: false};
9799
const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options});

0 commit comments

Comments
 (0)