joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
MIT License
4.24k stars 970 forks source link

How to set proxy? #124

Open Liang-YY opened 1 year ago

Liang-YY commented 1 year ago

How to set proxy?

reme3d2y commented 1 year ago

Maybe someone will find it useful

const HttpsProxyAgent = require("https-proxy-agent");
const WebSocket = require("ws");
const ReconnectingWebSocket = require("reconnecting-websocket");

const wsUrl = "wss://some.url";

const agent = new HttpsProxyAgent("http://ip:port");

class WebSocketWithAgent extends WebSocket {
    constructor(url, protocols) {
        super(url, protocols, {
            agent,
        });
    }
}

const ws = new ReconnectingWebSocket(wsUrl, [], { WebSocket: WebSocketWithAgent });