【问题标题】:Can't connect to Socket.io Error: xhr poll error无法连接到 Socket.io 错误:xhr poll 错误
【发布时间】:2017-12-14 04:02:24
【问题描述】:

您好,我正在使用 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 文件:

感谢任何帮助...

【问题讨论】:

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


    【解决方案1】:

    这是广告拦截器

    为此页面禁用了广告拦截器,它现在可以工作了。

    愚蠢的…………

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 2021-10-07
      • 2021-11-11
      • 2014-12-27
      • 2017-08-28
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 2017-01-08
      相关资源
      最近更新 更多