[英]How to make websockets to go through a proxy in node.js
Generalizing that would be the question... how to make websockets to go through a proxy in node.js? 概括那将是一个问题......如何让websockets通过node.js中的代理?
In my particular case I'm using pusher.com with the node.js client library they recommend. 在我的特定情况下,我正在使用pusher.com和他们推荐的node.js客户端库 。 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
查看代码内部我想知道一些关于我应该更改的提示,以使这个库与代理一起工作...你可以在这里查看代码
Maybe I should somehow replace or modified the websockets module that is being used by the library? 也许我应该以某种方式替换或修改库使用的websockets模块 ?
EDIT 编辑
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):
需要考虑的几件事(对不起,如果我对其中的一些/全部错了,只是学习):
Try node-http-proxy 尝试node-http-proxy
It allows you to send http or websocket requests through a 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);
Most web proxies don't support websockets yet. 大多数Web代理尚不支持websockets。 The best workaround is to use encryption by specifying wss:// (websocket secure protocol):
最好的解决方法是通过指定wss://(websocket安全协议)来使用加密:
wss://ws.pusherapp.com:[port]/app/[key]
Using a proxy for websockets should work roughly the same as for https connections; 使用websockets的代理应该与https连接大致相同; you should use the CONNECT method.
你应该使用CONNECT方法。 At least that's what both the HTTP and HTML5 specs say.
至少这是HTTP和HTML5规范所说的。 So if your proxy implements CONNECT, you're good to go.
因此,如果您的代理实施CONNECT,那么您很高兴。
From https://www.npmjs.com/package/https-proxy-agent 来自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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.