From 86973a512e9624301d57686228aebf23b6489bf5 Mon Sep 17 00:00:00 2001 From: Simon Samtleben Date: Sun, 10 Mar 2019 11:16:02 +0100 Subject: [PATCH] Update readme, phpdocs, ... --- README.md | 98 +++++++++++++++++++-------- cli/client_demo.php | 2 +- cli/server.php | 3 - src/Application/Application.php | 5 -- src/Application/DemoApplication.php | 5 -- src/Application/StatusApplication.php | 6 -- src/Client.php | 7 +- src/Connection.php | 6 -- src/Server.php | 5 +- src/Socket.php | 7 -- 10 files changed, 77 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 1029917..76df72b 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,81 @@ -PHP WebSocket -============= -A websocket server implemented in php. +

+ +

-- Supports websocket draft hybi-10,13 (Currently tested with Chrome 18 and Firefox 11). -- Supports origin-check. -- Supports various security/performance settings. -- Supports binary frames. (Currently receive only) -- Supports wss. (Needs valid certificate in Firefox.) -- Application module, the server can be extended by custom behaviors. +

Bloatless PHP WebSockets

-## Bugs/Todos/Hints -- Add support for fragmented frames. +

+ Simple WebSocket server and client implemented in PHP. +

-## Server example +## About -This creates a server on localhost:8000 with one Application that listens on `ws://localhost:8000/demo`: +This application is an extremely simple implementation of the [WebSocket Protocol](https://tools.ietf.org/html/rfc6455) +in PHP. It includes a server as well as a client. This implementation is optimal to get started with WebSockets and +learn something. As soon as you want to create a full featured websocket based application you might want to switch +to more sophisticated solution. - $server = new \WebSocket\Server('127.0.0.1', 8000, false); // host,port,ssl +## Installation - // server settings: - $server->setCheckOrigin(true); - $server->setAllowedOrigin('foo.lh'); - $server->setMaxClients(100); - $server->setMaxConnectionsPerIp(20); - $server->setMaxRequestsPerMinute(1000); +Clone or download the repository to your server. - $server->registerApplication('demo', \WebSocket\Application\DemoApplication::getInstance()); - $server->run(); +### Requirements -## Libraries used +* PHP >= 7.2 -- [SplClassLoader](http://gist.github.com/221634) by the PHP Standards Working Group -- [jQuery](http://jquery.com/) -- [CoffeeScript PHP] (https://github.com/alxlit/coffeescript-php) +Hint: You can use version 1.0 if you're still on PHP5. -## Demo -- Check out http://jitt.li for a sample-project using this websocket server. \ No newline at end of file +## Usage + +* Adjust `cli/server.php` to your requirements. +* Run: `php cli/server.php` + +This will start a websocket server. (By default on localhost:8000) + +### Server example + +This will create a websocket server listening on port 8000. + +There a two applications registred to the server. The demo application will be available at `ws://localhost:8000/demo` +and the status application will be available at `ws://localhost:8000/status`. + +```php +// Require neccessary files here... + +$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000); + +// Server settings: +$server->setMaxClients(100); +$server->setCheckOrigin(false); +$server->setAllowedOrigin('foo.lh'); +$server->setMaxConnectionsPerIp(100); +$server->setMaxRequestsPerMinute(2000); + +// Add your applications here: +$server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance()); +$server->registerApplication('demo', \Bloatless\WebSocket\Application\DemoApplication::getInstance()); + +$server->run(); + +``` + +### Client example + +This creates a WebSocket cliente, connects to a server and sends a message to the server: + +```php +$client = new \Bloatless\WebSocket\Client; +$client->connect('127.0.0.1', 8000, '/demo', 'foo.lh'); +$client->sendData([ + 'action' => 'echo', + 'data' => 'Hello Wolrd!' +]); +``` + +### Browser example + +The repository contains two demo-pages to call in your browser. You can find them in the `public` folder. +The `index.html` is a simple application which you can use to send messages to the server. + +The `status.html` will display various server information. \ No newline at end of file diff --git a/cli/client_demo.php b/cli/client_demo.php index d1d991f..22eba9f 100644 --- a/cli/client_demo.php +++ b/cli/client_demo.php @@ -2,7 +2,7 @@ ini_set('display_errors', 1); error_reporting(E_ALL); -require_once __DIR__ . '/../src/Client.php'; +require __DIR__ . '/../src/Client.php'; $clients = []; $testClients = 30; diff --git a/cli/server.php b/cli/server.php index 38b6e8e..102c879 100644 --- a/cli/server.php +++ b/cli/server.php @@ -1,8 +1,5 @@ - */ abstract class Application implements ApplicationInterface { /** diff --git a/src/Application/DemoApplication.php b/src/Application/DemoApplication.php index f7f536d..cf85cf1 100644 --- a/src/Application/DemoApplication.php +++ b/src/Application/DemoApplication.php @@ -6,11 +6,6 @@ use Bloatless\WebSocket\Connection; -/** - * Websocket-Server demo and test application. - * - * @author Simon Samtleben - */ class DemoApplication extends Application { /** diff --git a/src/Application/StatusApplication.php b/src/Application/StatusApplication.php index 40ddd77..6469453 100644 --- a/src/Application/StatusApplication.php +++ b/src/Application/StatusApplication.php @@ -6,12 +6,6 @@ use Bloatless\WebSocket\Connection; -/** - * Shiny WSS Status Application - * Provides live server infos/messages to client/browser. - * - * @author Simon Samtleben - */ class StatusApplication extends Application { /** diff --git a/src/Client.php b/src/Client.php index 276875c..c05a770 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,11 +5,10 @@ namespace Bloatless\WebSocket; /** - * Very basic websocket client. - * Supporting draft hybi-10. + * Simple WebSocket client. * - * @author Simon Samtleben - * @version 2011-10-18 + * @author Simon Samtleben + * @version 2.0 */ class Client { diff --git a/src/Connection.php b/src/Connection.php index a84b14f..9c9dee0 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,12 +6,6 @@ use Bloatless\WebSocket\Application\ApplicationInterface; -/** - * WebSocket Connection class - * - * @author Nico Kaiser - * @author Simon Samtleben - */ class Connection { public $waitingForData = false; diff --git a/src/Server.php b/src/Server.php index 272b14a..e07399a 100644 --- a/src/Server.php +++ b/src/Server.php @@ -7,10 +7,11 @@ use Bloatless\WebSocket\Application\ApplicationInterface; /** - * Shiny WSS + * Simple WebSocket server implementation in PHP. * + * @author Simon Samtleben * @author Nico Kaiser - * @author Simon Samtleben + * @version 2.0 */ class Server extends Socket { diff --git a/src/Socket.php b/src/Socket.php index 03d11f1..e2cb964 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -4,13 +4,6 @@ namespace Bloatless\WebSocket; -/** - * Socket class - * - * @author Moritz Wutz - * @author Nico Kaiser - * @version 0.2 - */ class Socket { /**