|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carpenstar\ByBitAPI\Core\Objects\WebSockets; |
|
4
|
|
|
|
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Auth\Credentials; |
|
6
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\ExceptionResponse; |
|
7
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\WebSockets\Entity\WebSocketConnectionResponse; |
|
8
|
|
|
use Workerman\Worker; |
|
|
|
|
|
|
9
|
|
|
use Workerman\Connection\AsyncTcpConnection; |
|
|
|
|
|
|
10
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder; |
|
11
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseDataInterface; |
|
12
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IChannelHandlerInterface; |
|
13
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IWebSocketArgumentInterface; |
|
14
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IWebSocketsChannelInterface; |
|
15
|
|
|
use Workerman\Lib\Timer; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
abstract class WebSocketsPublicChannel implements IWebSocketsChannelInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public const CHANNEL_ACCESS = 'public'; |
|
20
|
|
|
protected string $hostStream; |
|
21
|
|
|
protected array $topic; |
|
22
|
|
|
protected string $operation; |
|
23
|
|
|
protected string $streamType; |
|
24
|
|
|
protected Worker $worker; |
|
25
|
|
|
protected IChannelHandlerInterface $callback; |
|
26
|
|
|
protected IResponseDataInterface $response; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
IWebSocketArgumentInterface $argument, |
|
30
|
|
|
Credentials $credentials, |
|
31
|
|
|
IChannelHandlerInterface $callback |
|
32
|
|
|
) { |
|
33
|
|
|
$this->worker = new Worker(); |
|
34
|
|
|
$this->streamType = $argument->getStreamType(); |
|
35
|
|
|
$this->topic = $argument->getTopic(); |
|
36
|
|
|
$this->operation = $this->getOperation(); |
|
|
|
|
|
|
37
|
|
|
$this->hostStream = $credentials->getHost(); |
|
38
|
|
|
$this->callback = $callback; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function execute(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->worker->onWorkerStart = function () { |
|
47
|
|
|
|
|
48
|
|
|
$connection = new AsyncTcpConnection($this->hostStream.'/'.self::CHANNEL_ACCESS.'/'.$this->streamType); |
|
49
|
|
|
$connection->transport = 'ssl'; |
|
50
|
|
|
|
|
51
|
|
|
$heartbeatTimerId = Timer::add(20, function() use ($connection) { |
|
52
|
|
|
$connection->send(json_encode(["op" => "ping"])); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Обработка соединения |
|
57
|
|
|
* |
|
58
|
|
|
* @param $connection |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
$connection->onConnect = function ($connection) use (&$heartbeatTimerId) { |
|
|
|
|
|
|
62
|
|
|
$connection->send(json_encode(["op" => $this->operation, "args" => $this->topic])); |
|
63
|
|
|
}; |
|
64
|
|
|
|
|
65
|
|
|
$callback = $this->callback; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Обработка сообщений |
|
69
|
|
|
* |
|
70
|
|
|
* @param $connection |
|
71
|
|
|
* @param $message |
|
72
|
|
|
* @return void |
|
73
|
|
|
* @throws \Exception |
|
74
|
|
|
*/ |
|
75
|
|
|
$connection->onMessage = function ($connection, $message) use ($callback) { |
|
76
|
|
|
$message = json_decode($message, true); |
|
77
|
|
|
$responseDto = $this->getResponseClassname($this->streamType); |
|
78
|
|
|
|
|
79
|
|
|
if (isset($message['success']) && !$message['success']) { |
|
80
|
|
|
$responseDto = ExceptionResponse::class; |
|
81
|
|
|
} else if (isset($message['success']) && $message['success']) { |
|
82
|
|
|
$responseDto = WebSocketConnectionResponse::class; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$dtoMessage = ResponseDtoBuilder::make($responseDto, $message); |
|
86
|
|
|
|
|
87
|
|
|
switch ($responseDto) { |
|
88
|
|
|
case WebSocketConnectionResponse::class: |
|
89
|
|
|
$callback->connectionHandle($dtoMessage, $connection); |
|
90
|
|
|
break; |
|
91
|
|
|
case ExceptionResponse::class: |
|
92
|
|
|
$callback->apiExceptionHandler($dtoMessage, $connection); |
|
93
|
|
|
break; |
|
94
|
|
|
default: |
|
95
|
|
|
$callback->handle($dtoMessage, $connection); |
|
96
|
|
|
} |
|
97
|
|
|
}; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* При закрытии соединения удаляем таймер |
|
101
|
|
|
* |
|
102
|
|
|
* @param $connection |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
$this->worker->onClose = function ($connection) use (&$heartbeatTimerId) { |
|
106
|
|
|
Timer::del($heartbeatTimerId); |
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
$connection->connect(); |
|
110
|
|
|
}; |
|
111
|
|
|
|
|
112
|
|
|
Worker::runAll(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths