[英]Node.js chat on Azure website with SSL
我使用Socket.io创建了一个Node.js聊天应用程序。 它在本地环境下运行良好。 当我使用SSL证书部署在Azure网站上时,出现错误。 你们能帮我吗?
Server.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
时的结果:
并且此错误消息来自客户端计算机:
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 Apps(也称为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 Apps仅向公共公开80和443端口。
请改用以下内容:
this.port = process.env.PORT || 3000;
3)Socket.IO使用WebSocket,默认情况下未在Azure上启用。 要启用Web套接字,请进入Azure门户 ,从Web Apps刀片中单击Web应用,然后单击所有设置 > 应用程序设置 。 在“ Web套接字”下 ,单击“ 打开” 。 然后点击保存 。
有关更多详细信息,请参阅在Azure App Service中使用Socket.IO创建Node.js聊天应用程序,并使用HTTPS保护应用程序的自定义域 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.