首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel-echo是如何推动和反应js的?

laravel-echo是如何推动和反应js的?
EN

Stack Overflow用户
提问于 2019-06-16 09:36:44
回答 2查看 5K关注 0票数 2

我已经用laravel passport创建了REST,并将它们放在我的子域中,即"api.abc.com“。我有一个连接到它的react js应用程序。现在,为了添加实时消息,我使用了"pusher“和"laravel echo”。

React应用程序可以成功连接到推流器,我每次都可以在调试控制台上看到这一点。在我发送消息的地方,它也会存储在数据库和推送器上。但是相应的laravel回声通道监听器没有被触发。我尝试了所有的变体,但都不能让它工作。

REST API放置在服务器上,React应用程序当前在本地托管。这就是为什么我将"encrypted“设置为"false”。

APP//Events//NewMessage

代码语言:javascript
复制
use App\Message;
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;

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

    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('messages.' .  $this->message->conversation_id);
    }

    public function broadcastWith()
    {
        return ["message" => $this->message];
    }

    public function broadcastAs()
    {
        return 'chat';
    }
}

在消息存储函数中注册事件

代码语言:javascript
复制
broadcast(new NewMessage($msg));

config//broadcasting.php

代码语言:javascript
复制
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => false,
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

routes//channels.php

代码语言:javascript
复制
<?php

use App\Conversation;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

Broadcast::channel('messages.{id}', function ($user, $id) {
    $con = Conversation::findOrFail($id);
    // return $user->id == $con->user_id || $user->id == $con->shop_owner_id;
    return true;
});

来自我的REACT应用程序

代码语言:javascript
复制
import Pusher from "pusher-js";
import Echo from "laravel-echo";
import Cookies from "universal-cookie";

const cookies = new Cookies();

const options = {
    broadcaster: "pusher",
    key: "d4b9af39550bd7832778",
    cluster: "ap2",
    forceTLS: true,
    encrypted: false,
    //authEndpoint is your apiUrl + /broadcasting/auth
    authEndpoint: "https://api.abc.com/broadcasting/auth",
    // As I'm using JWT tokens, I need to manually set up the headers.
    auth: {
        headers: {
            "X-CSRF-TOKEN": csrf_token,
            Authorization: "Bearer " + cookies.get("access_token"),
            Accept: "application/json"
        }
    }
};

const echo = new Echo(options);

在selectConversation函数中,我得到了以下代码

代码语言:javascript
复制
echo.private("messages." + id).listen(".chat", data => {
            console.log("rumman");
            console.log(data);
        });

我也有以下几点意见:

代码语言:javascript
复制
echo.private("private-messages." + id).listen(".chat", data => {
            console.log("rumman");
            console.log(data);
        });

但是我从来没有看到过任何console.log数据。

EN

回答 2

Stack Overflow用户

发布于 2019-09-13 03:08:44

ShouldBroadcast更改为ShouldBroadcastNow

您的邮件正在排队。

票数 2
EN

Stack Overflow用户

发布于 2019-08-11 06:48:36

Laravel发送广播到队列的事件。因此,您需要设置某种形式的队列驱动程序。理想情况下,我将使用Redis作为我的队列驱动程序,并且可以通过运行这个命令php artisan queue:work --tries=3来跟踪正在调度的事件

添加的编辑:https://laravel.com/docs/7.x/queues#max-job-attempts-and-timeout

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56615440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档