所以我要在聊天应用中使用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]);
}
success()
,validationError()
和errorWithMessage()
等辅助方法的自定义控制器类如上面的代码所示
我用正确的值填充了$user
和$message
,该请求可以正常运行
我认为频道方法甚至不会被解雇,
当我在调用AppLog
方法时检查broadcast
表时,仅保存TherapyMessageSent
事件broadcastOn
函数中的日志
甚至我保存在channels.php
方法开头的日志也没有保存,因此我认为此方法永远不会执行。
如果有人可以帮助我解决这个问题,我将很感激。