|
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
|
|
|
|
|
16
|
|
|
abstract class WebSocketsPublicChannel implements IWebSocketsChannelInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public const CHANNEL_ACCESS = 'public'; |
|
19
|
|
|
protected string $hostStream; |
|
20
|
|
|
protected array $topic; |
|
21
|
|
|
protected string $operation; |
|
22
|
|
|
protected string $streamType; |
|
23
|
|
|
protected Worker $worker; |
|
24
|
|
|
protected IChannelHandlerInterface $callback; |
|
25
|
|
|
protected IResponseDataInterface $response; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
IWebSocketArgumentInterface $argument, |
|
29
|
|
|
Credentials $credentials, |
|
30
|
|
|
IChannelHandlerInterface $callback |
|
31
|
|
|
) { |
|
32
|
|
|
$this->worker = new Worker(); |
|
33
|
|
|
$this->streamType = $argument->getStreamType(); |
|
34
|
|
|
$this->topic = $argument->getTopic(); |
|
35
|
|
|
$this->operation = $this->getOperation(); |
|
|
|
|
|
|
36
|
|
|
$this->hostStream = $credentials->getHost(); |
|
37
|
|
|
$this->callback = $callback; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function execute(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->worker->onWorkerStart = function () { |
|
46
|
|
|
$connection = new AsyncTcpConnection($this->hostStream.'/'.self::CHANNEL_ACCESS.'/'.$this->streamType); |
|
47
|
|
|
$connection->transport = 'ssl'; |
|
48
|
|
|
$connection->onConnect = function ($connection) { |
|
49
|
|
|
$connection->send(json_encode(["op" => $this->operation, "args" => $this->topic])); |
|
50
|
|
|
}; |
|
51
|
|
|
|
|
52
|
|
|
$callback = $this->callback; |
|
53
|
|
|
|
|
54
|
|
|
$connection->onMessage = function ($connection, $message) use ($callback) { |
|
55
|
|
|
$message = json_decode($message, true); |
|
56
|
|
|
|
|
57
|
|
|
// Default DTO |
|
58
|
|
|
$responseDto = $this->getResponseClassname(); |
|
59
|
|
|
|
|
60
|
|
|
if (isset($message['success']) && !$message['success']) { |
|
61
|
|
|
// If api return exception data |
|
62
|
|
|
$responseDto = ExceptionResponse::class; |
|
63
|
|
|
} else if (isset($message['success']) && $message['success']) { |
|
64
|
|
|
// If stream return connection data |
|
65
|
|
|
$responseDto = WebSocketConnectionResponse::class; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$dtoMessage = ResponseDtoBuilder::make($responseDto, $message); |
|
69
|
|
|
|
|
70
|
|
|
switch ($responseDto) { |
|
71
|
|
|
case WebSocketConnectionResponse::class: |
|
72
|
|
|
$callback->connectionHandle($dtoMessage, $connection); |
|
73
|
|
|
break; |
|
74
|
|
|
case ExceptionResponse::class: |
|
75
|
|
|
$callback->apiExceptionHandler($dtoMessage, $connection); |
|
76
|
|
|
break; |
|
77
|
|
|
default: |
|
78
|
|
|
$callback->handle($dtoMessage, $connection); |
|
79
|
|
|
} |
|
80
|
|
|
}; |
|
81
|
|
|
$connection->connect(); |
|
82
|
|
|
}; |
|
83
|
|
|
|
|
84
|
|
|
Worker::runAll(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
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