Created by: RoscoP
I created some changes in order to help facility intercepting and modifying websocket messages. I highly doubt this is something you would want to take in as is, but proved to be valuable to me and will hopefully for others.
When calling createProxyServer define callback functions in order to modify (and observe) the websocket message.
var proxySettings = {
// ...
ws: true,
wsOnMessageToClient: function(data, flags) {
// modify or observe data
return yourModifyingFunction(data);
},
wsOnMessageToServer: function(data, flags) {
// modify or observe data
return data;
}
};
You can also attach event listeners to only observe the messages.
var proxy = httpProxy.createProxyServer(proxySettings)
.on('proxyReqWs', function(proxyReqWs, req, clientSocket) {
proxyReqWs.on('upgrade', function(res, serverSocket) {
proxyReqWs
.on('message_toserver', function(data, flags) {
console.log('TOSERVER:', data);
}).on('message_toclient', function(data, flags) {
console.log('TOCLIENT:', data);
});
});