Skip to content

Commit

Permalink
Update readme, phpdocs, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
nekudo committed Mar 10, 2019
1 parent 776729c commit 86973a5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 67 deletions.
98 changes: 70 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,81 @@
PHP WebSocket
=============
A websocket server implemented in php.
<p align="center">
<img src="https://bloatless.org/img/logo.svg" width="60px" height="80px">
</p>

- 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.
<h1 align="center">Bloatless PHP WebSockets</h1>

## Bugs/Todos/Hints
- Add support for fragmented frames.
<p align="center">
Simple WebSocket server and client implemented in PHP.
</p>

## 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.
## 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.
2 changes: 1 addition & 1 deletion cli/client_demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions cli/server.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

require __DIR__ . '/../src/Connection.php';
require __DIR__ . '/../src/Socket.php';
require __DIR__ . '/../src/Server.php';
Expand Down
5 changes: 0 additions & 5 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

namespace Bloatless\WebSocket\Application;

/**
* WebSocket Server Application
*
* @author Nico Kaiser <[email protected]>
*/
abstract class Application implements ApplicationInterface
{
/**
Expand Down
5 changes: 0 additions & 5 deletions src/Application/DemoApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

use Bloatless\WebSocket\Connection;

/**
* Websocket-Server demo and test application.
*
* @author Simon Samtleben <[email protected]>
*/
class DemoApplication extends Application
{
/**
Expand Down
6 changes: 0 additions & 6 deletions src/Application/StatusApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

use Bloatless\WebSocket\Connection;

/**
* Shiny WSS Status Application
* Provides live server infos/messages to client/browser.
*
* @author Simon Samtleben <[email protected]>
*/
class StatusApplication extends Application
{
/**
Expand Down
7 changes: 3 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
namespace Bloatless\WebSocket;

/**
* Very basic websocket client.
* Supporting draft hybi-10.
* Simple WebSocket client.
*
* @author Simon Samtleben <[email protected]>
* @version 2011-10-18
* @author Simon Samtleben <[email protected]>
* @version 2.0
*/
class Client
{
Expand Down
6 changes: 0 additions & 6 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

use Bloatless\WebSocket\Application\ApplicationInterface;

/**
* WebSocket Connection class
*
* @author Nico Kaiser <[email protected]>
* @author Simon Samtleben <[email protected]>
*/
class Connection
{
public $waitingForData = false;
Expand Down
5 changes: 3 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use Bloatless\WebSocket\Application\ApplicationInterface;

/**
* Shiny WSS
* Simple WebSocket server implementation in PHP.
*
* @author Simon Samtleben <[email protected]>
* @author Nico Kaiser <[email protected]>
* @author Simon Samtleben <[email protected]>
* @version 2.0
*/
class Server extends Socket
{
Expand Down
7 changes: 0 additions & 7 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

namespace Bloatless\WebSocket;

/**
* Socket class
*
* @author Moritz Wutz <[email protected]>
* @author Nico Kaiser <[email protected]>
* @version 0.2
*/
class Socket
{
/**
Expand Down

0 comments on commit 86973a5

Please sign in to comment.