如何在 Node 中为 websocket 做代理

Posted

技术标签:

【中文标题】如何在 Node 中为 websocket 做代理【英文标题】:how to do a proxy for a websocket in Node 【发布时间】:2018-05-24 04:45:18 【问题描述】:

在 Node 中,我可以通过以下方式执行 http 代理:

https://localhost:443 -> http://localhost:8100

使用以下代码:

var express = require('express');
var app = express();
var https = require('https');
var httpProxy = require('http-proxy');
...
app.use(function (req, res, next) 
    httpProxy.createServer(
        target: 
            host: 'localhost',
            port: 8100,
        
    ).web(req, res);
);
...
https.createServer(sslOptions, app).listen(443, function()
    console.log('App running on port: 443');
);

但我还需要通过以下方式对 Web 套接字进行类似的重定向:

wss://localhost:8107 -> ws://localhost:8100

您对我该如何实现这一点有任何想法吗?

谢谢!

【问题讨论】:

也许这会有所帮助***.com/questions/23686379/… 您可以查看@davidesp 的答案吗?如果对您不起作用,请发表评论 【参考方案1】:

http-proxy 模块 supports websockets

只要做:

httpProxy.createServer(
  target: 'ws://localhost:8100',
  ws: true
).listen(8107);

【讨论】:

以上是关于如何在 Node 中为 websocket 做代理的主要内容,如果未能解决你的问题,请参考以下文章