arthurkushman /
php-wss
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WSSCTEST; |
||
| 4 | |||
| 5 | use WSSC\Contracts\ConnectionContract; |
||
| 6 | use WSSC\Contracts\WebSocket; |
||
| 7 | use WSSC\Exceptions\WebSocketException; |
||
| 8 | use Monolog\Logger; |
||
| 9 | use Monolog\Handler\StreamHandler; |
||
| 10 | |||
| 11 | class ServerHandler extends WebSocket |
||
| 12 | { |
||
| 13 | /* |
||
| 14 | * if You need to parse URI context like /messanger/chat/JKN324jn4213 |
||
| 15 | * You can do so by placing URI parts into an array - $pathParams, when Socket will receive a connection |
||
| 16 | * this variable will be appropriately set to key => value pairs, ex.: ':context' => 'chat' |
||
| 17 | * Otherwise leave $pathParams as an empty array |
||
| 18 | */ |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array|string[] |
||
| 22 | */ |
||
| 23 | public array $pathParams = [':entity', ':context', ':token']; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private array $clients = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Logger |
||
| 32 | */ |
||
| 33 | private Logger $log; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ServerHandler constructor. |
||
| 37 | * |
||
| 38 | * @throws \Exception |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | // create a log channel |
||
| 43 | $this->log = new Logger('ServerSocket'); |
||
| 44 | $this->log->pushHandler(new StreamHandler('./tests/tests.log')); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function onOpen(ConnectionContract $conn) |
||
| 48 | { |
||
| 49 | $this->clients[$conn->getUniqueSocketId()] = $conn; |
||
| 50 | $this->log->debug('Connection opened, total clients: ' . count($this->clients)); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function onMessage(ConnectionContract $recv, $msg) |
||
| 54 | { |
||
| 55 | $this->log->debug('Received message: ' . $msg); |
||
| 56 | $recv->send($msg); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function onClose(ConnectionContract $conn) |
||
| 60 | { |
||
| 61 | unset($this->clients[$conn->getUniqueSocketId()]); |
||
| 62 | $this->log->debug('close: ' . print_r($this->clients, 1)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 63 | $conn->close(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param ConnectionContract $conn |
||
| 68 | * @param WebSocketException $ex |
||
| 69 | */ |
||
| 70 | public function onError(ConnectionContract $conn, WebSocketException $ex) |
||
| 71 | { |
||
| 72 | echo 'Error occured: ' . $ex->printStack(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * You may want to implement these methods to bring ping/pong events |
||
| 77 | * |
||
| 78 | * @param ConnectionContract $conn |
||
| 79 | * @param string $msg |
||
| 80 | */ |
||
| 81 | public function onPing(ConnectionContract $conn, $msg) |
||
| 82 | { |
||
| 83 | // TODO: Implement onPing() method. |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param ConnectionContract $conn |
||
| 88 | * @param $msg |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | public function onPong(ConnectionContract $conn, $msg) |
||
| 92 | { |
||
| 93 | // TODO: Implement onPong() method. |
||
| 94 | } |
||
| 95 | } |
||
| 96 |