|
|
|
@ -2,7 +2,6 @@ |
|
|
|
|
|
|
|
namespace App\Events; |
|
|
|
|
|
|
|
use Carbon\Carbon; |
|
|
|
use Illuminate\Broadcasting\Channel; |
|
|
|
use Illuminate\Broadcasting\InteractsWithSockets; |
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
|
|
|
@ -13,36 +12,27 @@ class SendAnnouncement implements ShouldBroadcast |
|
|
|
{ |
|
|
|
use Dispatchable, InteractsWithSockets, SerializesModels; |
|
|
|
|
|
|
|
public string $message; |
|
|
|
public string $broadcastTime; |
|
|
|
public $message; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new event instance. |
|
|
|
*/ |
|
|
|
public function __construct(string $message) { |
|
|
|
public function __construct($message) |
|
|
|
{ |
|
|
|
$this->message = $message; |
|
|
|
$this->broadcastTime = Carbon::now()->toDateTimeString(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the channels the event should broadcast on. |
|
|
|
* |
|
|
|
* @return array<int, Channel> |
|
|
|
*/ |
|
|
|
public function broadcastOn(): array { |
|
|
|
// 定义事件要广播到的公共频道 |
|
|
|
public function broadcastOn(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
new Channel('channel-pub'), |
|
|
|
new Channel('test-channel'), // 公共频道 |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public function broadcastWith(): array { |
|
|
|
// 可选:自定义广播数据 |
|
|
|
public function broadcastWith(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
'name' => 'MyTestEvent', |
|
|
|
'message' => $this->message, |
|
|
|
'broadcastTime' => $this->broadcastTime |
|
|
|
'time' => now()->toDateTimeString(), |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|