-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
77 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,6 @@ | |
|
||
namespace Bloatless\WebSocket\Application; | ||
|
||
/** | ||
* WebSocket Server Application | ||
* | ||
* @author Nico Kaiser <[email protected]> | ||
*/ | ||
abstract class Application implements ApplicationInterface | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,6 @@ | |
|
||
use Bloatless\WebSocket\Connection; | ||
|
||
/** | ||
* Websocket-Server demo and test application. | ||
* | ||
* @author Simon Samtleben <[email protected]> | ||
*/ | ||
class DemoApplication extends Application | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,6 @@ | |
|
||
namespace Bloatless\WebSocket; | ||
|
||
/** | ||
* Socket class | ||
* | ||
* @author Moritz Wutz <[email protected]> | ||
* @author Nico Kaiser <[email protected]> | ||
* @version 0.2 | ||
*/ | ||
class Socket | ||
{ | ||
/** | ||
|