|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ekinhbayar\GitAmp\Websocket; |
|
4
|
|
|
|
|
5
|
|
|
use Aerys\Request; |
|
6
|
|
|
use Aerys\Response; |
|
7
|
|
|
use Aerys\Websocket; |
|
8
|
|
|
use Aerys\Websocket\Endpoint; |
|
9
|
|
|
use ekinhbayar\GitAmp\Client\GitAmp; |
|
10
|
|
|
use ekinhbayar\GitAmp\Response\Results; |
|
11
|
|
|
use ekinhbayar\GitAmp\Storage\Counter; |
|
12
|
|
|
use function Amp\repeat; |
|
13
|
|
|
|
|
14
|
|
|
class Handler implements Websocket |
|
15
|
|
|
{ |
|
16
|
|
|
private $endpoint; |
|
17
|
|
|
|
|
18
|
|
|
private $counter; |
|
19
|
|
|
|
|
20
|
|
|
private $origin; |
|
21
|
|
|
|
|
22
|
|
|
private $gitamp; |
|
23
|
|
|
|
|
24
|
2 |
|
public function __construct(Counter $counter, string $origin, GitAmp $gitamp) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$this->origin = $origin; |
|
27
|
2 |
|
$this->counter = $counter; |
|
28
|
2 |
|
$this->gitamp = $gitamp; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function onStart(Endpoint $endpoint) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->endpoint = $endpoint; |
|
34
|
|
|
|
|
35
|
|
|
$this->counter->set('connected_users', 0); |
|
36
|
|
|
|
|
37
|
|
|
repeat(function() { |
|
38
|
|
|
$this->emit(yield $this->gitamp->listen()); |
|
39
|
|
|
}, 25000); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function onHandshake(Request $request, Response $response) |
|
43
|
|
|
{ |
|
44
|
2 |
|
if ($request->getHeader('origin') !== $this->origin) { |
|
45
|
1 |
|
$response->setStatus(403); |
|
46
|
1 |
|
$response->end('<h1>origin not allowed</h1>'); |
|
47
|
|
|
|
|
48
|
1 |
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return $request->getConnectionInfo()['client_addr']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function onOpen(int $clientId, $handshakeData) |
|
55
|
|
|
{ |
|
56
|
|
|
// send initial results |
|
57
|
|
|
$this->emit(yield $this->gitamp->listen()); |
|
58
|
|
|
|
|
59
|
|
|
yield $this->counter->increment('connected_users'); |
|
60
|
|
|
|
|
61
|
|
|
$this->sendConnectedUsersCount(yield $this->counter->get('connected_users')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function emit(Results $events) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$events->hasEvents()) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->endpoint->send(null, $events->jsonEncode()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function sendConnectedUsersCount(int $count) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->endpoint->send(null, \json_encode(['connectedUsers' => $count])); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function onData(int $clientId, Websocket\Message $msg) |
|
79
|
|
|
{ |
|
80
|
|
|
// yielding $msg buffers the complete payload into a single string. |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function onClose(int $clientId, int $code, string $reason) |
|
84
|
|
|
{ |
|
85
|
|
|
yield $this->counter->decrement('connected_users'); |
|
86
|
|
|
|
|
87
|
|
|
$this->sendConnectedUsersCount(yield $this->counter->get('connected_users')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function onStop() |
|
91
|
|
|
{ |
|
92
|
|
|
// intentionally left blank |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|