1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Messages; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Ratchet\ConnectionInterface; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
class PusherChannelProtocolMessage extends PusherClientMessage |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Respond with the payload. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
|
public function respond() |
17
|
|
|
{ |
18
|
|
|
$eventName = Str::camel(Str::after($this->payload->event, ':')); |
19
|
|
|
|
20
|
|
|
if (method_exists($this, $eventName) && $eventName !== 'respond') { |
21
|
|
|
call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass()); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Ping the connection. |
27
|
|
|
* |
28
|
|
|
* @see https://pusher.com/docs/pusher_protocol#ping-pong |
29
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected function ping(ConnectionInterface $connection) |
33
|
|
|
{ |
34
|
|
|
$connection->send(json_encode([ |
35
|
|
|
'event' => 'pusher:pong', |
36
|
|
|
])); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Subscribe to channel. |
41
|
|
|
* |
42
|
|
|
* @see https://pusher.com/docs/pusher_protocol#pusher-subscribe |
43
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
44
|
|
|
* @param \stdClass $payload |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
protected function subscribe(ConnectionInterface $connection, stdClass $payload) |
48
|
|
|
{ |
49
|
|
|
$this->channelManager->subscribeToChannel($connection, $payload->channel, $payload); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Unsubscribe from the channel. |
54
|
|
|
* |
55
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
56
|
|
|
* @param \stdClass $payload |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function unsubscribe(ConnectionInterface $connection, stdClass $payload) |
60
|
|
|
{ |
61
|
|
|
$this->channelManager->unsubscribeFromChannel($connection, $payload->channel, $payload); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|