[英]ERR_SSL_PROTOCOL_ERROR - Socket.io and Node.js
我正在使用 Node.js 服務器並使用 Socket.io 通過 Socket 管理連接,但我遇到了 SSL 證書問題。 很多用戶可以正常訪問 Node.js 服務器,但其他用戶無法訪問,他們收到此錯誤:
當我設置我的服務器時,我有這個 SSL 選項:
var privateKey = fs.readFileSync('/root/cert/key.key', 'utf8').toString();
var certificate = fs.readFileSync('/root/cert/cert.crt', 'utf8').toString();
var ca = fs.readFileSync('/root/cert/ca.crt').toString();
var credentials = { key: privateKey, cert: certificate, ca: ca };
var app = express();
var httpsServer = https.createServer(credentials, app);
var io = require('socket.io')(httpsServer);
有人知道如何解決這個錯誤嗎? 問題是只有部分用戶收到此錯誤。
好吧,據我所知,您缺少 https 服務器的偵聽端口。
var httpsServer = https.createServer(credentials, app).listen(<port>);
我對ERR_SSL_PROTOCOL_ERROR
有很多麻煩,我必須給你我的解決方案
NodeJS 服務器16.15.0
與"socket.io": "2.4.1"
(我假設它適用於其他 NodeJS 版本)
const path = require("path");
const fs = require("fs");
const io = require("socket.io")();
const folder = path.join(__dirname, "ssl");
const privateKey = fs.readFileSync(path.join(folder, "server_key.pem"), "utf8");
const certificate = fs.readFileSync(path.join(folder, "server_cert.pem"), "utf8");
const optSsl = {
key: privateKey,
cert: certificate,
ca: [certificate],
requestCert: false, // put true if you want a client certificate, tested and it works
rejectUnauthorized: false,
};
const server = require("https");
const webServer = server.createServer(optSsl);
webServer.listen(8025); // port number
io.listen(webServer);
io.on("connection", (client) => {
/*...*/
});
/*...*/
帶有“ngx-socket-io”的Angular 10.2.7
"ngx-socket-io": "3.2.0"
(我認為它也適用於其他Angular版本)
import { Socket, SocketIoConfig } from 'ngx-socket-io';
/*...*/
socket: Socket;
socketConfig: SocketIoConfig;
socketConfig = {
url: "https://myserver:8025",
options: {},
};
socket = new Socket(socketConfig);
socket.connect();
看起來您的 apache2 配置不正確。 檢查您是否啟用了配置文件:
a2ensite default-ssl
然后重啟服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.