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