@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @return int |
| 14 | 14 | */ |
| 15 | - final public static function getType (): int { |
|
| 15 | + final public static function getType(): int { |
|
| 16 | 16 | return SOCK_STREAM; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * @return StreamClient |
| 26 | 26 | * @throws SocketError |
| 27 | 27 | */ |
| 28 | - public function accept (): StreamClient { |
|
| 28 | + public function accept(): StreamClient { |
|
| 29 | 29 | if (!$resource = @socket_accept($this->resource)) { |
| 30 | 30 | throw new SocketError($this->resource); // reliable errno |
| 31 | 31 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * @return $this |
| 46 | 46 | * @throws SocketError |
| 47 | 47 | */ |
| 48 | - public function listen (int $backlog = 0) { |
|
| 48 | + public function listen(int $backlog = 0) { |
|
| 49 | 49 | if (!@socket_listen($this->resource, $backlog)) { |
| 50 | 50 | throw new SocketError($this->resource); // reliable errno |
| 51 | 51 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * @param resource $resource The accepted connection. |
| 59 | 59 | * @return StreamClient |
| 60 | 60 | */ |
| 61 | - protected function newClient ($resource): StreamClient { |
|
| 61 | + protected function newClient($resource): StreamClient { |
|
| 62 | 62 | return new StreamClient($resource); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param $resource |
| 32 | 32 | * @param Reactor $reactor |
| 33 | 33 | */ |
| 34 | - public function __construct ($resource, Reactor $reactor) { |
|
| 34 | + public function __construct($resource, Reactor $reactor) { |
|
| 35 | 35 | parent::__construct($resource); |
| 36 | 36 | $reactor->add($this); |
| 37 | 37 | $this->reactor = $reactor; |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | /** |
| 41 | 41 | * @return WebSocketClient |
| 42 | 42 | */ |
| 43 | - public function accept (): WebSocketClient { |
|
| 43 | + public function accept(): WebSocketClient { |
|
| 44 | 44 | /** |
| 45 | 45 | * @see newClient() |
| 46 | 46 | * @var WebSocketClient $client |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @param int $opCode |
| 58 | 58 | * @param string $payload |
| 59 | 59 | */ |
| 60 | - public function broadcast (int $opCode, string $payload) { |
|
| 60 | + public function broadcast(int $opCode, string $payload) { |
|
| 61 | 61 | foreach ($this->clients as $client) { |
| 62 | 62 | if ($client->isOk()) { |
| 63 | 63 | $client->getFrameHandler()->write($opCode, $payload); |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | /** |
| 69 | 69 | * @param string $payload |
| 70 | 70 | */ |
| 71 | - public function broadcastBinary (string $payload) { |
|
| 71 | + public function broadcastBinary(string $payload) { |
|
| 72 | 72 | $this->broadcast(Frame::OP_BINARY, $payload); |
| 73 | 73 | } |
| 74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @param string $payload |
| 79 | 79 | */ |
| 80 | - public function broadcastPing (string $payload = '') { |
|
| 80 | + public function broadcastPing(string $payload = '') { |
|
| 81 | 81 | $this->broadcast(Frame::OP_PING, $payload); |
| 82 | 82 | } |
| 83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * |
| 87 | 87 | * @param string $text |
| 88 | 88 | */ |
| 89 | - public function broadcastText (string $text) { |
|
| 89 | + public function broadcastText(string $text) { |
|
| 90 | 90 | $this->broadcast(Frame::OP_TEXT, $text); |
| 91 | 91 | } |
| 92 | 92 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * @param string $reason |
| 98 | 98 | * @return $this |
| 99 | 99 | */ |
| 100 | - public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
| 100 | + public function close(int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
| 101 | 101 | foreach ($this->clients as $client) { |
| 102 | 102 | try { |
| 103 | 103 | $client->close($code, $reason); // clients remove themselves |
@@ -115,14 +115,14 @@ discard block |
||
| 115 | 115 | * |
| 116 | 116 | * @return int |
| 117 | 117 | */ |
| 118 | - public function count (): int { |
|
| 118 | + public function count(): int { |
|
| 119 | 119 | return count($this->clients); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | 123 | * @return WebSocketClient[] |
| 124 | 124 | */ |
| 125 | - public function getClients () { |
|
| 125 | + public function getClients() { |
|
| 126 | 126 | return $this->clients; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -130,21 +130,21 @@ discard block |
||
| 130 | 130 | * @param resource $resource |
| 131 | 131 | * @return WebSocketClient |
| 132 | 132 | */ |
| 133 | - protected function newClient ($resource): WebSocketClient { |
|
| 133 | + protected function newClient($resource): WebSocketClient { |
|
| 134 | 134 | return new WebSocketClient($resource, $this); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /** |
| 138 | 138 | * WebSocket servers never get OOB data. |
| 139 | 139 | */ |
| 140 | - final public function onOutOfBand (): void { |
|
| 140 | + final public function onOutOfBand(): void { |
|
| 141 | 141 | // do nothing |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | /** |
| 145 | 145 | * Auto-accept. |
| 146 | 146 | */ |
| 147 | - public function onReadable (): void { |
|
| 147 | + public function onReadable(): void { |
|
| 148 | 148 | $this->accept(); |
| 149 | 149 | } |
| 150 | 150 | |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @param WebSocketClient $client |
| 155 | 155 | */ |
| 156 | - public function remove ($client): void { |
|
| 156 | + public function remove($client): void { |
|
| 157 | 157 | unset($this->clients[$client->getId()]); |
| 158 | 158 | $this->reactor->remove($client); |
| 159 | 159 | } |
@@ -101,8 +101,7 @@ |
||
| 101 | 101 | foreach ($this->clients as $client) { |
| 102 | 102 | try { |
| 103 | 103 | $client->close($code, $reason); // clients remove themselves |
| 104 | - } |
|
| 105 | - catch (Exception $e) { |
|
| 104 | + } catch (Exception $e) { |
|
| 106 | 105 | continue; |
| 107 | 106 | } |
| 108 | 107 | } |
@@ -43,21 +43,21 @@ discard block |
||
| 43 | 43 | /** |
| 44 | 44 | * @param WebSocketClient $client |
| 45 | 45 | */ |
| 46 | - public function __construct (WebSocketClient $client) { |
|
| 46 | + public function __construct(WebSocketClient $client) { |
|
| 47 | 47 | $this->client = $client; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * @return string[] |
| 52 | 52 | */ |
| 53 | - public function getHeaders () { |
|
| 53 | + public function getHeaders() { |
|
| 54 | 54 | return $this->headers; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * @return string |
| 59 | 59 | */ |
| 60 | - public function getMethod (): string { |
|
| 60 | + public function getMethod(): string { |
|
| 61 | 61 | return $this->method; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @throws WebSocketError |
| 69 | 69 | * @throws Throwable |
| 70 | 70 | */ |
| 71 | - public function onReadable (): bool { |
|
| 71 | + public function onReadable(): bool { |
|
| 72 | 72 | // read into the buffer |
| 73 | 73 | $this->buffer .= $bytes = $this->client->recvAll(); |
| 74 | 74 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $key = strtolower(trim($key)); |
| 101 | 101 | $value = trim($value); |
| 102 | 102 | if (isset($this->headers[$key])) { |
| 103 | - $this->headers[$key] .= ', ' . $value; |
|
| 103 | + $this->headers[$key] .= ', '.$value; |
|
| 104 | 104 | } |
| 105 | 105 | else { |
| 106 | 106 | $this->headers[$key] = $value; |
@@ -129,8 +129,8 @@ discard block |
||
| 129 | 129 | /** |
| 130 | 130 | * Sends the connection upgrade headers. |
| 131 | 131 | */ |
| 132 | - protected function upgrade (): void { |
|
| 133 | - $key = base64_encode(sha1($this->headers['sec-websocket-key'] . self::RFC_GUID, true)); |
|
| 132 | + protected function upgrade(): void { |
|
| 133 | + $key = base64_encode(sha1($this->headers['sec-websocket-key'].self::RFC_GUID, true)); |
|
| 134 | 134 | $this->client->write(implode("\r\n", [ |
| 135 | 135 | "HTTP/1.1 101 Switching Protocols", |
| 136 | 136 | "Connection: Upgrade", |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @throws WebSocketError |
| 146 | 146 | */ |
| 147 | - protected function validate (): void { |
|
| 147 | + protected function validate(): void { |
|
| 148 | 148 | if (!( |
| 149 | 149 | $check = 'method = http 1.1' |
| 150 | 150 | and preg_match('/HTTP\/1\.1$/i', $this->method) |
@@ -101,19 +101,16 @@ |
||
| 101 | 101 | $value = trim($value); |
| 102 | 102 | if (isset($this->headers[$key])) { |
| 103 | 103 | $this->headers[$key] .= ', ' . $value; |
| 104 | - } |
|
| 105 | - else { |
|
| 104 | + } else { |
|
| 106 | 105 | $this->headers[$key] = $value; |
| 107 | 106 | } |
| 108 | 107 | } |
| 109 | 108 | $this->buffer = ''; // wipe the buffer |
| 110 | 109 | $this->validate(); |
| 111 | - } |
|
| 112 | - catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow |
|
| 110 | + } catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow |
|
| 113 | 111 | $this->client->write("HTTP/1.1 {$e->getCode()} WebSocket Handshake Failure\r\n\r\n"); |
| 114 | 112 | throw $e; |
| 115 | - } |
|
| 116 | - catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow |
|
| 113 | + } catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow |
|
| 117 | 114 | $this->client->write("HTTP/1.1 500 WebSocket Internal Error\r\n\r\n"); |
| 118 | 115 | throw $e; |
| 119 | 116 | } |
@@ -82,8 +82,7 @@ discard block |
||
| 82 | 82 | if ($code >= 1000 and $this->isOk()) { |
| 83 | 83 | $this->frameHandler->writeClose($code, $reason); |
| 84 | 84 | } |
| 85 | - } |
|
| 86 | - finally { |
|
| 85 | + } finally { |
|
| 87 | 86 | $this->server->remove($this); |
| 88 | 87 | parent::close(); |
| 89 | 88 | $this->state = self::STATE_CLOSED; |
@@ -200,16 +199,13 @@ discard block |
||
| 200 | 199 | $this->state = self::STATE_OK; |
| 201 | 200 | $this->onStateOk(); |
| 202 | 201 | } |
| 203 | - } |
|
| 204 | - elseif ($this->isOk()) { |
|
| 202 | + } elseif ($this->isOk()) { |
|
| 205 | 203 | $this->frameHandler->onReadable(); |
| 206 | 204 | } |
| 207 | - } |
|
| 208 | - catch (WebSocketError $e) { |
|
| 205 | + } catch (WebSocketError $e) { |
|
| 209 | 206 | $this->close($e->getCode(), $e->getMessage()); |
| 210 | 207 | throw $e; |
| 211 | - } |
|
| 212 | - catch (Throwable $e) { |
|
| 208 | + } catch (Throwable $e) { |
|
| 213 | 209 | $this->close(Frame::CLOSE_INTERNAL_ERROR); |
| 214 | 210 | throw $e; |
| 215 | 211 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @param resource $resource |
| 54 | 54 | * @param WebSocketServer $server |
| 55 | 55 | */ |
| 56 | - public function __construct ($resource, WebSocketServer $server) { |
|
| 56 | + public function __construct($resource, WebSocketServer $server) { |
|
| 57 | 57 | parent::__construct($resource); |
| 58 | 58 | $this->server = $server; |
| 59 | 59 | $this->handshake = new Handshake($this); |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * @param string $reason Sent to the peer, if code is >= 1000 |
| 79 | 79 | * @return $this |
| 80 | 80 | */ |
| 81 | - public function close (int $code = null, string $reason = '') { |
|
| 81 | + public function close(int $code = null, string $reason = '') { |
|
| 82 | 82 | try { |
| 83 | 83 | if ($code >= 1000 and $this->isOk()) { |
| 84 | 84 | $this->frameHandler->writeClose($code, $reason); |
@@ -95,32 +95,32 @@ discard block |
||
| 95 | 95 | /** |
| 96 | 96 | * @return FrameHandler |
| 97 | 97 | */ |
| 98 | - public function getFrameHandler (): FrameHandler { |
|
| 98 | + public function getFrameHandler(): FrameHandler { |
|
| 99 | 99 | return $this->frameHandler; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | 103 | * @return WebSocketServer |
| 104 | 104 | */ |
| 105 | - public function getServer (): WebSocketServer { |
|
| 105 | + public function getServer(): WebSocketServer { |
|
| 106 | 106 | return $this->server; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * @return int |
| 111 | 111 | */ |
| 112 | - public function getState (): int { |
|
| 112 | + public function getState(): int { |
|
| 113 | 113 | return $this->state; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - final public function isNegotiating (): bool { |
|
| 116 | + final public function isNegotiating(): bool { |
|
| 117 | 117 | return $this->state === self::STATE_HANDSHAKE; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * @return bool |
| 122 | 122 | */ |
| 123 | - final public function isOk (): bool { |
|
| 123 | + final public function isOk(): bool { |
|
| 124 | 124 | return $this->state === self::STATE_OK; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * @param string $binary |
| 133 | 133 | * @throws WebSocketError |
| 134 | 134 | */ |
| 135 | - public function onBinary (string $binary): void { |
|
| 135 | + public function onBinary(string $binary): void { |
|
| 136 | 136 | unset($binary); |
| 137 | 137 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data."); |
| 138 | 138 | } |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * @param int $code |
| 144 | 144 | * @param string $reason |
| 145 | 145 | */ |
| 146 | - public function onClose (int $code, string $reason): void { |
|
| 146 | + public function onClose(int $code, string $reason): void { |
|
| 147 | 147 | unset($code, $reason); |
| 148 | 148 | $this->close(); |
| 149 | 149 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * Closes the connection with a protocol-error frame. |
| 157 | 157 | */ |
| 158 | - final public function onOutOfBand (): void { |
|
| 158 | + final public function onOutOfBand(): void { |
|
| 159 | 159 | $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data."); |
| 160 | 160 | } |
| 161 | 161 | |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @param string $message |
| 168 | 168 | */ |
| 169 | - public function onPing (string $message): void { |
|
| 169 | + public function onPing(string $message): void { |
|
| 170 | 170 | $this->frameHandler->writePong($message); |
| 171 | 171 | } |
| 172 | 172 | |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @param string $message |
| 179 | 179 | */ |
| 180 | - public function onPong (string $message): void { |
|
| 180 | + public function onPong(string $message): void { |
|
| 181 | 181 | // stub |
| 182 | 182 | } |
| 183 | 183 | |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | * @throws WebSocketError |
| 188 | 188 | * @throws Throwable |
| 189 | 189 | */ |
| 190 | - public function onReadable (): void { |
|
| 190 | + public function onReadable(): void { |
|
| 191 | 191 | try { |
| 192 | 192 | if ($this->isNegotiating()) { |
| 193 | 193 | if ($this->handshake->onReadable()) { |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | * If you have negotiated an extension during {@link Handshake}, |
| 218 | 218 | * claim the RSV bits here via {@link FrameReader::setRsv()} |
| 219 | 219 | */ |
| 220 | - protected function onStateOk (): void { |
|
| 220 | + protected function onStateOk(): void { |
|
| 221 | 221 | // stub |
| 222 | 222 | } |
| 223 | 223 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | * @param string $text |
| 230 | 230 | * @throws WebSocketError |
| 231 | 231 | */ |
| 232 | - public function onText (string $text): void { |
|
| 232 | + public function onText(string $text): void { |
|
| 233 | 233 | unset($text); |
| 234 | 234 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text."); |
| 235 | 235 | } |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | * |
| 240 | 240 | * @param string $binary |
| 241 | 241 | */ |
| 242 | - public function writeBinary (string $binary): void { |
|
| 242 | + public function writeBinary(string $binary): void { |
|
| 243 | 243 | $this->frameHandler->writeBinary($binary); |
| 244 | 244 | } |
| 245 | 245 | |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | * @param mixed $data |
| 250 | 250 | * @throws JsonException |
| 251 | 251 | */ |
| 252 | - public function writeJSON ($data): void { |
|
| 252 | + public function writeJSON($data): void { |
|
| 253 | 253 | $this->frameHandler->writeText(json_encode($data, JSON_THROW_ON_ERROR)); |
| 254 | 254 | } |
| 255 | 255 | |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | * |
| 259 | 259 | * @param string $text |
| 260 | 260 | */ |
| 261 | - public function writeText (string $text): void { |
|
| 261 | + public function writeText(string $text): void { |
|
| 262 | 262 | $this->frameHandler->writeText($text); |
| 263 | 263 | } |
| 264 | 264 | |
@@ -43,15 +43,15 @@ discard block |
||
| 43 | 43 | 0x0f => 'RESERVED CONTROL 0x0f', |
| 44 | 44 | ]; |
| 45 | 45 | |
| 46 | - const CLOSE_NORMAL = 1000; // mutual closure |
|
| 47 | - const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
| 48 | - const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
| 49 | - const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
| 50 | - const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
| 51 | - const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
| 52 | - const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
| 53 | - const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
| 54 | - const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
| 46 | + const CLOSE_NORMAL = 1000; // mutual closure |
|
| 47 | + const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
| 48 | + const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
| 49 | + const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
| 50 | + const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
| 51 | + const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
| 52 | + const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
| 53 | + const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
| 54 | + const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | 57 | * @var bool |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param int $opCode |
| 82 | 82 | * @param string $payload |
| 83 | 83 | */ |
| 84 | - public function __construct (bool $final, int $rsv, int $opCode, string $payload) { |
|
| 84 | + public function __construct(bool $final, int $rsv, int $opCode, string $payload) { |
|
| 85 | 85 | $this->final = $final; |
| 86 | 86 | $this->rsv = $rsv; |
| 87 | 87 | $this->opCode = $opCode; |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @return string |
| 95 | 95 | */ |
| 96 | - public function __toString () { |
|
| 96 | + public function __toString() { |
|
| 97 | 97 | if ($this->isClose()) { |
| 98 | 98 | return $this->getCloseReason(); |
| 99 | 99 | } |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return int |
| 109 | 109 | */ |
| 110 | - final public function getCloseCode (): int { |
|
| 110 | + final public function getCloseCode(): int { |
|
| 111 | 111 | $code = substr($this->payload, 0, 2); |
| 112 | 112 | return isset($code[1]) ? unpack('n', $code)[1] : self::CLOSE_NORMAL; |
| 113 | 113 | } |
@@ -119,126 +119,126 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return string |
| 121 | 121 | */ |
| 122 | - final public function getCloseReason (): string { |
|
| 122 | + final public function getCloseReason(): string { |
|
| 123 | 123 | return substr($this->payload, 2); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * @return int |
| 128 | 128 | */ |
| 129 | - final public function getLength (): int { |
|
| 129 | + final public function getLength(): int { |
|
| 130 | 130 | return strlen($this->payload); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * @return string |
| 135 | 135 | */ |
| 136 | - public function getName (): string { |
|
| 136 | + public function getName(): string { |
|
| 137 | 137 | return self::NAMES[$this->opCode]; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * @return int |
| 142 | 142 | */ |
| 143 | - final public function getOpCode (): int { |
|
| 143 | + final public function getOpCode(): int { |
|
| 144 | 144 | return $this->opCode; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * @return string |
| 149 | 149 | */ |
| 150 | - final public function getPayload (): string { |
|
| 150 | + final public function getPayload(): string { |
|
| 151 | 151 | return $this->payload; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * @return int |
| 156 | 156 | */ |
| 157 | - final public function getRsv (): int { |
|
| 157 | + final public function getRsv(): int { |
|
| 158 | 158 | return $this->rsv; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | 162 | * @return bool |
| 163 | 163 | */ |
| 164 | - final public function hasRsv1 (): bool { |
|
| 165 | - return (bool)($this->rsv & self::RSV1); |
|
| 164 | + final public function hasRsv1(): bool { |
|
| 165 | + return (bool) ($this->rsv & self::RSV1); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
| 169 | 169 | * @return bool |
| 170 | 170 | */ |
| 171 | - final public function hasRsv2 (): bool { |
|
| 172 | - return (bool)($this->rsv & self::RSV2); |
|
| 171 | + final public function hasRsv2(): bool { |
|
| 172 | + return (bool) ($this->rsv & self::RSV2); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /** |
| 176 | 176 | * @return bool |
| 177 | 177 | */ |
| 178 | - final public function hasRsv3 (): bool { |
|
| 179 | - return (bool)($this->rsv & self::RSV3); |
|
| 178 | + final public function hasRsv3(): bool { |
|
| 179 | + return (bool) ($this->rsv & self::RSV3); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
| 183 | 183 | * @return bool |
| 184 | 184 | */ |
| 185 | - final public function isBinary (): bool { |
|
| 185 | + final public function isBinary(): bool { |
|
| 186 | 186 | return $this->opCode === self::OP_BINARY; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | 190 | * @return bool |
| 191 | 191 | */ |
| 192 | - final public function isClose (): bool { |
|
| 192 | + final public function isClose(): bool { |
|
| 193 | 193 | return $this->opCode === self::OP_CLOSE; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
| 197 | 197 | * @return bool |
| 198 | 198 | */ |
| 199 | - final public function isContinuation (): bool { |
|
| 199 | + final public function isContinuation(): bool { |
|
| 200 | 200 | return $this->opCode === self::OP_CONTINUATION; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
| 204 | 204 | * @return bool |
| 205 | 205 | */ |
| 206 | - final public function isControl (): bool { |
|
| 206 | + final public function isControl(): bool { |
|
| 207 | 207 | return $this->opCode >= self::OP_CLOSE; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * @return bool |
| 212 | 212 | */ |
| 213 | - final public function isData (): bool { |
|
| 213 | + final public function isData(): bool { |
|
| 214 | 214 | return $this->opCode < self::OP_CLOSE; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
| 218 | 218 | * @return bool |
| 219 | 219 | */ |
| 220 | - final public function isFinal (): bool { |
|
| 220 | + final public function isFinal(): bool { |
|
| 221 | 221 | return $this->final; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * @return bool |
| 226 | 226 | */ |
| 227 | - final public function isPing (): bool { |
|
| 227 | + final public function isPing(): bool { |
|
| 228 | 228 | return $this->opCode === self::OP_PING; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * @return bool |
| 233 | 233 | */ |
| 234 | - final public function isPong (): bool { |
|
| 234 | + final public function isPong(): bool { |
|
| 235 | 235 | return $this->opCode === self::OP_PONG; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| 239 | 239 | * @return bool |
| 240 | 240 | */ |
| 241 | - final public function isText (): bool { |
|
| 241 | + final public function isText(): bool { |
|
| 242 | 242 | return $this->opCode === self::OP_TEXT; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | /** |
| 63 | 63 | * @param WebSocketClient $client |
| 64 | 64 | */ |
| 65 | - public function __construct (WebSocketClient $client) { |
|
| 65 | + public function __construct(WebSocketClient $client) { |
|
| 66 | 66 | $this->client = $client; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * @return null|Frame |
| 73 | 73 | * @throws WebSocketError |
| 74 | 74 | */ |
| 75 | - protected function getFrame (): ?Frame { |
|
| 75 | + protected function getFrame(): ?Frame { |
|
| 76 | 76 | // wait for the header |
| 77 | 77 | if (!$this->header ??= $this->getFrame_header()) { |
| 78 | 78 | return null; |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
| 111 | 111 | * @return null|array |
| 112 | 112 | */ |
| 113 | - protected function getFrame_header (): ?array { |
|
| 113 | + protected function getFrame_header(): ?array { |
|
| 114 | 114 | if (!preg_match(self::REGEXP, $this->buffer, $match)) { |
| 115 | 115 | if (strlen($this->buffer) >= 14) { // max head room |
| 116 | 116 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, 'Bad frame.'); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return Generator|Frame[] |
| 168 | 168 | */ |
| 169 | - public function getFrames () { |
|
| 169 | + public function getFrames() { |
|
| 170 | 170 | // read into the buffer |
| 171 | 171 | $this->buffer .= $bytes = $this->client->recvAll(); |
| 172 | 172 | |
@@ -184,14 +184,14 @@ discard block |
||
| 184 | 184 | /** |
| 185 | 185 | * @return int |
| 186 | 186 | */ |
| 187 | - public function getMaxLength (): int { |
|
| 187 | + public function getMaxLength(): int { |
|
| 188 | 188 | return $this->maxLength; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
| 192 | 192 | * @return int |
| 193 | 193 | */ |
| 194 | - public function getRsv (): int { |
|
| 194 | + public function getRsv(): int { |
|
| 195 | 195 | return $this->rsv; |
| 196 | 196 | } |
| 197 | 197 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | * @param string $payload |
| 202 | 202 | * @return Frame |
| 203 | 203 | */ |
| 204 | - protected function newFrame (string $payload): Frame { |
|
| 204 | + protected function newFrame(string $payload): Frame { |
|
| 205 | 205 | return new Frame($this->header['final'], $this->header['rsv'], $this->header['opCode'], $payload); |
| 206 | 206 | } |
| 207 | 207 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | * @param int $bytes |
| 210 | 210 | * @return $this |
| 211 | 211 | */ |
| 212 | - public function setMaxLength (int $bytes) { |
|
| 212 | + public function setMaxLength(int $bytes) { |
|
| 213 | 213 | $this->maxLength = min(max(125, $bytes), 2 ** 63 - 1); |
| 214 | 214 | return $this; |
| 215 | 215 | } |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | * @param int $rsv |
| 219 | 219 | * @return $this |
| 220 | 220 | */ |
| 221 | - public function setRsv (int $rsv) { |
|
| 221 | + public function setRsv(int $rsv) { |
|
| 222 | 222 | $this->rsv = $rsv; |
| 223 | 223 | return $this; |
| 224 | 224 | } |
@@ -141,19 +141,15 @@ |
||
| 141 | 141 | if ($badRsv = $rsv & ~$this->rsv) { |
| 142 | 142 | $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT); |
| 143 | 143 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV bits: 0b{$badRsv}"); |
| 144 | - } |
|
| 145 | - elseif ($opCode >= Frame::OP_CLOSE) { |
|
| 144 | + } elseif ($opCode >= Frame::OP_CLOSE) { |
|
| 146 | 145 | if ($opCode > Frame::OP_PONG) { |
| 147 | 146 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported control frame ({$opCode})"); |
| 148 | - } |
|
| 149 | - elseif (!$final) { |
|
| 147 | + } elseif (!$final) { |
|
| 150 | 148 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received fragmented control frame ({$opCode})"); |
| 151 | 149 | } |
| 152 | - } |
|
| 153 | - elseif ($opCode > Frame::OP_BINARY) { |
|
| 150 | + } elseif ($opCode > Frame::OP_BINARY) { |
|
| 154 | 151 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported data frame ({$opCode})"); |
| 155 | - } |
|
| 156 | - elseif ($length > $this->maxLength) { |
|
| 152 | + } elseif ($length > $this->maxLength) { |
|
| 157 | 153 | throw new WebSocketError(Frame::CLOSE_TOO_LARGE, "Payload would exceed {$this->maxLength} bytes"); |
| 158 | 154 | } |
| 159 | 155 | |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | /** |
| 68 | 68 | * @param WebSocketClient $client |
| 69 | 69 | */ |
| 70 | - public function __construct (WebSocketClient $client) { |
|
| 70 | + public function __construct(WebSocketClient $client) { |
|
| 71 | 71 | $this->client = $client; |
| 72 | 72 | $this->reader = new FrameReader($client); |
| 73 | 73 | } |
@@ -75,21 +75,21 @@ discard block |
||
| 75 | 75 | /** |
| 76 | 76 | * @return int |
| 77 | 77 | */ |
| 78 | - public function getFragmentSize (): int { |
|
| 78 | + public function getFragmentSize(): int { |
|
| 79 | 79 | return $this->fragmentSize; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * @return int |
| 84 | 84 | */ |
| 85 | - public function getMaxLength (): int { |
|
| 85 | + public function getMaxLength(): int { |
|
| 86 | 86 | return $this->maxLength; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * @return bool |
| 91 | 91 | */ |
| 92 | - public function isStream (): bool { |
|
| 92 | + public function isStream(): bool { |
|
| 93 | 93 | return $this->stream; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @param Frame $frame |
| 103 | 103 | * @throws WebSocketError |
| 104 | 104 | */ |
| 105 | - protected function onBinary (Frame $frame): void { |
|
| 105 | + protected function onBinary(Frame $frame): void { |
|
| 106 | 106 | if ($this->stream) { |
| 107 | 107 | $this->client->onBinary($frame->getPayload()); |
| 108 | 108 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * |
| 129 | 129 | * @param Frame $frame |
| 130 | 130 | */ |
| 131 | - protected function onClose (Frame $frame): void { |
|
| 131 | + protected function onClose(Frame $frame): void { |
|
| 132 | 132 | $this->client->onClose($frame->getCloseCode(), $frame->getCloseReason()); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param Frame $frame |
| 139 | 139 | * @throws WebSocketError |
| 140 | 140 | */ |
| 141 | - protected function onContinuation (Frame $frame): void { |
|
| 141 | + protected function onContinuation(Frame $frame): void { |
|
| 142 | 142 | if (!$this->continue) { |
| 143 | 143 | throw new WebSocketError( |
| 144 | 144 | Frame::CLOSE_PROTOCOL_ERROR, |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | * |
| 171 | 171 | * @param Frame $frame |
| 172 | 172 | */ |
| 173 | - protected function onControl (Frame $frame): void { |
|
| 173 | + protected function onControl(Frame $frame): void { |
|
| 174 | 174 | if ($frame->isClose()) { |
| 175 | 175 | $this->onClose($frame); |
| 176 | 176 | } |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @param Frame $frame |
| 192 | 192 | */ |
| 193 | - protected function onData (Frame $frame): void { |
|
| 193 | + protected function onData(Frame $frame): void { |
|
| 194 | 194 | // did we get a continuation? |
| 195 | 195 | if ($frame->isContinuation()) { |
| 196 | 196 | $this->onContinuation($frame); |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | elseif ($this->continue) { |
| 200 | 200 | throw new WebSocketError( |
| 201 | 201 | Frame::CLOSE_PROTOCOL_ERROR, |
| 202 | - "Received interleaved {$frame->getName()} against existing " . Frame::NAMES[$this->continue], |
|
| 202 | + "Received interleaved {$frame->getName()} against existing ".Frame::NAMES[$this->continue], |
|
| 203 | 203 | $frame |
| 204 | 204 | ); |
| 205 | 205 | } |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | * @param Frame $frame |
| 227 | 227 | * @throws WebSocketError |
| 228 | 228 | */ |
| 229 | - protected function onData_CheckLength (Frame $frame): void { |
|
| 229 | + protected function onData_CheckLength(Frame $frame): void { |
|
| 230 | 230 | if (strlen($this->buffer) + $frame->getLength() > $this->maxLength) { |
| 231 | 231 | throw new WebSocketError( |
| 232 | 232 | Frame::CLOSE_TOO_LARGE, |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @param Frame $frame |
| 247 | 247 | */ |
| 248 | - public function onFrame (Frame $frame): void { |
|
| 248 | + public function onFrame(Frame $frame): void { |
|
| 249 | 249 | if ($frame->isControl()) { |
| 250 | 250 | $this->onControl($frame); |
| 251 | 251 | } |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @param Frame $frame |
| 261 | 261 | */ |
| 262 | - protected function onPing (Frame $frame): void { |
|
| 262 | + protected function onPing(Frame $frame): void { |
|
| 263 | 263 | $this->client->onPing($frame->getPayload()); |
| 264 | 264 | } |
| 265 | 265 | |
@@ -268,14 +268,14 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @param Frame $frame |
| 270 | 270 | */ |
| 271 | - protected function onPong (Frame $frame): void { |
|
| 271 | + protected function onPong(Frame $frame): void { |
|
| 272 | 272 | $this->client->onPong($frame->getPayload()); |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
| 276 | 276 | * Uses {@link FrameReader} to read frames and passes them off to {@link onFrame()} |
| 277 | 277 | */ |
| 278 | - public function onReadable (): void { |
|
| 278 | + public function onReadable(): void { |
|
| 279 | 279 | foreach ($this->reader->getFrames() as $frame) { |
| 280 | 280 | $this->onFrame($frame); |
| 281 | 281 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param Frame $frame |
| 289 | 289 | * @throws WebSocketError |
| 290 | 290 | */ |
| 291 | - protected function onText (Frame $frame): void { |
|
| 291 | + protected function onText(Frame $frame): void { |
|
| 292 | 292 | $this->onData_CheckLength($frame); |
| 293 | 293 | $this->buffer .= $frame->getPayload(); |
| 294 | 294 | if ($frame->isFinal()) { |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | * @param int $bytes |
| 306 | 306 | * @return $this |
| 307 | 307 | */ |
| 308 | - public function setFragmentSize (int $bytes) { |
|
| 308 | + public function setFragmentSize(int $bytes) { |
|
| 309 | 309 | $this->fragmentSize = $bytes; |
| 310 | 310 | return $this; |
| 311 | 311 | } |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @param int $bytes |
| 315 | 315 | * @return $this |
| 316 | 316 | */ |
| 317 | - public function setMaxLength (int $bytes) { |
|
| 317 | + public function setMaxLength(int $bytes) { |
|
| 318 | 318 | $this->maxLength = $bytes; |
| 319 | 319 | return $this; |
| 320 | 320 | } |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | * @param bool $stream |
| 324 | 324 | * @return $this |
| 325 | 325 | */ |
| 326 | - public function setStream (bool $stream) { |
|
| 326 | + public function setStream(bool $stream) { |
|
| 327 | 327 | $this->stream = $stream; |
| 328 | 328 | return $this; |
| 329 | 329 | } |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | * @param int $opCode |
| 335 | 335 | * @param string $payload |
| 336 | 336 | */ |
| 337 | - public function write (int $opCode, string $payload): void { |
|
| 337 | + public function write(int $opCode, string $payload): void { |
|
| 338 | 338 | $offset = 0; |
| 339 | 339 | $total = strlen($payload); |
| 340 | 340 | do { |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | /** |
| 351 | 351 | * @param string $payload |
| 352 | 352 | */ |
| 353 | - public function writeBinary (string $payload): void { |
|
| 353 | + public function writeBinary(string $payload): void { |
|
| 354 | 354 | $this->write(Frame::OP_BINARY, $payload); |
| 355 | 355 | } |
| 356 | 356 | |
@@ -358,8 +358,8 @@ discard block |
||
| 358 | 358 | * @param int $code |
| 359 | 359 | * @param string $reason |
| 360 | 360 | */ |
| 361 | - public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 362 | - $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason); |
|
| 361 | + public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 362 | + $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | * @param int $opCode |
| 370 | 370 | * @param string $payload |
| 371 | 371 | */ |
| 372 | - protected function writeFrame (bool $final, int $opCode, string $payload): void { |
|
| 372 | + protected function writeFrame(bool $final, int $opCode, string $payload): void { |
|
| 373 | 373 | if ($opCode & 0x08 and !$final) { |
| 374 | 374 | throw new LogicException("Would have sent a fragmented control frame ({$opCode}) {$payload}"); |
| 375 | 375 | } |
@@ -386,27 +386,27 @@ discard block |
||
| 386 | 386 | else { |
| 387 | 387 | $head .= chr($length); |
| 388 | 388 | } |
| 389 | - $this->client->write($head . $payload); |
|
| 389 | + $this->client->write($head.$payload); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | /** |
| 393 | 393 | * @param string $payload |
| 394 | 394 | */ |
| 395 | - public function writePing (string $payload = ''): void { |
|
| 395 | + public function writePing(string $payload = ''): void { |
|
| 396 | 396 | $this->writeFrame(true, Frame::OP_PING, $payload); |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | /** |
| 400 | 400 | * @param string $payload |
| 401 | 401 | */ |
| 402 | - public function writePong (string $payload = ''): void { |
|
| 402 | + public function writePong(string $payload = ''): void { |
|
| 403 | 403 | $this->writeFrame(true, Frame::OP_PONG, $payload); |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
| 407 | 407 | * @param string $payload |
| 408 | 408 | */ |
| 409 | - public function writeText (string $payload): void { |
|
| 409 | + public function writeText(string $payload): void { |
|
| 410 | 410 | $this->write(Frame::OP_TEXT, $payload); |
| 411 | 411 | } |
| 412 | 412 | } |
| 413 | 413 | \ No newline at end of file |
@@ -105,8 +105,7 @@ discard block |
||
| 105 | 105 | protected function onBinary (Frame $frame): void { |
| 106 | 106 | if ($this->stream) { |
| 107 | 107 | $this->client->onBinary($frame->getPayload()); |
| 108 | - } |
|
| 109 | - else { |
|
| 108 | + } else { |
|
| 110 | 109 | $this->onData_CheckLength($frame); |
| 111 | 110 | $this->buffer .= $frame->getPayload(); |
| 112 | 111 | if ($frame->isFinal()) { |
@@ -149,12 +148,10 @@ discard block |
||
| 149 | 148 | try { |
| 150 | 149 | if ($this->continue === Frame::OP_TEXT) { |
| 151 | 150 | $this->onText($frame); |
| 152 | - } |
|
| 153 | - else { |
|
| 151 | + } else { |
|
| 154 | 152 | $this->onBinary($frame); |
| 155 | 153 | } |
| 156 | - } |
|
| 157 | - finally { |
|
| 154 | + } finally { |
|
| 158 | 155 | if ($frame->isFinal()) { |
| 159 | 156 | $this->continue = null; |
| 160 | 157 | } |
@@ -173,14 +170,11 @@ discard block |
||
| 173 | 170 | protected function onControl (Frame $frame): void { |
| 174 | 171 | if ($frame->isClose()) { |
| 175 | 172 | $this->onClose($frame); |
| 176 | - } |
|
| 177 | - elseif ($frame->isPing()) { |
|
| 173 | + } elseif ($frame->isPing()) { |
|
| 178 | 174 | $this->onPing($frame); |
| 179 | - } |
|
| 180 | - elseif ($frame->isPong()) { |
|
| 175 | + } elseif ($frame->isPong()) { |
|
| 181 | 176 | $this->onPong($frame); |
| 182 | - } |
|
| 183 | - else { |
|
| 177 | + } else { |
|
| 184 | 178 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Unsupported control frame.", $frame); |
| 185 | 179 | } |
| 186 | 180 | } |
@@ -248,8 +242,7 @@ discard block |
||
| 248 | 242 | public function onFrame (Frame $frame): void { |
| 249 | 243 | if ($frame->isControl()) { |
| 250 | 244 | $this->onControl($frame); |
| 251 | - } |
|
| 252 | - else { |
|
| 245 | + } else { |
|
| 253 | 246 | $this->onData($frame); |
| 254 | 247 | } |
| 255 | 248 | } |
@@ -378,12 +371,10 @@ discard block |
||
| 378 | 371 | if ($length > 65535) { |
| 379 | 372 | $head .= chr(127); |
| 380 | 373 | $head .= pack('J', $length); |
| 381 | - } |
|
| 382 | - elseif ($length >= 126) { |
|
| 374 | + } elseif ($length >= 126) { |
|
| 383 | 375 | $head .= chr(126); |
| 384 | 376 | $head .= pack('n', $length); |
| 385 | - } |
|
| 386 | - else { |
|
| 377 | + } else { |
|
| 387 | 378 | $head .= chr($length); |
| 388 | 379 | } |
| 389 | 380 | $this->client->write($head . $payload); |
@@ -31,12 +31,12 @@ discard block |
||
| 31 | 31 | * @return int |
| 32 | 32 | * @throws SocketError |
| 33 | 33 | */ |
| 34 | - public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
| 34 | + public static function select(array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
| 35 | 35 | $rwe = [$read, $write, $except]; |
| 36 | 36 | array_walk_recursive($rwe, function(SocketInterface &$each) { |
| 37 | 37 | $each = $each->getResource(); |
| 38 | 38 | }); |
| 39 | - $uSec = (int)(fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
| 39 | + $uSec = (int) (fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
| 40 | 40 | $count = @socket_select($rwe[0], $rwe[1], $rwe[2], $timeout, $uSec); // keys are preserved |
| 41 | 41 | if ($count === false) { |
| 42 | 42 | $read = $write = $except = []; |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | * @param ReactiveInterface $socket |
| 55 | 55 | * @return $this |
| 56 | 56 | */ |
| 57 | - public function add (ReactiveInterface $socket) { |
|
| 57 | + public function add(ReactiveInterface $socket) { |
|
| 58 | 58 | $this->sockets[$socket->getId()] = $socket; |
| 59 | 59 | return $this; |
| 60 | 60 | } |
@@ -64,14 +64,14 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return int |
| 66 | 66 | */ |
| 67 | - public function count (): int { |
|
| 67 | + public function count(): int { |
|
| 68 | 68 | return count($this->sockets); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | 72 | * @return ReactiveInterface[] |
| 73 | 73 | */ |
| 74 | - public function getSockets () { |
|
| 74 | + public function getSockets() { |
|
| 75 | 75 | return $this->sockets; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param ReactiveInterface $socket |
| 82 | 82 | * @return bool |
| 83 | 83 | */ |
| 84 | - public function has (ReactiveInterface $socket): bool { |
|
| 84 | + public function has(ReactiveInterface $socket): bool { |
|
| 85 | 85 | return isset($sockets[$socket->getId()]); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * @param ReactiveInterface $socket |
| 91 | 91 | * @param Throwable $error |
| 92 | 92 | */ |
| 93 | - public function onError (int $channel, $socket, Throwable $error): void { |
|
| 93 | + public function onError(int $channel, $socket, Throwable $error): void { |
|
| 94 | 94 | unset($channel); |
| 95 | 95 | echo "{$error}\n\n"; |
| 96 | 96 | if ($socket->isOpen()) { |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. |
| 114 | 114 | * @return int Number of sockets selected. |
| 115 | 115 | */ |
| 116 | - public function react (?float $timeout = null): int { |
|
| 116 | + public function react(?float $timeout = null): int { |
|
| 117 | 117 | /** @var ReactiveInterface[][] $rwe */ |
| 118 | 118 | $rwe = [$this->sockets, [], $this->sockets]; |
| 119 | 119 | $count = static::select($rwe[0], $rwe[1], $rwe[2], $timeout); |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @param ReactiveInterface $socket |
| 143 | 143 | * @return $this |
| 144 | 144 | */ |
| 145 | - public function remove (ReactiveInterface $socket) { |
|
| 145 | + public function remove(ReactiveInterface $socket) { |
|
| 146 | 146 | unset($this->sockets[$socket->getId()]); |
| 147 | 147 | return $this; |
| 148 | 148 | } |
@@ -96,8 +96,7 @@ discard block |
||
| 96 | 96 | if ($socket->isOpen()) { |
| 97 | 97 | if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) { |
| 98 | 98 | $socket->close($error->getCode(), $error->getMessage()); |
| 99 | - } |
|
| 100 | - else { |
|
| 99 | + } else { |
|
| 101 | 100 | $socket->close(); |
| 102 | 101 | } |
| 103 | 102 | } |
@@ -121,12 +120,10 @@ discard block |
||
| 121 | 120 | foreach ($rwe[$channel] as $id => $socket) { |
| 122 | 121 | try { |
| 123 | 122 | $socket->{$method}(); |
| 124 | - } |
|
| 125 | - catch (Throwable $error) { |
|
| 123 | + } catch (Throwable $error) { |
|
| 126 | 124 | unset($rwe[0][$id]); // prevent onReadable() if this is an OOB error. |
| 127 | 125 | $this->onError($channel, $socket, $error); |
| 128 | - } |
|
| 129 | - finally { |
|
| 126 | + } finally { |
|
| 130 | 127 | if (!$socket->isOpen() and $this->has($socket)) { |
| 131 | 128 | $this->remove($socket); |
| 132 | 129 | } |