diff --git a/composer.json b/composer.json index 4752657..3476fc3 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "jszczypk/websocket", + "name": "bkwatch/websocket", "description": "WebSocket client library", "type": "library", "keywords": [ "websocket" ], @@ -9,6 +9,11 @@ "name": "Janusz Szczypka", "email": "janusz@smartb2b.eu", "role": "Developer" + }, + { + "name": "Jonathan Turkanis", + "email": "j@coderage.com", + "role": "Developer" } ], "require": { @@ -20,7 +25,7 @@ }, "autoload": { "psr-4": { - "JSzczypk\\WebSocket\\": "src/" + "BKWTools\\WebSocket\\": "src/" } }, "archive": { @@ -37,7 +42,7 @@ "vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src" ], "fix": [ - "vendor/bin/phpcbf --standard=PSR2 src" + "vendor/bin/phpcbf --standard=PSR2 src" ] } } diff --git a/src/Client.php b/src/Client.php index 404363e..7a1b38e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,6 +1,6 @@ isConnected) { - $this->close(); - } elseif ($this->socket) { - fclose($this->socket); + try { + if ($this->isConnected) { + $this->close(); + } elseif ($this->socket) { + fclose($this->socket); + } + } catch (\Throwable $e) { + // No-op } } @@ -112,7 +116,7 @@ protected function connect(): void $headers = [ 'Host' => "$host:$port", - 'User-Agent' => 'jszczypk/websocket', + 'User-Agent' => 'bkwtools/websocket', 'Connection' => 'Upgrade', 'Upgrade' => 'WebSocket', 'Sec-WebSocket-Key' => $key, @@ -232,7 +236,7 @@ public function send(string $payload, int $opcode = self::OPCODE_TEXT): void $first |= ($i == 0) ? $opcode : self::OPCODE_CONTINUATION; $second = 0x80; // always mask - + $length = strlen($fragments[$i]); if ($length <= 125) { @@ -245,7 +249,7 @@ public function send(string $payload, int $opcode = self::OPCODE_TEXT): void $second |= 127; $msg = pack('CCJ', $first, $second, $length); } - + $mask = random_bytes(4); $msg .= $mask; $msg .= $this->mask($fragments[$i], $mask); @@ -355,7 +359,14 @@ public function close(int $status = 1000, string $message = ''): void protected function write(string $data): void { - $written = fwrite($this->socket, $data); + try { + set_error_handler(function($errno, $errstr) { + throw new ConnectionException("Error when sending data: $errstr"); + }); + $written = fwrite($this->socket, $data); + } finally { + restore_error_handler(); + } if ($written === false) { throw new ConnectionException("Error when sending data."); @@ -370,7 +381,14 @@ protected function read(int $length): string { $data = ''; while (strlen($data) < $length) { - $buffer = fread($this->socket, $length - strlen($data)); + try { + set_error_handler(function($errno, $errstr) { + throw new ConnectionException("Error when receiving data: $errstr"); + }); + $buffer = fread($this->socket, $length - strlen($data)); + } finally { + restore_error_handler(); + } if ($buffer === false) { $metadata = stream_get_meta_data($this->socket); throw new ConnectionException('Broken frame, read ' . strlen($data) . " of stated {$length} bytes. Stream state: ".json_encode($metadata)); diff --git a/src/ConnectionException.php b/src/ConnectionException.php index c9d4b83..d351b69 100644 --- a/src/ConnectionException.php +++ b/src/ConnectionException.php @@ -1,6 +1,6 @@