java websocket 代理_How to facade (proxy) websocket port

I have server side rendering react app in which i have proxied all http calls to different port. Please see the code below for http proxy.

import proxy from "express-http-proxy";

const app = express();

const bodyParser = require("body-parser");

const http = require('http');

const httpProxy = require('http-proxy');

const PORT = process.env.PORT || 3001;

httpProxy.createServer({

target: 'ws://localhost:4000',

ws: true

}).listen(PORT); //Throws error since 3000 port is already used by //app.listen.

app.use(

"/api",

proxy("http://localhost:4000/", {

proxyReqOptDecorator(opts) {

opts.headers["x-forwarded-host"] = "http://localhost:4000/";

return opts;

}

})

);

app.post("/logger", (req, res) => {

logger.debug(req.body.data);

res.send({ status: "SUCCESS" });

});

app.listen(PORT, () => {

logger.debug(`Portal listening on ${PORT}`);

});

that means when i make any calls /api/endpoint it will redirect to localhost:4000/endpoint but will be seen in the network as http://localhost:3000/endpoint1

I want the same behaviour with websockets as well. I am using new WebSocket(ws://localhost:3000/endpoint1); It should redirect to ws://localhost:4000/endpoint1. and should be shown in network tab as ws://localhost:3000/endpoint1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java WebSocket和SSL WebSocket与HTTPS连接,你需要做以下几步: 1. 生成SSL证书 你需要为你的服务器生成一个SSL证书来启用SSL。你可以使用Java的keytool工具来生成自签名的SSL证书: ``` keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -validity 3650 ``` 2. 配置SSL连接 你需要在你的服务器端点配置SSL连接。这是一个示例代码,你可以根据你的需要进行修改: ``` SSLContext sslContext = SSLContext.getInstance("TLS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("keystore.jks")), "password".toCharArray()); keyManagerFactory.init(keyStore, "password".toCharArray()); sslContext.init(keyManagerFactory.getKeyManagers(), null, null); HttpServer server = HttpServer.create(new InetSocketAddress(443), 0); server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { public void configure(HttpsParameters params) { SSLContext context = getSSLContext(); SSLEngine engine = context.createSSLEngine(); params.setNeedClientAuth(false); params.setCipherSuites(engine.getEnabledCipherSuites()); params.setProtocols(engine.getEnabledProtocols()); params.setSSLParameters(context.getDefaultSSLParameters()); } }); ``` 3. 创建SSL WebSocket服务器 你可以使用Jetty或Tyrus等Java WebSocket库来创建SSL WebSocket服务器。这里以Tyrus为例: ``` SSLContext sslContext = SSLContext.getInstance("TLS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("keystore.jks")), "password".toCharArray()); keyManagerFactory.init(keyStore, "password".toCharArray()); sslContext.init(keyManagerFactory.getKeyManagers(), null, null); Server server = new Server("localhost", 443, "/", null, ChatEndpoint.class, new HashMap<String, Object>(), sslContext); server.start(); ``` 这些步骤应该可以帮助你配置Java WebSocket和SSL WebSocket与HTTPS连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值