Created by: RoscoP
I offer this PR up in case this functionality is desired for node-http-proxy - I needed this ability, but couldn't find it anywhere and thought someone else might find it useful. Thanks for the solid code.
I've isolated this ability away from the normal proxy behavior. If you set wsInterceptMessages
to true
in the options then it will intercept and relay websocket messages.
In options:
wsOnMessageToServer
- Is a function called when a message is intercepted on its way to the server. It takes two arguments: data is a websocket message in a Buffer class, and flags describe the message (compressed, binary, etc - from the websocket ws/lib/Receiver and Sender classes. This function is expected to return a buffer that is to be used for the websocket message. If null
is returned, the message is consumed and nothing will be forwarded along the websocket proxy.
wsOnMessageToClient
- Is a function called when a message is intercepted on its way to the client. It also takes the same arguments as wsOnMessageToServer
Events:
message_toserver
- Emitted on message to server, can be used in place of the functions if you only wish to monitor and not modify the messages.
message_toclient
- Same as above for client.
websocket_connected
- Emitted when websocket interception setup is complete.