Laravel Echo如何处理连接,断开连接,重新连接等

时间:2018-03-14 03:49:07

标签: javascript laravel-5 socket.io vuejs2 laravel-echo

有没有人知道如何处理Laravel Echo上的连接,断开连接,重新连接等?

我正在使用VueJS btw

1 个答案:

答案 0 :(得分:3)

要与您建立联系,请执行以下操作:

从“ laravel-echo”导入Echo

在功能或负载中,然后执行以下操作:

                window.Echo = new Echo({
                    broadcaster: 'socket.io',
                    host: socketServerURL, //whatever url you need
                    auth: {headers: {Authorization: 'Bearer ' + Vue.auth.getToken() }}
                });

                window.Echo.connector.socket.on('connect', function(){
                    this.isConnected = true
                })

                window.Echo.connector.socket.on('disconnect', function(){
                    this.isConnected = false
                })

                window.Echo.private('contacts').listen('ContactUpdated', event => {
                    console.log(event)
                })