簡體   English   中英

Laravel Echo從未接受過活動

[英]Laravel Echo never recieving event

無論出於何種原因,我無法從laravel echo接收客戶端的任何數據。 我正在使用laravel-echo-server(socket.io),redis廣播公司和redis隊列。 據我所知,它們都是功能性的。 我會帶你了解我如何進行測試。 首先,我創建了一個UserCreated事件:

class UserCreated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function broadcastOn()
    {
        return new Channel('user-created');
    }

    public function broadcastAs()
    {
        return 'user.created';
    }
}

然后,為了測試此事件,我創建了一個CreateUser命令:

class CreateUser extends Command
{
    protected $signature = 'create:user {username}';

    protected $description = 'Create a new user!';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $user = \Chatter\User::create([
            'name' => $this->argument('username'),
            'email' => uniqid() . '@gmail.com',
            'password' => bcrypt('secret')
        ]);
        event(new \Chatter\Events\UserCreated($user));
    }
}

最后,這是我的laravel-echo-server.json:

{
    "authEndpoint": "/broadcasting/auth",
    "authHost": "chatter.dev",
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "localhost"
        }
    },
    "port": 6001,
    "protocol": "http",
    "sslCertPath": "",
    "sslKeyPath": "",
    "socketio": {}
}

接下來我運行php artisan queue:listen ,並laravel-echo-server start 他們都跑了沒有錯誤。 為了確保服務器工作,我做了php artisan create:user Bob123 它很成功。 隊列返回此(在控制台中):

[2017-06-01 01:28:27] Processing: Chatter\Events\UserCreated
[2017-06-01 01:28:27] Processed:  Chatter\Events\UserCreated

並且laravel-echo-server返回了這個(在控制台中):

CHANNEL user-created

因此,為了獲得我與用戶一起發送的數據,我創建了一個Vue組件,在掛載的函數中有一個echo偵聽器。 這是我的組件:

<template>
    <div class="container">
        <div class="row">

        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.');
            window.Echo.channel('user-created')
                .listen('.user.created', (data) => {
                    console.log(data);
                });
        },

        data(){
            return {
                messages: []
            }
        }
     }
</script>

當我刷新我的網頁時, Component mounted已記錄到控制台,所以我確定它已加載。 但是,無論出於何種原因,當我發送CreateUser命令時,數據都不會記錄到瀏覽器控制台中。

以下是您可能指出的所有內容:

我在我的.env廣播驅動程序更改為redis

BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

我還在app.php配置中取消注釋了BroadcastingServiceProvider。

我現在想不出任何其他事情,你可能會指出我已經完成了,但如果我記得其他人,我會更新問題。

謝謝你的幫助。

注意:我對Redis並不熟悉,所以目前我傾向於認為這是我的問題。 簡單地添加predis\\predis作為依賴只是不覺得我只需要這樣做。 希望這個說明有所幫助。

編輯:

正如所建議的那樣,我在運行laravel-echo-server之前嘗試運行redis-server -server。 這是在控制台中返回的內容:

vagrant@homestead:~/Code/Chatter$ redis-server
16342:C 01 Jun 05:45:38.066 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
16342:M 01 Jun 05:45:38.067 * Increased maximum number of open files to 10032 (it was originally set to 1024).
16342:M 01 Jun 05:45:38.067 # Creating Server TCP listening socket *:6379: bind: Address already in use

我認為這意味着redis服務器正在運行,所以我猜想宅基地默認自動啟動它。

Github存儲庫:

對不起,這花了這么久,我個人非常忙。 這是該項目的github存儲庫的鏈接。 如果對存儲庫有任何疑問,請告訴我。

https://github.com/Dastur1970/laravel-echo-test

編輯2:

萬一有人需要它,這是我用來創建我的Echo實例的代碼。

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
    // auth: {
    //     headers: {
    //         'Authorization': 'bf242984230eb684 1076b91e572e5bbcc81a852471364c49'
    //     }
    // },
    // key: '1076b91e572e5bbcc81a852471364c497',
    // csrfToken: token.content
});

注釋掉了我正在測試的一些東西。 不確定我是否會再次需要,所以我只是讓他們評論。 如果任何評論的代碼可能實際上有用,請告訴我。

好的,讓我們找出錯誤可能出現的部分:

這是服務器正在運行的隊列

  1. 事件
  2. 傾聽者
  3. Redis的
  4. Laravel回波 - 服務器
  5. 客戶

[2017-06-01 01:28:27]已處理:Chatter \\ Events \\ UserCreated

聽眾成功處理了該事件(步驟2)

CHANNEL由用戶創建

事件被寫入數據庫,laravel-echo-server得到它(步驟4)

通常,當新用戶加入頻道時, laravel-echo-server打印一條消息。 我在你的代碼中看不到這些消息,所以laravel-echo-server和客戶端之間的連接可能就是問題所在。 另外,我沒有看到任何使用Javascript在客戶端上創建Echo對象的代碼。

我已經在我的本地PC上設置了你的GIT倉庫並得到了問題。

您正在使用名為“Chatter”的自定義命名空間,因此您已在JS代碼中明確定義了該命名空間,以便Echo可以知道如何查找該Event類。

所以只需在public / js / app.js中用“.Chatter.Events.UserCreated”更改“UserCreated”,你就可以了:

mounted: function mounted() {
        console.log('Component mounted.');
        console.log(window.Echo);
        window.Echo.channel('user-created').listen('.Chatter.Events.UserCreated', function (data) {
            console.log('tester123');
        });
    },

更多信息: https//laravel.com/docs/master/broadcasting#namespaces

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM