From 3f8d2086935266c6b5f488783ca7eb0d4ece2a22 Mon Sep 17 00:00:00 2001 From: Lorenzo Monaco Date: Sun, 24 Apr 2016 15:42:35 +0200 Subject: [PATCH] Added examples --- examples/CustomReader.php | 91 +++++++++++++++++++++++++++++++++++++++ src/SBS/Reader.php | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 examples/CustomReader.php diff --git a/examples/CustomReader.php b/examples/CustomReader.php new file mode 100644 index 0000000..89a2160 --- /dev/null +++ b/examples/CustomReader.php @@ -0,0 +1,91 @@ + + * @copyright Copyright (c) 2016 Lorenzo Monaco (http://www.lnxlabs.it/) + * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3 + * @filesource + */ + +/** Error reporting */ +error_reporting(E_ALL); +ini_set('display_errors', true); +ini_set('display_startup_errors', true); +date_default_timezone_set('Europe/London'); + +/** SBS Autoloader */ +spl_autoload_register(function ($class) { + $classFile = str_replace('\\', '/', $class).'.php'; + if (file_exists(__DIR__.'/../src/'.$classFile)) { + require_once __DIR__.'/../src/'.$classFile; + } +}); + +/** Define custom reader that extends \SBS\Reader */ +class CustomReader extends \SBS\Reader { + + /** Overwrite default onConnect method */ + public function onConnect($hostname, $port, $timeout) { + + /** Print a message with connection data */ + printf('Reader connected to %s:%d', $hostname, $port); + + } + + /** Overwrite default onDataReceive method */ + public function onDataReceive($data) { + + /** Print a message with received data size */ + printf('Data received: array(%d)', count($data)); + + /** Return true to continue message process */ + return true; + + } + + /** Overwrite default onMessage method */ + public function onMessage($message, $type, $transmission) { + + /** Print a message with received message type */ + printf('New %s message received', $type); + + } + + /** Overwrite default onError method */ + public function onError($errno, $error) { + + /** Print a message with error number and error message */ + printf('Error %d: %s', $errno, $error); + + } + +} + +/** Create a new CustomReader that connects to localhost:30003 */ +$reader = new CustomReader('localhost', 30003); + +/** Connect the reader */ +if ($reader->connect()) { + + /** If connected, run main reader loop */ + $reader->run(); + +} diff --git a/src/SBS/Reader.php b/src/SBS/Reader.php index 61af7a2..d0b54bb 100644 --- a/src/SBS/Reader.php +++ b/src/SBS/Reader.php @@ -43,7 +43,7 @@ * *
<?php
  *
- * class MyReader extends \SBS\Reader {
+ * class CustomReader extends \SBS\Reader {
  *
  *   public function onConnect($hostname, $port, $timeout) {
  *     printf('Reader connected to %s:%d', $hostname, $port);
@@ -68,7 +68,7 @@
  *
  * }
  *
- * $reader = new MyReader('localhost', 30003);
+ * $reader = new CustomReader('localhost', 30003);
  * if ($reader->connect()) {
  *   $reader->run();
  * }