1 | <?php |
||
14 | class WebSocketClient extends StreamClient implements ReactiveInterface { |
||
15 | |||
16 | const STATE_HANDSHAKE = 0; |
||
17 | const STATE_OK = 1; |
||
18 | const STATE_CLOSE = 2; |
||
19 | |||
20 | /** |
||
21 | * @var FrameHandler |
||
22 | */ |
||
23 | protected $frameHandler; |
||
24 | |||
25 | /** |
||
26 | * @var FrameReader |
||
27 | */ |
||
28 | protected $frameReader; |
||
29 | |||
30 | /** |
||
31 | * @var HandShake |
||
32 | */ |
||
33 | protected $handshake; |
||
34 | |||
35 | /** |
||
36 | * @var WebSocketServer |
||
37 | */ |
||
38 | protected $server; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $state = self::STATE_HANDSHAKE; |
||
44 | |||
45 | /** |
||
46 | * @param resource $resource |
||
47 | * @param WebSocketServer $server |
||
48 | */ |
||
49 | public function __construct ($resource, WebSocketServer $server) { |
||
53 | |||
54 | /** |
||
55 | * Closes, optionally with a code and reason sent to the peer. |
||
56 | * |
||
57 | * https://tools.ietf.org/html/rfc6455#section-5.5.1 |
||
58 | * > The application MUST NOT send any more data frames after sending a |
||
59 | * > Close frame. |
||
60 | * > |
||
61 | * > After both sending and receiving a Close message, an endpoint |
||
62 | * > considers the WebSocket connection closed and MUST close the |
||
63 | * > underlying TCP connection. |
||
64 | * |
||
65 | * https://tools.ietf.org/html/rfc6455#section-7.4.2 |
||
66 | * > Status codes in the range 0-999 are not used. |
||
67 | * |
||
68 | * @param int|null $code Only used if `>= 1000` |
||
69 | * @param string $reason |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function close (int $code = null, string $reason = '') { |
||
85 | |||
86 | /** |
||
87 | * @return FrameHandler |
||
88 | */ |
||
89 | public function getFrameHandler (): FrameHandler { |
||
92 | |||
93 | /** |
||
94 | * @return FrameReader |
||
95 | */ |
||
96 | public function getFrameReader (): FrameReader { |
||
99 | |||
100 | /** |
||
101 | * @return HandShake |
||
102 | */ |
||
103 | public function getHandshake (): HandShake { |
||
106 | |||
107 | /** |
||
108 | * @return WebSocketServer |
||
109 | */ |
||
110 | public function getServer (): WebSocketServer { |
||
113 | |||
114 | /** |
||
115 | * @return int |
||
116 | */ |
||
117 | public function getState (): int { |
||
120 | |||
121 | /** |
||
122 | * @return bool |
||
123 | */ |
||
124 | final public function isOk (): bool { |
||
127 | |||
128 | /** |
||
129 | * Called when a complete binary payload is received. |
||
130 | * |
||
131 | * @param string $binary |
||
132 | */ |
||
133 | public function onBinary (string $binary): void { |
||
137 | |||
138 | /** |
||
139 | * WebSockets do not use the out-of-band channel. |
||
140 | * |
||
141 | * The RFC says the connection must be dropped if any unsupported activity occurs. |
||
142 | */ |
||
143 | final public function onOutOfBand (): void { |
||
146 | |||
147 | /** |
||
148 | * Delegates received data to handlers. |
||
149 | * |
||
150 | * @throws Exception |
||
151 | */ |
||
152 | public function onReadable (): void { |
||
184 | |||
185 | /** |
||
186 | * Called when the initial connection handshake succeeds and frame I/O can occur. |
||
187 | */ |
||
188 | protected function onStateOk (): void { |
||
191 | |||
192 | /** |
||
193 | * Called when a complete text payload is received. |
||
194 | * |
||
195 | * @param string $text |
||
196 | */ |
||
197 | public function onText (string $text): void { |
||
201 | |||
202 | } |