gpt4 book ai didi

使用 SSL 在 Azure 网站上使用 Node.js 聊天

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:22 25 4
gpt4 key购买 nike

我使用 Socket.io 创建了一个 Node.js 聊天应用程序。它在本地环境中运作良好。当我使用 SSL 证书部署到我的 Azure 网站时,出现错误。你们能帮帮我吗?

服务器.js:

const connect = require('connect');
const fs = require('fs');

const options = {
key: fs.readFileSync('./www.reic.vn.key'),
cert: fs.readFileSync('./www.reic.vn.crt'),
ca: fs.readFileSync('./bundle.crt'),
requestCert: false,
rejectUnauthorized: false
};

onst express = require("express");
const http = require('https');//.createServer(options);
const socketio = require('socket.io');

const bodyParser = require('body-parser');
const routes = require('./utils/routes');
const config = require('./utils/config');

const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/chat');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('we are connected!');
});
var chatSchema = mongoose.Schema({
From:String,
Message:String,
To:String,
FromIdWeb: String,
ToIdWeb: String,
FromImgWeb: String,
ToImgWeb:String,
IsReaded: {type:String,default:'1'},
CreatedDate:{type:Date,default:Date.now}
});
var Chat = mongoose.model('Message',chatSchema);
//
class Server{
constructor(){
//this.port = process.env.PORT || 3000;
this.port = process.env.HTTPS_PORT||3000;
this.host = `127.0.0.1`;
this.app = express();
//this.https = https.Server(options.this.app);
this.http = http.Server(options,this.app);
//this.http = http.Server(this.app);
this.socket = socketio(this.http);
}
appConfig(){
this.app.use(
bodyParser.json()
);
new config(this.app);
}
/* Including app Routes starts*/
includeRoutes(){
new routes(this.app,this.socket).routesConfig();
}
/* Including app Routes ends*/
appExecute(){
debugger;
this.appConfig();
this.includeRoutes();
this.http.listen(this.port, this.host, () => {
console.log(`Listening on http://${this.host}:${this.port}`);
});
}
}
const app = new Server();
app.appExecute();

端口 3000 是在 Azure 应用程序设置中创建的。

我正在使用 Comodo EssentialSSL 证书。我使用 OPENSSL-Win32 将 Comodo 证书文件生成为 .gem 和 .key。

这是我运行命令 node sever.js 时的结果:

result

来自客户端计算机的错误消息:

Firefox can’t establish a connection to the server at wss://www.reic.vn/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=SbXYLozbTo28Waa4k6r1fueDrvVEmjTozpydvGbJQqTXSiNdEiSr03svJNXa2D8gp1UwYUt85axpXvcdIn4Lkr7GBjP%2FUYN4%2BmEyu5nO7%2FtwC4SJHuV%2F8LjYzbCs1oJ97MMFKEYLo7kP4eHATVBBtw%3D%3D&connectionData=%5B%7B%22name%22%3A%22hubs%22%7D%5D&tid=10.

希望大家帮帮忙。谢谢!

最佳答案

由于您将应用程序部署到 Azure Web 应用程序(也称为 Azure 网站),因此您需要注意以下事项。

1) 您不必通过在 Azure 上手动执行命令 node server.js 来启动在 Azure Web Apps 上运行的 Node.js 应用程序。由于你的应用根目录下已有入口文件server.js和IIS web.config,你可以直接通过URL浏览你的Node.js应用.

2) 您不应在 Azure 应用程序设置中创建任何端口变量,因为 Azure 使用管道端口来转换 HTTP 请求,而 Azure Web 应用程序仅公开 80443 公开端口。

请改用以下内容:

this.port =  process.env.PORT || 3000;

3) Socket.IO 使用 WebSockets,这在 Azure 上默认不启用。要启用网络套接字,请进入 Azure Portal ,单击 Web 应用边栏选项卡中的 Web 应用,单击所有设置> 应用程序设置。在 Web Sockets 下,点击开启。然后点击保存

更多详情请引用Create a Node.js chat application with Socket.IO in Azure App ServiceSecure your app's custom domain with HTTPS .

关于使用 SSL 在 Azure 网站上使用 Node.js 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com