Passed
Push — master ( c03036...d048db )
by Shahrad
02:03
created

startTicker()   A

Complexity

Conditions 5
Paths 19

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
c 0
b 0
f 0
nc 19
nop 2
dl 0
loc 29
rs 9.3888
1
<?php
2
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
3
4
use EasyHttp\Exceptions\WebSocketException;
5
use EasyHttp\WebSocket;
6
use EasyHttp\WebSocketConfig;
7
8
9
$close_time = time() + 10;
10
$SocketClient = new WebSocket();
11
12
$SocketClient->onWhile = function (WebSocket $socket) use ($close_time) {
13
	if (time() >= $close_time) {
14
		$socket->close();
15
	}
16
};
17
18
$SocketClient->onOpen = function (WebSocket $socket) {
19
	echo sprintf(
20
		'<pre><b>%s</b>: Connected to %s</pre><br/>',
21
		date('Y-m-d H:i:s'),
22
		$socket->getSocketUrl()
23
	);
24
25
	$socket->send(json_encode([
26
		'method' => 'subscribe',
27
		'id' => 'price',
28
		'data' => [
29
			'cryptoIds' => [1, 1027, 825, 3408, 1839, 4687, 52, 2010, 5426],
30
			'index' => 'detail'
31
		]
32
	]));
33
};
34
35
$SocketClient->onClose = function (WebSocket $socket, int $closeStatus) {
36
	echo sprintf(
37
		'<pre><b>%s</b>: Disconnected with status: %s</pre><br/>',
38
		date('Y-m-d H:i:s'),
39
		$closeStatus
40
	);
41
};
42
43
$SocketClient->onMessage = function (WebSocket $socket, string $message) {
44
	$data = json_decode($message, true);
45
	if (isset($data['id']) && $data['id'] == "price") {
46
		echo sprintf(
47
			'<pre><b>%s</b>: %s</pre><br/>',
48
			date('Y-m-d H:i:s'),
49
			$message
50
		);
51
	}
52
};
53
54
$SocketClient->onError = function (WebSocket $socket, WebSocketException $exception) {
55
	echo sprintf(
56
		"<pre>%s: Error: %s<br>File: %s:%s<br></pre><br>",
57
		date('Y-m-d H:i:s'),
58
		$exception->getMessage(),
59
		$exception->getFile(),
60
		$exception->getLine()
61
	);
62
};
63
64
$SocketClient->connect(
65
	'wss://stream.coinmarketcap.com/price/latest',
66
	(new WebSocketConfig())->setFragmentSize(8096)->setTimeout(15)
67
);