laravel广播redis socket-io,不侦听事件

x759pob2  于 2022-10-31  发布在  Redis
关注(0)|答案(3)|浏览(167)

我正在使用这个教程的laravel广播与redis和socket-io,https://www.itsolutionstuff.com/post/laravel-broadcast-redis-socket-io-tutorial-example.html
但在最终结果中,我在页面上没有得到任何消息,实际上它在触发时没有侦听事件。
Redis输出

1622090795.392138 [0 127.0.0.1:40302] "SELECT" "0"
1622090795.392327 [0 127.0.0.1:40302] "EVAL" "for i = 2, #ARGV do\n  redis.call('publish', ARGV[i], ARGV[1])\nend" "0" "{\"event\":\"UserEvent\",\"data\":{\"title\":\"This notification from ItSolutionStuff.com\",\"socket\":null},\"socket\":null}" "user-channel"
1622090795.392370 [0 lua] "publish" "user-channel" "{\"event\":\"UserEvent\",\"data\":{\"title\":\"This notification from ItSolutionStuff.com\",\"socket\":null},\"socket\":null}"

Laravel Echo服务器

pc@pc-ThinkPad-T440:/opt/lampp/htdocs/app$ laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.6.2

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

Channel: user-channel
Event: UserEvent

Laravel Echo setup.js文件

import Echo from 'laravel-echo';
window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ":" + 6001
    // host: window.location.hostname + ":" + window.laravel_echo_port
});

我的Welcome blade文件

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>Laravel Broadcast Redis Socket io Tutorial - ItSolutionStuff.com</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <h1>Laravel Broadcast Redis Socket io Tutorial - ItSolutionStuff.com</h1>

            <div id="notification"></div>
        </div>
    </body>

    <script>
            window.laravel_echo_port='{{env("LARAVEL_ECHO_PORT")}}';
    </script>
    <script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
    <script src="{{ url('/js/laravel-echo-setup.js') }}" type="text/javascript"></script>

    <script type="text/javascript">
        var i = 0;
        console.log(Echo);
        Echo.channel('user-channel')
         .listen('.UserEvent', (data) => {
            i++;
            console.log(data);
            $("#notification").append('<div class="alert alert-success">'+i+'.'+data.title+'</div>');
        });
    </script>
</html>

和SendMessage.php事件文件,

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class SendMessage implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */

    public $data = ['asas'];
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('user-channel');
        // return new PrivateChannel('channel- name');
    }
    public function broadcastAs()
    {
        return 'UserEvent';
    }
    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastWith()
    {
        return ['title'=>'This notification from ItSolutionStuff.com'];
    }
}

而我使用的路线

Route::get('/t', function () {
    event(new \App\Events\SendMessage());
    dd('Event Run Successfully.');
});
Route::get('/', function () {
    return view('welcome');
});

我的config/database.php看起来和教程中提到的一样

'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', ''),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],

第一次

fae0ux8s

fae0ux8s1#

REDIS_PORT下的laravel config/database.php文件中,使用您正在与redis服务器一起使用的端口40302

v7pvogib

v7pvogib2#

在package.json套接字中,io-client可以是工作版本

"socket.io-client": "^2.3.0"

相关问题