Socket.io连接但在HTTPS时从不监听任何事件

时间:2018-04-01 17:23:20

标签: node.js sockets ssl nginx socket.io

长话短说; 在localhost和http上开发我的代码时,socket.io很好,并且在我的客户端和服务器之间来回传递。

现在我实现了NGINX并添加了SSL代理,socket.io似乎成功连接了服务器,但是没有听到任何事件或者#34;触发了#34;。

这里是NGINX的error.log文件显示的内容

  

2018/04/01 15:58:45 [错误] 31893#0:* 772 connect()失败(111:连接被拒绝)连接上游,客户端:XXX.XXX.XXX.XXX,服务器:。 com,请求:" GET /socket.io/?EIO=3&transport=polling&t=MA1xgon HTTP / 2.0",上游:" http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=MA1xgon",主持人:" .com",推荐人:" https://.com/"

这是我的服务器WebSocket文件:

    import * as socketIo from 'socket.io';

let io;

export class WebSocket {

    static setServer(server): void {
        io = socketIo(server);

        io.on('connection', (socket) => {
            console.log('new connection');
            socket.on('sessionData', (data) => {
                console.log('Session Data event received');
                try {
                    data = JSON.parse(data);
                } catch(e) {
                    console.log('Unable to parse JSON data', data);
                    return;
                }
                console.log('Permission ID: ' + data.perm_id);
                io.emit('CON-' + data.perm_id,  JSON.stringify(data));
            });
        })
    }
}

这是我的主要服务器类,其中socket.io附加到端口8000

import * as express from 'express';
import * as http from 'http';
import * as cors from 'cors';
import * as Passport from 'passport';
import * as bodyParser from 'body-parser';
import * as fileUpload from 'express-fileupload';
import {AuthStrategy} from './includes/passport-strategy';
import {WebSocket} from './includes/ws';
import {Database} from './includes/database';    

const app = express();


// Middleware go here
Passport.use('jwt', AuthStrategy);
app.use(Passport.initialize());
app.use(cors());
app.use(bodyParser.json());
app.use(fileUpload());

const server = http.createServer(app);
WebSocket.setServer(server);
server.listen(8000, function(){
    console.log('Server is running...');
    Database.connect().then(() => {
      console.log('Connected to SQL...');
      // Database.initializeDB().then(success => { console.log('success');}).catch(err => {
      //     console.log(err);
      // });
    }).catch( err => {
        console.log(err);
        console.log('error connecting to DB, make sure credentials entered are correct (secureaccessserver/Mavis/src/main/Server/includes/config.txt)');
    })
});

现在我的客户端试图向我的socket.io服务器发送

    pathToSocket = 'https://' + window.location.hostname + '/socket.io/';
    // I've also tried pathToSocket = 'https://' + ..... + '/server/' with same results
    socket = io(this.pathToSocket);

    terminateConnection(): void {
    const Permission = this.sessionService.getPermission();
    this.socket.emit('sessionData', JSON.stringify({
      perm_id: Permission.perm_id,
      terminate: true
    }));
  }

最后我的nginx配置文件

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  cracenter.com www.cracenter.com;

        ssl_certificate "/root/cracenter.com.crt";
        ssl_certificate_key "/root/cracenter.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        include /etc/nginx/default.d/*.conf;

        location /server/ {
        proxy_pass "http://127.0.0.1:8000";
        }

        location /socket.io/ {
         proxy_pass "http://127.0.0.1:8000/socket.io/";
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
         proxy_set_header Host $host;
        }
    }

}

现在,当我运行服务器并启动我的客户端(运行在同一个云服务器上,只是不同的端口)时......我得到了新的连接'控制台日志成功(来自websocket文件代码),但"会话数据事件收到"永远不会在控制台中打印,无论我尝试触发多少次" terminateConnection"我的服务器代码中的函数

1 个答案:

答案 0 :(得分:0)

您可能const server = http.createServer(app);

时有const server = https.createServer(app);