1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: shanmaseen |
5
|
|
|
* Date: 26/03/19 |
6
|
|
|
* Time: 12:09 م |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Laravel\Ratchet\Traits; |
10
|
|
|
use Shamaseen\Laravel\Ratchet\Objects\Rooms\Room; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
trait RoomUtility |
14
|
|
|
{ |
15
|
|
|
function addMember($room_id) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** @var Room $room */ |
18
|
|
|
$room = $this->receiver->rooms[$room_id]; |
|
|
|
|
19
|
|
|
$client = $this->clients[$this->userAuthSocketMapper[\Auth::id()]]; |
|
|
|
|
20
|
|
|
$room->addMember($client); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This function will automatically remove the room if no member still on it. |
25
|
|
|
* @param $room_id |
26
|
|
|
*/ |
27
|
|
|
function removeMember($room_id) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** @var Room $room */ |
30
|
|
|
$room = $this->receiver->rooms[$room_id]; |
31
|
|
|
$client = $this->clients[$this->userAuthSocketMapper[\Auth::id()]]; |
32
|
|
|
$room->removeMember($client); |
33
|
|
|
|
34
|
|
|
if(count($room->members) == 0) |
35
|
|
|
{ |
36
|
|
|
unset($this->receiver->rooms[$room_id]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $room_id |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
function hasMember($room_id) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
/** @var Room $room */ |
48
|
|
|
$room = $this->receiver->rooms[$room_id]; |
49
|
|
|
$client = $this->clients[$this->userAuthSocketMapper[\Auth::id()]]; |
50
|
|
|
return $room->hasMember($client); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $room_id |
55
|
|
|
* @param bool $createIfNotExist |
56
|
|
|
* @return Room |
57
|
|
|
*/ |
58
|
|
|
function validateRoom($room_id,$createIfNotExist = false) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if(!array_key_exists($room_id,$this->receiver->rooms)) |
61
|
|
|
{ |
62
|
|
|
if($createIfNotExist) |
63
|
|
|
{ |
64
|
|
|
$room = $this->receiver->rooms[$room_id] = new Room($room_id); |
65
|
|
|
return $room; |
66
|
|
|
} |
67
|
|
|
$this->error($this->request,$this->conn,'Room is not exist'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $room_id |
73
|
|
|
* @param $message |
74
|
|
|
*/ |
75
|
|
|
function sendToRoom($room_id, $message) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->validateRoom($room_id); |
78
|
|
|
/** @var Room $room */ |
79
|
|
|
$room = $this->rooms[$room_id]; |
|
|
|
|
80
|
|
|
$client = $this->clients[$this->userAuthSocketMapper[\Auth::id()]]; |
81
|
|
|
if(!$this->hasMember($client)) |
82
|
|
|
$this->error($this->request,$this->conn,'You can\'t send a message to room which you are not in !'); |
|
|
|
|
83
|
|
|
|
84
|
|
|
foreach ($room->members as $member) |
85
|
|
|
{ |
86
|
|
|
$this->sendToUser($member->id,$message); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.