【发布时间】:2017-09-18 07:05:42
【问题描述】:
我使用 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.
希望大家能帮忙。谢谢!
【问题讨论】:
-
也许你可以尝试在你的 nginx 代理中使用 ssl,而不是你的 express 服务器。
标签: node.js ssl socket.io chat azure-web-app-service