node.js - 无法连接到 Socket.io 错误 : xhr poll error

标签 node.js socket.io laravel-5.3 homestead

您好,我正在使用 Homestead 和 Laravel 应用程序。

我无法让客户端从服务器接收数据...

我的socket.js:

var server = require('http').Server();

var io = require('socket.io')(server);

var Redis = require('ioredis');
var redis = new Redis();


redis.subscribe('test-channel');

redis.on('message', function (channel, message) {
    message = JSON.parse(message);

    io.emit(channel + ':' + message.event, message.data);
});


/*Booting Up the Server : port 3000 */
server.listen(3000 , function(){
    console.log('The Server Is Running');
});

这正在监听正在运行的端口 3000。 控制台输出:

vagrant@homestead:~/code/chatting-app$ nodemon -L socket.js 
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node socket.js`
The Server Is Running

在客户端,我也在监听端口 3000。

我使用一段代码来显示错误:

function checkSocketIoConnect(url, timeout) {
        return new Promise(function(resolve, reject) {
            var errAlready = false;
            timeout = timeout || 5000;
            var socket = io(url, {reconnection: false, timeout: timeout});

            // success
            socket.on("connect", function() {
                clearTimeout(timer);
                resolve();
                socket.close();
            });

            // set our own timeout in case the socket ends some other way than what we are listening for
            var timer = setTimeout(function() {
                timer = null;
                error("local timeout");
            }, timeout);

            // common error handler
            function error(data) {
                if (timer) {
                    clearTimeout(timer);
                    timer = null;
                }
                if (!errAlready) {
                    errAlready = true;
                    reject(data);
                    socket.disconnect();
                }
            }

            // errors
            socket.on("connect_error", error);
            socket.on("connect_timeout", error);
            socket.on("error", error);
            socket.on("disconnect", error);
        });
    }

    checkSocketIoConnect("http://192.168.10.10:3000").then(function() {
        console.log('succes');
    }, function(reason) {
        console.log(reason);
    });

但这会显示以下错误:

Error: xhr poll error
Stacktrace:
[14]</n.prototype.onError@https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js:1:24221
[17]</</o.prototype.doPoll/<@https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js:1:29697
[9]</n.prototype.emit@https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js:1:13388
[17]</</i.prototype.onError@https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js:1:31004

我不知道该怎么办?!我尝试了我能想到的一切。

我的 homestead.Yaml 文件:

enter image description here

感谢任何帮助...

最佳答案

这是广告拦截器

已为此页面禁用广告拦截器,现在可以使用。

笨蛋......

关于node.js - 无法连接到 Socket.io 错误 : xhr poll error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011713/

相关文章:

javascript - 如何使用socket_id单独发送给客户端?

python - Socket.io 与 flask-socketio python。如何设置套接字保活/超时

php - Laravel Carbon 本地化不起作用(从数字中获取本地化的月份名称)

php - Laravel:如何优化多个查询

sql - GraphQL 和 SQL - 是否可以通过 1 个 SQL JOIN 减少数据库调用?

angularjs - E/configParser - 错误消息 : configuration file cli. js 未导出配置对象

node.js - Mongoose 找到所有不发送回调

node.js - 如何使用 couchnode 设置 CouchBase 的过期时间

node.js - socketio Node 为另一个命名空间发出

php - 如何设置 laravel 5.3 注销重定向路径?