Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frago9876543210/ws
Lightweight multithreaded websocket server written in PHP
https://github.com/frago9876543210/ws
php-parallel php-websocket-server phpstan-l8 websocket
Last synced: 3 months ago
JSON representation
Lightweight multithreaded websocket server written in PHP
- Host: GitHub
- URL: https://github.com/frago9876543210/ws
- Owner: Frago9876543210
- License: mit
- Created: 2020-04-01T12:25:33.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-02T09:21:25.000Z (almost 5 years ago)
- Last Synced: 2024-04-04T18:01:38.576Z (11 months ago)
- Topics: php-parallel, php-websocket-server, phpstan-l8, websocket
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ws
[![Build Status](https://travis-ci.org/Frago9876543210/ws.svg?branch=master)](https://travis-ci.org/Frago9876543210/ws)Lightweight multithreaded websocket server written in PHP
## Requirements
- PHP 7.3+
- [ext-parallel](https://github.com/krakjoe/parallel)## Example
Project tree
```
├── composer.json
└── src
├── MyListener.php
└── run.php
```### `composer.json`:
```json
{
"require": {
"frago9876543210/ws": "dev-master",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"app\\": "src/"
}
},
"minimum-stability": "dev"
}
```### `src/run.php`:
```php
getAddress()}\n";
}public function onMessage(Connection $connection, string $message) : void{
try{
$data = json_decode($message, true, 2, JSON_THROW_ON_ERROR);$validator = new Validator();
$validator->required("username")->string();
$validator->required("message")->string();if($validator->validate($data)->isValid()){
$connection->send($message, true);
echo "{$data["username"]} ({$connection->getAddress()}): {$data["message"]}\n";
}else{
$connection->close("Invalid data provided");
}
}catch(JsonException $e){
$connection->close("Failed to parse json: " . $e->getMessage());
}
}public function onDisconnect(Connection $connection) : void{
echo "- {$connection->getAddress()}\n";
}
}
```