Laravel广播频道功能不会触发

时间:2019-05-11 20:04:58

标签: php laravel websocket pusher laravel-broadcast

所以我要在聊天应用中使用laravel广播

我遵循了Laravel Broadcasting的方法,

  • 未注释的App\Providers\BroadcastServiceProvider 来自providers内的config/app.php数组

  • 在pusher网站中注册,创建了一个频道 并用我的推送程序频道信息

  • 填充.env文件中下面的字段
PUSHER_APP_ID
PUSHER_APP_KEY
PUSHER_APP_SECRET
PUSHER_APP_CLUSTER
  • 在我的broadcast.php配置文件中,我将默认driver设置为pusher,同时还添加了
'options' => [
    'cluster' => 'us2',
    'encrypted' => true,
],
根据我在推送面板中的频道信息,

移至pusher数组中的connections数组

  • 使用composer require pusher/pusher-php-server "~3.0"命令在我的laravel项目中安装了pusher php软件包

这是我的活动课

<?php

namespace App\Events;
use App\User;
use App\TherapyMessage;
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 App\AppLog;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

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

    /**
     * User that sent the message
     *
     * @var User
     */
    public $user;

    /**
     * Message details
     *
     * @var Message
     */
    public $message;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        $message_id = $this->message->id;
        $user = $this->user;

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "broadcast before send with Message ID of $message_id from $user->full_name"
        ]);

        return new PrivateChannel("therapy-chat.$message_id");
    }
}

  • AppLog是一种用于在项目内部记录日志的模型
  • 我一开始尝试实现ShouldBroadcast接口,但这也不起作用
  • 我还在EventServiceProvider.php文件中注册了我的事件并运行php artisan event:generate命令,这是EventServiceProvider $listen数组:
protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
        TherapyMessageSent::class
    ],
];

我还将事件名称空间导入到文件中其他名称空间的旁边: use \App\Events\TherapyMessageSent;

这是我在routes/channels.php文件中定义的频道:


use App\AppLog;

Broadcast::channel('therapy-chat.{message_id}', function ($user, $message_id) {

    AppLog::create([
        'file_name' => __FILE__,
        'message' => "broadcast sending with Message ID of $message_id to $user->full_name"
    ]);

    if (!Auth::check()) return false;

    $message = \App\TherapyMessage::find($message_id);

    if (!$message) {

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "Message with ID of $message_id Not Found for broadcasting"
        ]);

        return false;
    }

    $will_send = false;

    if ($therapist = $user->therapist) {
        $will_send = $therapist->id === $message->therapist_id;
    } else if ($patient = $user->patient) {
        $will_send = $message->patient_id === $patient->id;
    }

    if ($will_send) {

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "Message with ID of $message_id broadcasted to $user->full_name"
        ]);
    }

    return $will_send;
});

最后,这是我的控制器方法:

public function sendToTherapist(Request $request) {

    $validation = \Validator::make($request->all(), ['message' => 'required']);

    if ($validation->fails()) return $this->validationError($validation);

    $user = \Auth::user();
    $patient = $user->patient;
    $therapist = $patient->therapist;

    if (!$therapist) return $this->errorWithMessage('Patient does not have Therapist');

    $message = \App\TherapyMessage::create([
        'patient_id' => $patient->id,
        'therapist_id' => $therapist->id,
        'type' => TherapyMessageType::TEXT,
        'text' => $request->message,
        'sender_role' => TherapyMessageSenderRole::PATIENT
    ]);

    broadcast(new TherapyMessageSent($user, $message))->toOthers();

    return $this->success(['id' => $message->id]);
}
  • 我的控制器类从BaseController扩展而来,BaseController是一个具有success()validationError()errorWithMessage()等辅助方法的自定义控制器类

如上面的代码所示 我用正确的值填充了$user$message,该请求可以正常运行

我认为频道方法甚至不会被解雇, 当我在调用AppLog方法时检查broadcast表时,仅保存TherapyMessageSent事件broadcastOn函数中的日志 甚至我保存在channels.php方法开头的日志也没有保存,因此我认为此方法永远不会执行。

如果有人可以帮助我解决这个问题,我将很感激。

0 个答案:

没有答案