Bläddra i källkod

Add stream start + end events

Daniel Supernault 3 år sedan
förälder
incheckning
a45deb93ed

+ 49 - 0
app/Events/LiveStream/StreamEnd.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Events\LiveStream;
+
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use App\Models\LiveStream;
+
+class StreamEnd implements ShouldBroadcast
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $livestream;
+
+    /**
+     * Create a new event instance.
+     *
+     * @return void
+     */
+    public function __construct(LiveStream $livestream)
+    {
+        $this->livestream = $livestream;
+    }
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('live.chat.' . $this->livestream->profile_id);
+    }
+
+    public function broadcastAs()
+    {
+        return 'stream.end';
+    }
+
+    public function broadcastWith()
+    {
+        return ['ts' => time() ];
+    }
+}

+ 49 - 0
app/Events/LiveStream/StreamStart.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Events\LiveStream;
+
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use App\Models\LiveStream;
+
+class StreamStart implements ShouldBroadcast
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $livestream;
+
+    /**
+     * Create a new event instance.
+     *
+     * @return void
+     */
+    public function __construct(LiveStream $livestream)
+    {
+        $this->livestream = $livestream;
+    }
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('live.chat.' . $this->livestream->profile_id);
+    }
+
+    public function broadcastAs()
+    {
+        return 'stream.start';
+    }
+
+    public function broadcastWith()
+    {
+        return ['ts' => time() ];
+    }
+}

+ 5 - 1
app/Http/Controllers/LiveStreamController.php

@@ -15,6 +15,8 @@ use App\Events\LiveStream\DeleteChatComment;
 use App\Events\LiveStream\BanUser;
 use App\Events\LiveStream\PinChatMessage;
 use App\Events\LiveStream\UnpinChatMessage;
+use App\Events\LiveStream\StreamStart;
+use App\Events\LiveStream\StreamEnd;
 
 class LiveStreamController extends Controller
 {
@@ -373,6 +375,8 @@ class LiveStreamController extends Controller
 		if($request->filled('name') && $token == false) {
 			$stream->live_at = now();
 			$stream->save();
+
+			StreamStart::dispatch($stream);
 			return [];
 		} else {
 			abort(400);
@@ -389,7 +393,7 @@ class LiveStreamController extends Controller
 		$name = $url['name'] ?? $request->input('name');
 
 		$stream = LiveStream::whereStreamId($name)->whereStreamKey($url['key'])->firstOrFail();
-
+		StreamEnd::dispatch($stream);
 		LiveStreamService::clearChat($stream->profile_id);
 
 		if(config('livestreaming.broadcast.delete_token_after_finished')) {