概括这将是一个问题......如何让websockets通过node.js中的代理?
在我的特定情况下我将pusher.com与他们推荐的node.js client library一起使用。查看代码内部我想知道一些关于我应该更改的提示,以使该库与代理一起工作...您可以查看代码here
也许我应该以某种方式替换或修改库正在使用的websockets module?
修改
感谢您的回答/评论!需要考虑的几件事(对不起,如果我对其中的一些/全部错了,只是学习):
答案 0 :(得分:3)
大多数网络代理尚不支持网页套件。最好的解决方法是通过指定wss://(websocket安全协议)来使用加密:
wss://ws.pusherapp.com:[port]/app/[key]
答案 1 :(得分:1)
它允许您通过代理发送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);
来源:link
答案 2 :(得分:1)
答案 3 :(得分:1)
来自 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();
});