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

时间:2017-04-21 10:13:38

标签: node.js ssl socket.io chat azure-web-sites

我用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时的结果:

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.

希望你们能提供帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

由于您已将应用程序部署到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使用WebSockets,Azure上默认不启用。要启用Web套接字,请转到Azure Portal,从Web Apps刀片中单击Web应用程序,然后单击所有设置> 应用程序设置。在网络套接字下,点击开启。然后点击保存

有关详细信息,请参阅Create a Node.js chat application with Socket.IO in Azure App ServiceSecure your app's custom domain with HTTPS