|
1 |
| -PHP WebSocket |
2 |
| -============= |
3 |
| -A websocket server implemented in php. |
| 1 | +<p align="center"> |
| 2 | + <img src="https://bloatless.org/img/logo.svg" width="60px" height="80px"> |
| 3 | +</p> |
4 | 4 |
|
5 |
| -- Supports websocket draft hybi-10,13 (Currently tested with Chrome 18 and Firefox 11). |
6 |
| -- Supports origin-check. |
7 |
| -- Supports various security/performance settings. |
8 |
| -- Supports binary frames. (Currently receive only) |
9 |
| -- Supports wss. (Needs valid certificate in Firefox.) |
10 |
| -- Application module, the server can be extended by custom behaviors. |
| 5 | +<h1 align="center">Bloatless PHP WebSockets</h1> |
11 | 6 |
|
12 |
| -## Bugs/Todos/Hints |
13 |
| -- Add support for fragmented frames. |
| 7 | +<p align="center"> |
| 8 | + Simple WebSocket server and client implemented in PHP. |
| 9 | +</p> |
14 | 10 |
|
15 |
| -## Server example |
| 11 | +## About |
16 | 12 |
|
17 |
| -This creates a server on localhost:8000 with one Application that listens on `ws://localhost:8000/demo`: |
| 13 | +This application is an extremely simple implementation of the [WebSocket Protocol](https://tools.ietf.org/html/rfc6455) |
| 14 | +in PHP. It includes a server as well as a client. This implementation is optimal to get started with WebSockets and |
| 15 | +learn something. As soon as you want to create a full featured websocket based application you might want to switch |
| 16 | +to more sophisticated solution. |
18 | 17 |
|
19 |
| - $server = new \WebSocket\Server('127.0.0.1', 8000, false); // host,port,ssl |
| 18 | +## Installation |
20 | 19 |
|
21 |
| - // server settings: |
22 |
| - $server->setCheckOrigin(true); |
23 |
| - $server->setAllowedOrigin('foo.lh'); |
24 |
| - $server->setMaxClients(100); |
25 |
| - $server->setMaxConnectionsPerIp(20); |
26 |
| - $server->setMaxRequestsPerMinute(1000); |
| 20 | +Clone or download the repository to your server. |
27 | 21 |
|
28 |
| - $server->registerApplication('demo', \WebSocket\Application\DemoApplication::getInstance()); |
29 |
| - $server->run(); |
| 22 | +### Requirements |
30 | 23 |
|
31 |
| -## Libraries used |
| 24 | +* PHP >= 7.2 |
32 | 25 |
|
33 |
| -- [SplClassLoader](http://gist.github.com/221634) by the PHP Standards Working Group |
34 |
| -- [jQuery](http://jquery.com/) |
35 |
| -- [CoffeeScript PHP] (https://github.com/alxlit/coffeescript-php) |
| 26 | +Hint: You can use version 1.0 if you're still on PHP5. |
36 | 27 |
|
37 |
| -## Demo |
38 | 28 |
|
39 |
| -- Check out http://jitt.li for a sample-project using this websocket server. |
| 29 | +## Usage |
| 30 | + |
| 31 | +* Adjust `cli/server.php` to your requirements. |
| 32 | +* Run: `php cli/server.php` |
| 33 | + |
| 34 | +This will start a websocket server. (By default on localhost:8000) |
| 35 | + |
| 36 | +### Server example |
| 37 | + |
| 38 | +This will create a websocket server listening on port 8000. |
| 39 | + |
| 40 | +There a two applications registred to the server. The demo application will be available at `ws://localhost:8000/demo` |
| 41 | +and the status application will be available at `ws://localhost:8000/status`. |
| 42 | + |
| 43 | +```php |
| 44 | +// Require neccessary files here... |
| 45 | + |
| 46 | +$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000); |
| 47 | + |
| 48 | +// Server settings: |
| 49 | +$server->setMaxClients(100); |
| 50 | +$server->setCheckOrigin(false); |
| 51 | +$server->setAllowedOrigin('foo.lh'); |
| 52 | +$server->setMaxConnectionsPerIp(100); |
| 53 | +$server->setMaxRequestsPerMinute(2000); |
| 54 | + |
| 55 | +// Add your applications here: |
| 56 | +$server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance()); |
| 57 | +$server->registerApplication('demo', \Bloatless\WebSocket\Application\DemoApplication::getInstance()); |
| 58 | + |
| 59 | +$server->run(); |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +### Client example |
| 64 | + |
| 65 | +This creates a WebSocket cliente, connects to a server and sends a message to the server: |
| 66 | + |
| 67 | +```php |
| 68 | +$client = new \Bloatless\WebSocket\Client; |
| 69 | +$client->connect('127.0.0.1', 8000, '/demo', 'foo.lh'); |
| 70 | +$client->sendData([ |
| 71 | + 'action' => 'echo', |
| 72 | + 'data' => 'Hello Wolrd!' |
| 73 | +]); |
| 74 | +``` |
| 75 | + |
| 76 | +### Browser example |
| 77 | + |
| 78 | +The repository contains two demo-pages to call in your browser. You can find them in the `public` folder. |
| 79 | +The `index.html` is a simple application which you can use to send messages to the server. |
| 80 | + |
| 81 | +The `status.html` will display various server information. |
0 commit comments