如何使websockets在node.js中通过代理

推广这将是一个问题…如何使websockets通过node.js代理?

在我个人的情况下,我使用pusher.com和他们推荐的node.js客户端库 。 在代码里面,我想知道我应该改变的一些提示,以便使这个库能够与代理一起工作…你可以看看这里的代码

也许我应该以某种方式replace或修改库正在使用的websockets模块 ?

编辑

感谢您的回答/评论! 一些事情要考虑(对不起,如果我有一些/所有的人都错了,只是学习):

  • 我不想创build代理服务器。 我只想使用我公司内现有的代理服务器来代理我的websockets请求(特别是pusher.com)
  • 只是为了让你知道,如果我使用像Windows Proxifier这样的代理服务器,我build立了规则来检查所有连接到端口443去通过代理服务器proxy-my.coporate.com:1080(typesSOCKS5)它就像一个魅力。
  • 但我不想这样。 我想编程在我的节点js代码中configuration这个代理服务器(即使涉及修改我提到的推进器库)
  • 我知道如何使用Request模块为HTTP执行此操作(查找提及如何使用代理的部分)。
    • 我想为websockets一个类似的东西。

大多数networking代理还不支持websocket。 最好的解决方法是通过指定wss://(websocket安全协议)来使用encryption:

wss://ws.pusherapp.com:[port]/app/[key] 

使用websockets代理应该与https连接大致相同; 你应该使用CONNECT方法。 至less这就是HTTP和HTML5规范所说的。 所以,如果你的代理实现连接,你很好去。

试试node-http-proxy

它允许您通过代理发送http或websocket请求。

 var http = require('http'), httpProxy = require('http-proxy'); // // Create a basic proxy server in one line of code... // // This listens on port 8000 for incoming HTTP requests // and proxies them to port 9000 httpProxy.createServer(9000, 'localhost').listen(8000); // // ...and a simple http server to show us our request back. // http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); }).listen(9000); 

来源: 链接