Is there any option to create fake websocket connections? I want to use this in test environment to check the behaviour of server and client.
const WebSocket = require('ws');
const wss1 = new WebSocket('ws://www.host.com/path');
const wss3 = new WebSocket('ws://www.host.com/path');
In above code wss1 and wss3 are same.
I want to create different connections for both of them.
Is it possible to achieve?
What do you mean with "fake"?
The above code creates two different connections.
If you don't want to establish a connection you can use a custom agent:
Hi @lpinca
I think my question is not clear. Let me try to explain my requirement.
const WebSocket = require('ws');
let wss = new WebSocket.Server({
clientTracking: true,
port: 8000
});
wss.on('connection', ws => {
if(ws._socket.remoteAddress == "127.0.0.1"){ // Some code.. }
else{//Code for connection from different url..}
})
const wss1 = new WebSocket('ws://www.host.com/path');
const wss3 = new WebSocket('ws://www.host.com/path');
Now for connection from wss1 and wss3 both satisfies the if condition inside on('connection').
Is there any way I can define some different ip for ws._socket.remoteAddress while connecting to websocket from clients for test environment?
Take a look at this test:
I am getting the error mentioned in the on error part. EADDRNOTAVAIL. @lpinca
It should work on Linux/Windows, if you are on macOS you have to add additional loopback addresses, search on Google how to do that.
Thanks @lpinca
I did the changes and now able to connect ws for different ip addresses.
For anyone using macOS and getting error EADDRNOTAVAIL can follow the link:
Most helpful comment
Take a look at this test:
https://github.com/websockets/ws/blob/1504d6713138431dacd67407c115290c0985899c/test/websocket.test.js#L88-L110