diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
commit | 0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6 (patch) | |
tree | 84c735df2e97350a721273e9dd425729d43cc8a2 /vendor/textalk/websocket/examples | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.tar.xz icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.zip |
Adding upstream version 0.10.2+dfsg1.upstream/0.10.2+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/textalk/websocket/examples')
-rw-r--r-- | vendor/textalk/websocket/examples/echoserver.php | 87 | ||||
-rw-r--r-- | vendor/textalk/websocket/examples/random_client.php | 94 | ||||
-rw-r--r-- | vendor/textalk/websocket/examples/random_server.php | 93 | ||||
-rw-r--r-- | vendor/textalk/websocket/examples/send.php | 51 |
4 files changed, 325 insertions, 0 deletions
diff --git a/vendor/textalk/websocket/examples/echoserver.php b/vendor/textalk/websocket/examples/echoserver.php new file mode 100644 index 0000000..231c4c9 --- /dev/null +++ b/vendor/textalk/websocket/examples/echoserver.php @@ -0,0 +1,87 @@ +<?php + +/** + * This file is used for the tests, but can also serve as an example of a WebSocket\Server. + * Run in console: php examples/echoserver.php + * + * Console options: + * --port <int> : The port to listen to, default 8000 + * --timeout <int> : Timeout in seconds, default 200 seconds + * --debug : Output log data (if logger is available) + */ + +namespace WebSocket; + +require __DIR__ . '/../vendor/autoload.php'; + +error_reporting(-1); + +echo "> Random server\n"; + +// Server options specified or random +$options = array_merge([ + 'port' => 8000, + 'timeout' => 200, + 'filter' => ['text', 'binary', 'ping', 'pong'], +], getopt('', ['port:', 'timeout:', 'debug'])); + +// If debug mode and logger is available +if (isset($options['debug']) && class_exists('WebSocket\EchoLog')) { + $logger = new EchoLog(); + $options['logger'] = $logger; + echo "> Using logger\n"; +} + +// Setting timeout to 200 seconds to make time for all tests and manual runs. +try { + $server = new Server($options); +} catch (ConnectionException $e) { + echo "> ERROR: {$e->getMessage()}\n"; + die(); +} + +echo "> Listening to port {$server->getPort()}\n"; + +// Force quit to close server +while (true) { + try { + while ($server->accept()) { + echo "> Accepted on port {$server->getPort()}\n"; + while (true) { + $message = $server->receive(); + $opcode = $server->getLastOpcode(); + if (is_null($message)) { + echo "> Closing connection\n"; + continue 2; + } + echo "> Got '{$message}' [opcode: {$opcode}]\n"; + if (in_array($opcode, ['ping', 'pong'])) { + $server->send($message); + continue; + } + // Allow certain string to trigger server action + switch ($message) { + case 'exit': + echo "> Client told me to quit. Bye bye.\n"; + $server->close(); + echo "> Close status: {$server->getCloseStatus()}\n"; + exit; + case 'headers': + $server->text(implode("\r\n", $server->getRequest())); + break; + case 'ping': + $server->ping($message); + break; + case 'auth': + $auth = $server->getHeader('Authorization'); + $server->text("{$auth} - {$message}"); + break; + default: + $server->text($message); + } + } + } + } catch (ConnectionException $e) { + echo "> ERROR: {$e->getMessage()}\n"; + } +} diff --git a/vendor/textalk/websocket/examples/random_client.php b/vendor/textalk/websocket/examples/random_client.php new file mode 100644 index 0000000..b23bd6b --- /dev/null +++ b/vendor/textalk/websocket/examples/random_client.php @@ -0,0 +1,94 @@ +<?php + +/** + * Websocket client that read/write random data. + * Run in console: php examples/random_client.php + * + * Console options: + * --uri <uri> : The URI to connect to, default ws://localhost:8000 + * --timeout <int> : Timeout in seconds, random default + * --fragment_size <int> : Fragment size as bytes, random default + * --debug : Output log data (if logger is available) + */ + +namespace WebSocket; + +require __DIR__ . '/../vendor/autoload.php'; + +error_reporting(-1); + +$randStr = function (int $maxlength = 4096) { + $string = ''; + $length = rand(1, $maxlength); + for ($i = 0; $i < $length; $i++) { + $string .= chr(rand(33, 126)); + } + return $string; +}; + +echo "> Random client\n"; + +// Server options specified or random +$options = array_merge([ + 'uri' => 'ws://localhost:8000', + 'timeout' => rand(1, 60), + 'fragment_size' => rand(1, 4096) * 8, +], getopt('', ['uri:', 'timeout:', 'fragment_size:', 'debug'])); + +// If debug mode and logger is available +if (isset($options['debug']) && class_exists('WebSocket\EchoLog')) { + $logger = new EchoLog(); + $options['logger'] = $logger; + echo "> Using logger\n"; +} + +// Main loop +while (true) { + try { + $client = new Client($options['uri'], $options); + $info = json_encode([ + 'uri' => $options['uri'], + 'timeout' => $options['timeout'], + 'framgemt_size' => $client->getFragmentSize(), + ]); + echo "> Creating client {$info}\n"; + + try { + while (true) { + // Random actions + switch (rand(1, 10)) { + case 1: + echo "> Sending text\n"; + $client->text("Text message {$randStr()}"); + break; + case 2: + echo "> Sending binary\n"; + $client->binary("Binary message {$randStr()}"); + break; + case 3: + echo "> Sending close\n"; + $client->close(rand(1000, 2000), "Close message {$randStr(8)}"); + break; + case 4: + echo "> Sending ping\n"; + $client->ping("Ping message {$randStr(8)}"); + break; + case 5: + echo "> Sending pong\n"; + $client->pong("Pong message {$randStr(8)}"); + break; + default: + echo "> Receiving\n"; + $received = $client->receive(); + echo "> Received {$client->getLastOpcode()}: {$received}\n"; + } + sleep(rand(1, 5)); + } + } catch (\Throwable $e) { + echo "ERROR I/O: {$e->getMessage()} [{$e->getCode()}]\n"; + } + } catch (\Throwable $e) { + echo "ERROR: {$e->getMessage()} [{$e->getCode()}]\n"; + } + sleep(rand(1, 5)); +} diff --git a/vendor/textalk/websocket/examples/random_server.php b/vendor/textalk/websocket/examples/random_server.php new file mode 100644 index 0000000..0b0849c --- /dev/null +++ b/vendor/textalk/websocket/examples/random_server.php @@ -0,0 +1,93 @@ +<?php + +/** + * Websocket server that read/write random data. + * Run in console: php examples/random_server.php + * + * Console options: + * --port <int> : The port to listen to, default 8000 + * --timeout <int> : Timeout in seconds, random default + * --fragment_size <int> : Fragment size as bytes, random default + * --debug : Output log data (if logger is available) + */ + +namespace WebSocket; + +require __DIR__ . '/../vendor/autoload.php'; + +error_reporting(-1); + +$randStr = function (int $maxlength = 4096) { + $string = ''; + $length = rand(1, $maxlength); + for ($i = 0; $i < $length; $i++) { + $string .= chr(rand(33, 126)); + } + return $string; +}; + +echo "> Random server\n"; + +// Server options specified or random +$options = array_merge([ + 'port' => 8000, + 'timeout' => rand(1, 60), + 'fragment_size' => rand(1, 4096) * 8, +], getopt('', ['port:', 'timeout:', 'fragment_size:', 'debug'])); + +// If debug mode and logger is available +if (isset($options['debug']) && class_exists('WebSocket\EchoLog')) { + $logger = new EchoLog(); + $options['logger'] = $logger; + echo "> Using logger\n"; +} + +// Force quit to close server +while (true) { + try { + // Setup server + $server = new Server($options); + $info = json_encode([ + 'port' => $server->getPort(), + 'timeout' => $options['timeout'], + 'framgemt_size' => $server->getFragmentSize(), + ]); + echo "> Creating server {$info}\n"; + + while ($server->accept()) { + while (true) { + // Random actions + switch (rand(1, 10)) { + case 1: + echo "> Sending text\n"; + $server->text("Text message {$randStr()}"); + break; + case 2: + echo "> Sending binary\n"; + $server->binary("Binary message {$randStr()}"); + break; + case 3: + echo "> Sending close\n"; + $server->close(rand(1000, 2000), "Close message {$randStr(8)}"); + break; + case 4: + echo "> Sending ping\n"; + $server->ping("Ping message {$randStr(8)}"); + break; + case 5: + echo "> Sending pong\n"; + $server->pong("Pong message {$randStr(8)}"); + break; + default: + echo "> Receiving\n"; + $received = $server->receive(); + echo "> Received {$server->getLastOpcode()}: {$received}\n"; + } + sleep(rand(1, 5)); + } + } + } catch (\Throwable $e) { + echo "ERROR: {$e->getMessage()} [{$e->getCode()}]\n"; + } + sleep(rand(1, 5)); +} diff --git a/vendor/textalk/websocket/examples/send.php b/vendor/textalk/websocket/examples/send.php new file mode 100644 index 0000000..30e48e0 --- /dev/null +++ b/vendor/textalk/websocket/examples/send.php @@ -0,0 +1,51 @@ +<?php + +/** + * Simple send & receive client for test purpose. + * Run in console: php examples/send.php <options> <message> + * + * Console options: + * --uri <uri> : The URI to connect to, default ws://localhost:8000 + * --opcode <string> : Opcode to send, default 'text' + * --debug : Output log data (if logger is available) + */ + +namespace WebSocket; + +require __DIR__ . '/../vendor/autoload.php'; + +error_reporting(-1); + +echo "> Send client\n"; + +// Server options specified or random +$options = array_merge([ + 'uri' => 'ws://localhost:8000', + 'opcode' => 'text', +], getopt('', ['uri:', 'opcode:', 'debug'])); +$message = array_pop($argv); + +// If debug mode and logger is available +if (isset($options['debug']) && class_exists('WebSocket\EchoLog')) { + $logger = new EchoLog(); + $options['logger'] = $logger; + echo "> Using logger\n"; +} + +try { + // Create client, send and recevie + $client = new Client($options['uri'], $options); + $client->send($message, $options['opcode']); + echo "> Sent '{$message}' [opcode: {$options['opcode']}]\n"; + if (in_array($options['opcode'], ['text', 'binary'])) { + $message = $client->receive(); + $opcode = $client->getLastOpcode(); + if (!is_null($message)) { + echo "> Got '{$message}' [opcode: {$opcode}]\n"; + } + } + $client->close(); + echo "> Closing client\n"; +} catch (\Throwable $e) { + echo "ERROR: {$e->getMessage()} [{$e->getCode()}]\n"; +} |