1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laratrade\GDAX\WebSocket; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
7
|
|
|
use Laratrade\GDAX\WebSocket\Contracts\Subscriber as SubscriberContract; |
8
|
|
|
use Psr\Log\LoggerInterface as LoggerContract; |
9
|
|
|
use Ratchet\Client\WebSocket; |
10
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface as MessageContract; |
11
|
|
|
use React\Promise\PromiseInterface as PromiseContract; |
12
|
|
|
|
13
|
|
|
class Subscriber implements SubscriberContract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The logger instance. |
17
|
|
|
* |
18
|
|
|
* @var LoggerContract |
19
|
|
|
*/ |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The dispatcher instance. |
24
|
|
|
* |
25
|
|
|
* @var DispatcherContract |
26
|
|
|
*/ |
27
|
|
|
protected $dispatcher; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The event mappings. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $events; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new subscriber instance. |
38
|
|
|
* |
39
|
|
|
* @param LoggerContract $logger |
40
|
|
|
* @param DispatcherContract $dispatcher |
41
|
|
|
* @param array $events |
42
|
|
|
*/ |
43
|
16 |
|
public function __construct(LoggerContract $logger, DispatcherContract $dispatcher, array $events) |
44
|
|
|
{ |
45
|
16 |
|
$this->logger = $logger; |
46
|
16 |
|
$this->dispatcher = $dispatcher; |
47
|
16 |
|
$this->events = $events; |
48
|
16 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle the connect event. |
52
|
|
|
* |
53
|
|
|
* @param WebSocket $webSocket |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
2 |
|
public function onConnect(WebSocket $webSocket): void |
58
|
|
|
{ |
59
|
2 |
|
$this->logger->info('websocket connect'); |
60
|
|
|
|
61
|
2 |
|
$webSocket->on('message', [$this, 'onMessage']); |
62
|
2 |
|
$webSocket->on('close', [$this, 'onDisconnect']); |
63
|
|
|
|
64
|
2 |
|
$webSocket->send(json_encode([ |
65
|
2 |
|
'type' => 'subscribe', |
66
|
|
|
'product_ids' => [], |
67
|
|
|
'channels' => [ |
68
|
|
|
'ticker', |
69
|
|
|
], |
70
|
|
|
])); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Handle the message event. |
75
|
|
|
* |
76
|
|
|
* @param MessageContract $message |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
4 |
|
public function onMessage(MessageContract $message): void |
81
|
|
|
{ |
82
|
4 |
|
$payload = json_decode($message->getPayload()); |
83
|
|
|
|
84
|
4 |
|
$this->logger->info('websocket message', compact('payload')); |
85
|
|
|
|
86
|
4 |
|
if (!isset($payload->type) || !isset($this->events[$payload->type])) { |
87
|
2 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$this->dispatcher->dispatch(new $this->events[$payload->type]($payload)); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Handle the disconnect event. |
95
|
|
|
* |
96
|
|
|
* @param int|null $code |
97
|
|
|
* @param string|null $reason |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
2 |
|
public function onDisconnect(int $code = null, string $reason = null): void |
102
|
|
|
{ |
103
|
2 |
|
$this->logger->warning('websocket disconnect', compact('code', 'reason')); |
104
|
2 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Handle the error event. |
108
|
|
|
* |
109
|
|
|
* @param Exception $exception |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
2 |
|
public function onError(Exception $exception): void |
114
|
|
|
{ |
115
|
2 |
|
$this->logger->error('websocket error', compact('exception')); |
116
|
2 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Register the listeners for the subscriber. |
120
|
|
|
* |
121
|
|
|
* @param PromiseContract $connection |
122
|
|
|
*/ |
123
|
2 |
|
public function subscribe(PromiseContract $connection): void |
124
|
|
|
{ |
125
|
2 |
|
$connection->then([$this, 'onConnect'], [$this, 'onError']); |
126
|
2 |
|
} |
127
|
|
|
} |
128
|
|
|
|