socket.io node.js 连接被拒绝

时间:2021-08-01 16:15:57

标签: node.js nginx websocket socket.io

我有以下 node.js 服务器

const port = 5555;
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http,{
    path: '/testSocket/socket.io'
});

app.get('/testSocket', function(req, res) {
    res.sendfile('index.html');
});


io.on('connection', function (socket) {
    socket.on( 'new_notification', function( data ) {
        console.log(data.title,data.message);
        io.sockets.emit( 'show_notification', {
            title: data.title,
            message: data.message,
            icon: data.icon,
        });
    });
});

http.listen(port, function() {
    console.log('listening on localhost' +port);
});

这里是nginx配置:

location /testSocket {
                proxy_pass http://localhost:5555;
                 proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                # WebSocket support
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection $http_connection;
         }

在我的 index.html 文件中,我正在调用:

var ws = new WebSocket("wss://test.com/testSocket");

已经在这里呆了几个小时,但不确定此时我错过了什么。只是连接被拒绝。尝试对套接字使用 path 选项而不使用它。但是,导致同样的错误。

0 个答案:

没有答案
相关问题