1 | <?php |
||
15 | class WebSocketClient extends StreamClient implements ReactiveInterface { |
||
16 | |||
17 | /** |
||
18 | * The peer has connected but hasn't negotiated a session yet. |
||
19 | */ |
||
20 | const STATE_HANDSHAKE = 0; |
||
21 | |||
22 | /** |
||
23 | * The session is active and the client can perform frame I/O with the peer. |
||
24 | */ |
||
25 | const STATE_OK = 1; |
||
26 | |||
27 | /** |
||
28 | * The peer has disconnected. |
||
29 | */ |
||
30 | const STATE_CLOSED = 2; |
||
31 | |||
32 | /** |
||
33 | * @var FrameHandler |
||
34 | */ |
||
35 | protected $frameHandler; |
||
36 | |||
37 | /** |
||
38 | * @var Handshake |
||
39 | */ |
||
40 | protected $handshake; |
||
41 | |||
42 | /** |
||
43 | * @var WebSocketServer |
||
44 | */ |
||
45 | protected $server; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $state = self::STATE_HANDSHAKE; |
||
51 | |||
52 | /** |
||
53 | * @param resource $resource |
||
54 | * @param WebSocketServer $server |
||
55 | */ |
||
56 | public function __construct ($resource, WebSocketServer $server) { |
||
62 | |||
63 | /** |
||
64 | * Closes, optionally with a code and reason sent to the peer. |
||
65 | * |
||
66 | * https://tools.ietf.org/html/rfc6455#section-5.5.1 |
||
67 | * > The application MUST NOT send any more data frames after sending a |
||
68 | * > Close frame. |
||
69 | * > |
||
70 | * > After both sending and receiving a Close message, an endpoint |
||
71 | * > considers the WebSocket connection closed and MUST close the |
||
72 | * > underlying TCP connection. |
||
73 | * |
||
74 | * https://tools.ietf.org/html/rfc6455#section-7.4.2 |
||
75 | * > Status codes in the range 0-999 are not used. |
||
76 | * |
||
77 | * @param int|null $code Sent to the peer if >= 1000 |
||
78 | * @param string $reason Sent to the peer, if code is >= 1000 |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function close (int $code = null, string $reason = '') { |
||
94 | |||
95 | /** |
||
96 | * @return FrameHandler |
||
97 | */ |
||
98 | public function getFrameHandler (): FrameHandler { |
||
101 | |||
102 | /** |
||
103 | * @return WebSocketServer |
||
104 | */ |
||
105 | public function getServer (): WebSocketServer { |
||
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | */ |
||
112 | public function getState (): int { |
||
115 | |||
116 | final public function isNegotiating (): bool { |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | final public function isOk (): bool { |
||
126 | |||
127 | /** |
||
128 | * Called when a complete `BINARY` payload is received from the peer. |
||
129 | * |
||
130 | * Throws by default. |
||
131 | * |
||
132 | * @param string $binary |
||
133 | * @throws WebSocketError |
||
134 | */ |
||
135 | public function onBinary (string $binary): void { |
||
139 | |||
140 | /** |
||
141 | * Called when a `CLOSE` frame is received from the peer. |
||
142 | * |
||
143 | * @param int $code |
||
144 | * @param string $reason |
||
145 | */ |
||
146 | public function onClose (int $code, string $reason): void { |
||
150 | |||
151 | /** |
||
152 | * WebSockets do not use the out-of-band channel. |
||
153 | * |
||
154 | * The RFC says the connection must be dropped if any unsupported activity occurs. |
||
155 | * |
||
156 | * Closes the connection with a protocol-error frame. |
||
157 | */ |
||
158 | final public function onOutOfBand (): void { |
||
161 | |||
162 | /** |
||
163 | * Called when a `PING` is received from the peer. |
||
164 | * |
||
165 | * Automatically PONGs back the payload back by default. |
||
166 | * |
||
167 | * @param string $message |
||
168 | */ |
||
169 | public function onPing (string $message): void { |
||
172 | |||
173 | /** |
||
174 | * Called when a `PONG` is received from the peer. |
||
175 | * |
||
176 | * Does nothing by default. |
||
177 | * |
||
178 | * @param string $message |
||
179 | */ |
||
180 | public function onPong (string $message): void { |
||
183 | |||
184 | /** |
||
185 | * Delegates the read-channel to handlers. |
||
186 | * |
||
187 | * @throws WebSocketError |
||
188 | * @throws Throwable |
||
189 | */ |
||
190 | public function onReadable (): void { |
||
211 | |||
212 | /** |
||
213 | * Called when the initial connection handshake succeeds and frame I/O can occur. |
||
214 | * |
||
215 | * Does nothing by default. |
||
216 | * |
||
217 | * If you have negotiated an extension during {@link Handshake}, |
||
218 | * claim the RSV bits here via {@link FrameReader::setRsv()} |
||
219 | */ |
||
220 | protected function onStateOk (): void { |
||
223 | |||
224 | /** |
||
225 | * Called when a complete `TEXT` payload is received from the peer. |
||
226 | * |
||
227 | * Throws by default. |
||
228 | * |
||
229 | * @param string $text |
||
230 | * @throws WebSocketError |
||
231 | */ |
||
232 | public function onText (string $text): void { |
||
236 | |||
237 | /** |
||
238 | * Forwards to the {@link FrameHandler} |
||
239 | * |
||
240 | * @param string $binary |
||
241 | */ |
||
242 | public function writeBinary (string $binary): void { |
||
245 | |||
246 | /** |
||
247 | * JSON-encodes and sends as `TEXT`. |
||
248 | * |
||
249 | * @param mixed $data |
||
250 | * @throws JsonException |
||
251 | */ |
||
252 | public function writeJSON ($data): void { |
||
255 | |||
256 | /** |
||
257 | * Forwards to the {@link FrameHandler} |
||
258 | * |
||
259 | * @param string $text |
||
260 | */ |
||
261 | public function writeText (string $text): void { |
||
264 | |||
265 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.