| Conditions | 5 |
| Paths | 19 |
| Total Lines | 29 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | function startTicker(array $ids, callable $callback): void |
||
| 15 | { |
||
| 16 | try { |
||
| 17 | |||
| 18 | $close_time = time() + 10; |
||
| 19 | $ClientConfig = (new WSConfig())->setFragmentSize(8096)->setTimeout(15); |
||
| 20 | $WebSocketClient = new WebSocket('wss://stream.coinmarketcap.com/price/latest', $ClientConfig); |
||
| 21 | |||
| 22 | $WebSocketClient->send(json_encode([ |
||
| 23 | 'method' => 'subscribe', |
||
| 24 | 'id' => 'price', |
||
| 25 | 'data' => [ |
||
| 26 | 'cryptoIds' => $ids, |
||
| 27 | 'index' => 'detail' |
||
| 28 | ] |
||
| 29 | ])); |
||
| 30 | |||
| 31 | while ($close_time > time()) { |
||
| 32 | if (($message = $WebSocketClient->receive()) != "") { |
||
| 33 | $json_response = json_decode($message, true); |
||
| 34 | |||
| 35 | if ($json_response['id'] == "price") { |
||
| 36 | $callback($json_response); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | } catch (Exception $e) { |
||
| 42 | echo "<b>Error</b>: " . $e->getMessage(); |
||
| 43 | } |
||
| 54 | echo "<pre>End Time: " . date('Y-m-d H:i:s') . "</pre>"; |