如何让 websockets 通过 node.js 中的代理 [英] How to make websockets to go through a proxy in node.js

查看:22
本文介绍了如何让 websockets 通过 node.js 中的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概括这将是一个问题...如何使 websockets 通过 node.js 中的代理?

Generalizing that would be the question... how to make websockets to go through a proxy in node.js?

在我的特殊情况下我使用 pusher.com 和 他们推荐 node.js 客户端库.查看代码内部,我想知道一些关于我应该更改什么以使此库与代理一起使用的提示...您可以查看代码 这里

In my particular case I'm using pusher.com with the node.js client library they recommend. Looking inside the code I would like to know some hints on what I should change in order to make this library to work with a proxy... you can take a look in the code here

也许我应该以某种方式替换或修改 websockets 模块?

Maybe I should somehow replace or modified the websockets module that is being used by the library?

编辑

感谢您的回答/评论!需要考虑的几件事(如果我对其中的一些/全部错误,请原谅我,只是学习):

Thanks for your answers/comments! A couple of things to take into consideration (excuse me if I'm wrong with some/all of them, just learning):

  • 我不想创建代理服务器.我只想使用我公司内现有的代理服务器来代理我的 websockets 请求(特别是 pusher.com)
  • 只是想让你知道,如果我使用像 windows 那样的代理 Proxifier 并且我设置了检查到端口 443 的所有连接以通过代理服务器的规则proxy-my.coporate.com:1080(类型 SOCKS5)它就像一个魅力.
  • 但我不想走这条路.我想在我的节点 js 代码中以编程方式配置这个代理服务器(即使这涉及修改我提到的推送器库)
  • 我知道如何使用 请求模块(查找提到如何使用代理).
    • 我想要一个类似的 websockets.
    • I don't want to create a proxy server. I just want to use an existent proxy server within my company in order to proxified my websockets requests (particularly pusher.com)
    • Just to let you know, if I use a proxifier like the one for windows Proxifier and I set up the rule to inspect for all connections to port 443 to go through the proxy server proxy-my.coporate.com:1080 (type SOCKS5) it works like a charm.
    • But I don't want to go this way. I want to programatically configuring this proxy server within my node js code (even if that involved to modified the pusher library I mentioned)
    • I know how to do this for HTTP using Request module (look for the section that mentions how to use a proxy).
      • I want a similarly thing for websockets.

      推荐答案

      来自https://www.npmjs.com/package/https-proxy-agent

      var url = require('url');
      var WebSocket = require('ws');
      var HttpsProxyAgent = require('https-proxy-agent');
      
      // HTTP/HTTPS proxy to connect to
      var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
      console.log('using proxy server %j', proxy);
      
      // WebSocket endpoint for the proxy to connect to
      var endpoint = process.argv[2] || 'ws://echo.websocket.org';
      var parsed = url.parse(endpoint);
      console.log('attempting to connect to WebSocket %j', endpoint);
      
      // create an instance of the `HttpsProxyAgent` class with the proxy server information
      var options = url.parse(proxy);
      
      var agent = new HttpsProxyAgent(options);
      
      // finally, initiate the WebSocket connection
      var socket = new WebSocket(endpoint, { agent: agent });
      
      socket.on('open', function () {
        console.log('"open" event!');
        socket.send('hello world');
      });
      
      socket.on('message', function (data, flags) {
        console.log('"message" event! %j %j', data, flags);
        socket.close();
      });
      

      这篇关于如何让 websockets 通过 node.js 中的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆