【问题标题】:Binding callbacks on Laravel Echo with Laravel Websockets使用 Laravel Websockets 在 Laravel Echo 上绑定回调
【发布时间】:2020-06-02 14:11:31
【问题描述】:

我想知道我的用户何时遇到连接错误或连接中断。我读过很多书说这是可能的,但我不知道在哪里,我开始认为这可能是我正在使用的像 https://github.com/beyondcode/laravel-websockets 这样的第三方的问题,有人对此有任何问题吗?

以下根本不产生任何日志。

window.Echo.connector.pusher.bind('reconnect', (channel, data) => {
    console.log('RE-CONNECTED');
});

window.Echo.connector.pusher.bind('reconnecting', (channel, data) => {
    console.log('re-connecting...');
});

window.Echo.connector.pusher.bind('reconnect_error', (channel, data) => {
    console.log('reconnection error!');
});

window.Echo.connector.pusher.bind('reconnect_attempt', (channel, data) => {
    console.log('re-connecting...');
});

window.Echo.connector.pusher.bind('connect_error', (channel, data) => {
    console.log('connect error...');
});

window.Echo.connector.pusher.bind('error', (channel, data) => {
    console.log('error...');
});

window.Echo.connector.pusher.bind_global((channel, data) => {
    console.log(channel, data);
});

如果我使用这样的建议

window.Echo.connector.socket.on('connect_error', function(){
    console.log('connected', window.Echo.socketId());
});

window.Echo.connector.socket.on('connect', function(){
    console.log('connected', window.Echo.socketId());
});

window.Echo.connector.socket.on('disconnect', function(){
    console.log('disconnected');
});

window.Echo.connector.socket.on('reconnecting', function(attemptNumber){
    console.log('reconnecting', attemptNumber);
});

我收到Uncaught TypeError: Cannot read property 'on' of undefined

【问题讨论】:

    标签: laravel-echo laravel-websockets


    【解决方案1】:

    我发现了绑定事件的正确方法;

    window.Echo.connector.pusher.connection.bind($eventName, (payload) => {});
    

    事件参考可以在这里找到https://pusher.com/docs/channels/using_channels/connection

    window.Echo.connector.pusher.connection.bind('connecting', (payload) => {
    
        /**
         * All dependencies have been loaded and Channels is trying to connect.
         * The connection will also enter this state when it is trying to reconnect after a connection failure.
         */
    
        console.log('connecting...');
    
    });
    
    window.Echo.connector.pusher.connection.bind('connected', (payload) => {
    
        /**
         * The connection to Channels is open and authenticated with your app.
         */
    
        console.log('connected!', payload);
    });
    
    window.Echo.connector.pusher.connection.bind('unavailable', (payload) => {
    
        /**
         *  The connection is temporarily unavailable. In most cases this means that there is no internet connection.
         *  It could also mean that Channels is down, or some intermediary is blocking the connection. In this state,
         *  pusher-js will automatically retry the connection every 15 seconds.
         */
    
        console.log('unavailable', payload);
    });
    
    window.Echo.connector.pusher.connection.bind('failed', (payload) => {
    
        /**
         * Channels is not supported by the browser.
         * This implies that WebSockets are not natively available and an HTTP-based transport could not be found.
         */
    
        console.log('failed', payload);
    
    });
    
    window.Echo.connector.pusher.connection.bind('disconnected', (payload) => {
    
        /**
         * The Channels connection was previously connected and has now intentionally been closed
         */
    
        console.log('disconnected', payload);
    
    });
    
    window.Echo.connector.pusher.connection.bind('message', (payload) => {
    
        /**
         * Ping received from server
         */
    
        console.log('message', payload);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2021-03-12
      • 2020-09-15
      • 2022-01-25
      • 1970-01-01
      • 2021-03-21
      • 2020-07-23
      相关资源
      最近更新 更多