Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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";
}
}
```