1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp; |
4
|
|
|
|
5
|
|
|
use EasyHttp\Contracts\CommonsContract; |
6
|
|
|
use EasyHttp\Contracts\MessageContract; |
7
|
|
|
use EasyHttp\Contracts\WebSocketContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SocketClient class |
11
|
|
|
* |
12
|
|
|
* @method void send($payload, string $opcode = CommonsContract::EVENT_TYPE_TEXT) |
13
|
|
|
* @method bool|null|string close(int $status = 1000, string $message = 'ttfn') |
14
|
|
|
* @method void onOpen() |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/shahradelahi/easy-http |
17
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
18
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
19
|
|
|
*/ |
20
|
|
|
abstract class SocketClient implements WebSocketContract, MessageContract |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var WebSocket |
25
|
|
|
*/ |
26
|
|
|
protected WebSocket $websocket; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param WebSocket $websocket |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected function setConnection(WebSocket $websocket): void |
33
|
|
|
{ |
34
|
|
|
$this->websocket = $websocket; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* @param array $arguments |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function __call(string $name, array $arguments): mixed |
43
|
|
|
{ |
44
|
|
|
if (method_exists($this, $name)) { |
45
|
|
|
return call_user_func_array([$this, $name], $arguments); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (method_exists($this->websocket, $name)) { |
49
|
|
|
return call_user_func_array([$this->websocket, $name], $arguments); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new \BadMethodCallException(sprintf( |
53
|
|
|
'Method `%s` does not exist at `%s`', $name, get_class($this) |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|