Browse Source

ws测试3

master
wanghongjun 2 weeks ago
parent
commit
b5d2606fd7
  1. 29
      app/Events/SendAnnouncement.php

29
app/Events/SendAnnouncement.php

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

Loading…
Cancel
Save