1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Channels; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\DashboardLogger; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature; |
7
|
|
|
use Ratchet\ConnectionInterface; |
8
|
|
|
use stdClass; |
9
|
|
|
|
10
|
|
|
class PresenceChannel extends PrivateChannel |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Subscribe to the channel. |
14
|
|
|
* |
15
|
|
|
* @see https://pusher.com/docs/pusher_protocol#presence-channel-events |
16
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
17
|
|
|
* @param \stdClass $payload |
18
|
|
|
* @return void |
19
|
|
|
* @throws InvalidSignature |
20
|
|
|
*/ |
21
|
|
|
public function subscribe(ConnectionInterface $connection, stdClass $payload) |
22
|
|
|
{ |
23
|
|
|
$this->verifySignature($connection, $payload); |
24
|
|
|
|
25
|
|
|
$this->saveConnection($connection); |
26
|
|
|
|
27
|
|
|
$this->channelManager->userJoinedPresenceChannel( |
|
|
|
|
28
|
|
|
$connection, |
29
|
|
|
$user = json_decode($payload->channel_data), |
30
|
|
|
$this->getName(), |
31
|
|
|
$payload |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->channelManager |
35
|
|
|
->getChannelMembers($connection->app->id, $this->getName()) |
|
|
|
|
36
|
|
|
->then(function ($users) use ($connection) { |
37
|
|
|
$hash = []; |
38
|
|
|
|
39
|
|
|
foreach ($users as $socketId => $user) { |
40
|
|
|
$hash[$user->user_id] = $user->user_info ?? []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$connection->send(json_encode([ |
44
|
|
|
'event' => 'pusher_internal:subscription_succeeded', |
45
|
|
|
'channel' => $this->getName(), |
46
|
|
|
'data' => json_encode([ |
47
|
|
|
'presence' => [ |
48
|
|
|
'ids' => collect($users)->map(function ($user) { |
49
|
|
|
return (string) $user->user_id; |
50
|
|
|
})->values(), |
51
|
|
|
'hash' => $hash, |
52
|
|
|
'count' => count($users), |
53
|
|
|
], |
54
|
|
|
]), |
55
|
|
|
])); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$memberAddedPayload = [ |
59
|
|
|
'event' => 'pusher_internal:member_added', |
60
|
|
|
'channel' => $this->getName(), |
61
|
|
|
'data' => $payload->channel_data, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$this->broadcastToEveryoneExcept( |
65
|
|
|
(object) $memberAddedPayload, $connection->socketId, |
|
|
|
|
66
|
|
|
$connection->app->id |
|
|
|
|
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_SUBSCRIBED, [ |
|
|
|
|
70
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
71
|
|
|
'channel' => $this->getName(), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Unsubscribe connection from the channel. |
77
|
|
|
* |
78
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function unsubscribe(ConnectionInterface $connection) |
82
|
|
|
{ |
83
|
|
|
parent::unsubscribe($connection); |
84
|
|
|
|
85
|
|
|
$this->channelManager |
86
|
|
|
->getChannelMember($connection, $this->getName()) |
87
|
|
|
->then(function ($user) use ($connection) { |
88
|
|
|
$user = @json_decode($user); |
89
|
|
|
|
90
|
|
|
if (! $user) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->channelManager->userLeftPresenceChannel( |
95
|
|
|
$connection, $user, $this->getName() |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$memberRemovedPayload = [ |
99
|
|
|
'event' => 'pusher_internal:member_removed', |
100
|
|
|
'channel' => $this->getName(), |
101
|
|
|
'data' => json_encode([ |
102
|
|
|
'user_id' => $user->user_id, |
103
|
|
|
]), |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$this->broadcastToEveryoneExcept( |
107
|
|
|
(object) $memberRemovedPayload, $connection->socketId, |
|
|
|
|
108
|
|
|
$connection->app->id |
|
|
|
|
109
|
|
|
); |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: