I-Node ws package yamukela i-HTTP agent kuma-connection options ayo. Uma ufuna i-forward proxy, ingxenye ye-ES module ingasebenzisa i-HttpsProxyAgent:

import WebSocket from 'ws';
import { HttpsProxyAgent } from 'https-proxy-agent';

const agent = new HttpsProxyAgent('http://proxy.example.net:8080');
const socket = new WebSocket('wss://socket.example.net/events', {
  agent,
});

socket.on('error', error => {
  console.error(error.message);
});

Lokhu kucabanga ukuthi womabili amaphakethe asevele eyingxenye yohlelo lokusebenza. I-WebSocket URL ikhetha isevisi, i-agent URL ikhetha i-proxy. Ikheli le-HTTP elivela kuhlu lwe-HTTP proxy lingagcwalisa i-agent URL kokuhlolwa kwe-WSS komphakathi okuvunyelwe ngaphandle kobufakazi be-proxy; hlola ukufinyelela endaweni yokugcina yesevisi yangempela.

I-HttpsProxyAgent yakha umhubhe we-HTTP CONNECT futhi inikeza i-client ukuthuthwa okuxhunyiwe. Isekela kokubili ukuxhumana kwe-WebSocket nezicelo ze-HTTPS, ngakho uhlelo lokusebenza akudingeki lwakhe ukushintshana kwe-CONNECT ngokwalo.

Qhubeka nokuphatha okujwayelekile kwe-open, message ne-close kuhlelo lokusebenza. Umcimbi we-open ukhombisa ukuqedwa kwe-WebSocket handshake emzileni okhethiwe; ukudala into ye-JavaScript kuphela akukukugqibile.

Izihloko ezihlobene