node.js - Web socket 在其他机器上打不开
问题描述
Web 套接字连接错误
我的服务器有问题。它使用express
和express-ws
用于网络套接字。问题是该服务器在本地主机上运行良好。ssh
但是当我在(参见localhost.run )的帮助下运行它并通过另一台计算机(通过 Chrome)的给定链接访问该站点时,Web 套接字没有打开,并且下一个错误显示在控制台中
main.js:12 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
即使我添加了cert
和key
服务器连接。PS该站点也加载了,只是套接字不起作用。
这是 server.js 代码:
"use strict";
const fs = require("fs");
const credentials = {
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem")
};
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const https = require("https");
const PORT = process.env.PORT || 3000;
app.use(express.static(__dirname + "/public/Messenger"));
app.use(express.static(__dirname + "/public/Login"));
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = new https.createServer(credentials);
const expressWs = require("express-ws")(app, server);//if i take the
//second argument (server) away, it works fine on localhost:3000, but
//with this it fires the error:
//WebSocket connection to 'ws://localhost:3000/' failed: Connection
//closed before receiving a handshake response
const routes = require("./MVC/Router/router.js"); //importing route
routes(app);
app.listen(PORT, () => {
console.log("listening on port " + PORT);
});
这是router.js:
"use strict";
module.exports = function(app) {
const database = require("../Controller/controller.js");
// database Routes
app.route("/").get(database.loadPage);
app.route("/login").get(database.loginPageLoad);
app
.route("/signIn")
.get(database.signInPageLoad)
.post(database.signIn);
app
.route("/submitLogin")
.post(database.loginSubmit)
.get(database.showUsers);
app.ws("/", database.sendmsg);
};
它将处理流程重定向到 controller.js 的下一部分:
const CLIENTS = [];
let counter = 0;
exports.sendmsg = (ws, req) => {
console.log(cache.get("lorem"));
ws.on("message", msg => {
if (msg === "connected") {
console.log("connected");
CLIENTS.push([ws, counter]);
ws.send(JSON.stringify({ counter }));
counter++;
} else if (JSON.parse(msg).msg && JSON.parse(msg).ID) {
CLIENTS.forEach(box => {
if (box[1] === msg.ID) {
console.log(`user ${box[1]} is closed`);
box.push("closed");
box[0].close();
} else {
return;
}
});
} else {
sendAll(msg);
}
ws.on("close", () => {
console.log("disconnected");
ws.close();
});
});
};
function sendAll(message) {
for (let i = 0; i < CLIENTS.length; i++) {
if (CLIENTS[i][0].readyState === 1) {
CLIENTS[i][0].send(message);
}
}
}
最后一段代码就是它在服务器上的作用,不用太在意。问题是当我从另一台计算机输入链接时,网络套接字没有打开。我该如何解决?
解决方案
推荐阅读
- java - 从包含 java 源文件的 jar 创建包含 .class 文件的 jar 文件的最快方法?
- mysql - 当我使用子字符串时,起点是 1,而不是零 (0)
- javascript - 想要使用 getCurrentPosition() 或类似方法获取设备 GPS 位置并将结果返回到 PHP
- reactjs - RxJS - 从组件构造函数调度一个动作
- cors - 请求的默认请求模式实际上是“no-cors”,还是“cors”?
- tensorflow - 如何将 Tensorflow Keras 检查点保存到 pb 文件
- python - 如何从 python 中的 JSON 中获取一个键和值?
- git - 拉取请求数量高于我的回购预期?
- python - 使用 Pandas 沿文本文件行的描述性统计
- android-sensors - 使用 cooja 模拟器模拟传感器