1 | <?php declare(strict_types = 1); |
||
15 | class Handler implements Websocket |
||
16 | { |
||
17 | private $endpoint; |
||
18 | |||
19 | private $counter; |
||
20 | |||
21 | private $origin; |
||
22 | |||
23 | private $gitamp; |
||
24 | |||
25 | 9 | public function __construct(Counter $counter, string $origin, GitAmp $gitamp) |
|
31 | |||
32 | 5 | public function onStart(Endpoint $endpoint) |
|
33 | { |
||
34 | 5 | $this->endpoint = $endpoint; |
|
35 | |||
36 | 5 | $this->counter->set(0); |
|
37 | |||
38 | 5 | asyncCall(function () { |
|
39 | 1 | while (true) { |
|
40 | 5 | $this->emit(yield $this->gitamp->listen()); |
|
41 | |||
42 | 2 | yield new Delayed(25000); |
|
43 | } |
||
44 | 5 | }); |
|
45 | } |
||
46 | |||
47 | 2 | public function onHandshake(Request $request, Response $response) |
|
58 | |||
59 | 1 | public function onOpen(int $clientId, $handshakeData) |
|
65 | |||
66 | 2 | private function emit(Results $events) |
|
67 | { |
||
68 | 2 | if (!$events->hasEvents()) { |
|
69 | 1 | return; |
|
70 | } |
||
71 | |||
72 | 1 | $this->endpoint->broadcast($events->jsonEncode()); |
|
73 | } |
||
74 | |||
75 | 3 | private function sendConnectedUsersCount(int $count) |
|
79 | |||
80 | public function onData(int $clientId, Websocket\Message $msg) |
||
84 | |||
85 | 2 | public function onClose(int $clientId, int $code, string $reason) |
|
91 | |||
92 | public function onStop() |
||
96 | } |
||
97 |