proxy-websocket provides an easy way to connect to websockets with HTTP/HTTPS proxies.
You can install proxy-websocket via npm:
npm install proxy-websocket
const proxyToWs = require('proxy-websocket');
const socket = proxyToWs('wss://echo.websocket.org/', 'http://154.202.103.5:3128');
socket.on('open', () => {
console.log('WebSocket connection opened');
});
socket.on('message', (data) => {
const message = Buffer.from(data).toString('utf8');
console.log('Received message:', message);
});
socket.on('close', () => {
console.log('WebSocket connection closed');
});
The proxyToWs
function takes two arguments:
- The URL of the WebSocket server you want to connect to.
- The URL of the HTTP/HTTPS proxy you want to use.
const socket = proxyToWs(websocketServerUrl, httpProxyUrl);