Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "jszczypk/websocket",
"name": "bkwatch/websocket",
"description": "WebSocket client library",
"type": "library",
"keywords": [ "websocket" ],
Expand All @@ -9,6 +9,11 @@
"name": "Janusz Szczypka",
"email": "janusz@smartb2b.eu",
"role": "Developer"
},
{
"name": "Jonathan Turkanis",
"email": "j@coderage.com",
"role": "Developer"
}
],
"require": {
Expand All @@ -20,7 +25,7 @@
},
"autoload": {
"psr-4": {
"JSzczypk\\WebSocket\\": "src/"
"BKWTools\\WebSocket\\": "src/"
}
},
"archive": {
Expand All @@ -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"
]
}
}
38 changes: 28 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace JSzczypk\WebSocket;
namespace BKWTools\WebSocket;

class Client
{
Expand Down Expand Up @@ -75,10 +75,14 @@ public function __construct(string $uri, array $options = [])

public function __destruct()
{
if ($this->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
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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.");
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace JSzczypk\WebSocket;
namespace BKWTools\WebSocket;

class ConnectionException extends Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace JSzczypk\WebSocket;
namespace BKWTools\WebSocket;

class Exception extends \Exception
{
Expand Down