同一个 NodeJS 进程上的 http 服务器和 websocket 服务器 运行 可以在本地相互通信吗?
Can an http server and a websocket server running on the same NodeJS process communicate with each other locally?
我已经使用 express
和 ws 创建了一个 https 服务器和一个 websockets 服务器 运行 来自相同的节点进程和 [=] 上的相同端口 (443) 64=] 18.04 服务器。
我的server.js可以归结为:
const express = require('express');
const https = require('https');
const Websocket = require('ws');
const app = express();
app.use(/* ... Express routes */);
const credentials = /* SSL Certificate */;
const httpServer = https.createServer(credentials, app);
const wsServer = new WebSocket.Server({ server: httpServer });
wsServer.on('connection', (ws, req) => {
// Handle incoming websocket messages
});
httpServer.listen(443);
我需要能够在 https://example.com 上向此应用发出 http 请求,并通过 public 互联网从浏览器客户端向 wss://example.com 发送消息。这一切都正常工作。
我还需要能够从 http 服务器向 websockets 服务器发送消息。在 http 服务器上,客户端代码如下所示:
const WebSocket = require('ws');
const ws = new WebSocket("wss://example.com");
ws.onopen = () => {
ws.send( JSON.stringify({ msg: "hello from the http server" }) );
}
同样,这工作得很好。然而,对我来说,http 服务器需要去互联网与 websockets 服务器通信是违反直觉的。
在我的开发机器上,在 localhost:3000
上安装服务器,在 ws://localhost:3000
上安装 websockets 服务器,它们可以相互通信。
有没有办法让外部客户端 send/receive 来自 wss://example.com
的 websocket 消息,而本地 http 服务器与 websocket 服务器通信 "directly" 运行在同一个端口和进程上?
我想象这将避免 http 服务器调用 public 互联网(和 DNS 服务器)并产生延迟。
我尝试了以下(在http服务器连接到websockets服务器的代码中)
const ws = new WebSocket("wss://localhost");
const ws = new WebSocket("ws://localhost");
如果我使用 wss://
...
像在我的开发机器上那样将服务器称为 localhost
或 127.0.0.1
会出现 SSL 错误
Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames
...如果我在端口 80
上使用 ws://
,则会出现 "cannot connect" 错误
code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 80
我已经在寻找问题的答案,但没有找到任何答案。不过,我知道我可能使用了错误的术语进行搜索!
I have used express and ws to create an https server and a websockets server that run from the same Node process and the same port (443) on an Ubuntu 18.04 server.
不是真的...它只是一个 HTTPS 服务器,上面有您的 Web 套接字 运行。 Web 套接字连接以 HTTP 请求开始,然后 "upgraded" 到 Web 套接字。
一个底层服务器,绑定到一个 TCP 端口,这些连接由网络套接字库或您的 HTTP 请求处理程序处理,具体取决于请求方法和协议。
I also need to be able to send messages FROM the http server, TO the websockets server. On the http server, the client code looks something like this:
这没有任何意义。它们是同一个应用程序,对吗?
只需从处理您的网络套接字数据的其他函数中调用您需要的任何函数,而不是发出额外的 HTTP 请求。
However, it is counter intuitive to me that the http server needs to go out to the internet to communicate with the websockets server.
听起来不像是"going out to the internet"。发生这种情况时,您更有可能在本地建立连接。仍然,当您可以直接调用代码时效率不高。
我已经使用 express
和 ws 创建了一个 https 服务器和一个 websockets 服务器 运行 来自相同的节点进程和 [=] 上的相同端口 (443) 64=] 18.04 服务器。
我的server.js可以归结为:
const express = require('express');
const https = require('https');
const Websocket = require('ws');
const app = express();
app.use(/* ... Express routes */);
const credentials = /* SSL Certificate */;
const httpServer = https.createServer(credentials, app);
const wsServer = new WebSocket.Server({ server: httpServer });
wsServer.on('connection', (ws, req) => {
// Handle incoming websocket messages
});
httpServer.listen(443);
我需要能够在 https://example.com 上向此应用发出 http 请求,并通过 public 互联网从浏览器客户端向 wss://example.com 发送消息。这一切都正常工作。
我还需要能够从 http 服务器向 websockets 服务器发送消息。在 http 服务器上,客户端代码如下所示:
const WebSocket = require('ws');
const ws = new WebSocket("wss://example.com");
ws.onopen = () => {
ws.send( JSON.stringify({ msg: "hello from the http server" }) );
}
同样,这工作得很好。然而,对我来说,http 服务器需要去互联网与 websockets 服务器通信是违反直觉的。
在我的开发机器上,在 localhost:3000
上安装服务器,在 ws://localhost:3000
上安装 websockets 服务器,它们可以相互通信。
有没有办法让外部客户端 send/receive 来自 wss://example.com
的 websocket 消息,而本地 http 服务器与 websocket 服务器通信 "directly" 运行在同一个端口和进程上?
我想象这将避免 http 服务器调用 public 互联网(和 DNS 服务器)并产生延迟。
我尝试了以下(在http服务器连接到websockets服务器的代码中)
const ws = new WebSocket("wss://localhost");
const ws = new WebSocket("ws://localhost");
如果我使用 wss://
...
localhost
或 127.0.0.1
会出现 SSL 错误
Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames
...如果我在端口 80
上使用ws://
,则会出现 "cannot connect" 错误
code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 80
我已经在寻找问题的答案,但没有找到任何答案。不过,我知道我可能使用了错误的术语进行搜索!
I have used express and ws to create an https server and a websockets server that run from the same Node process and the same port (443) on an Ubuntu 18.04 server.
不是真的...它只是一个 HTTPS 服务器,上面有您的 Web 套接字 运行。 Web 套接字连接以 HTTP 请求开始,然后 "upgraded" 到 Web 套接字。
一个底层服务器,绑定到一个 TCP 端口,这些连接由网络套接字库或您的 HTTP 请求处理程序处理,具体取决于请求方法和协议。
I also need to be able to send messages FROM the http server, TO the websockets server. On the http server, the client code looks something like this:
这没有任何意义。它们是同一个应用程序,对吗?
只需从处理您的网络套接字数据的其他函数中调用您需要的任何函数,而不是发出额外的 HTTP 请求。
However, it is counter intuitive to me that the http server needs to go out to the internet to communicate with the websockets server.
听起来不像是"going out to the internet"。发生这种情况时,您更有可能在本地建立连接。仍然,当您可以直接调用代码时效率不高。