|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2014-2025 Textalk and contributors. |
| 5 | + * This file is part of Websocket PHP and is free software under the ISC License. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WebSocket; |
| 9 | + |
| 10 | +use Phrity\Net\{ |
| 11 | + Context, |
| 12 | + StreamFactory, |
| 13 | +}; |
| 14 | +use Psr\Log\{ |
| 15 | + LoggerAwareInterface, |
| 16 | + LoggerInterface, |
| 17 | +}; |
| 18 | +use Stringable; |
| 19 | +use Throwable; |
| 20 | +use WebSocket\Exception\{ |
| 21 | + ServerException |
| 22 | +}; |
| 23 | +use WebSocket\Http\{ |
| 24 | + DefaultHttpFactory, |
| 25 | + Response, |
| 26 | + ServerRequest, |
| 27 | +}; |
| 28 | +use WebSocket\Trait\{ |
| 29 | + ConfigurationTrait, |
| 30 | + StringableTrait |
| 31 | +}; |
| 32 | +use WebSocket\Runtime\Watcher; |
| 33 | + |
| 34 | +/** |
| 35 | + * WebSocket\Server class. |
| 36 | + * Entry class for WebSocket server. |
| 37 | + */ |
| 38 | +class Cluster implements LoggerAwareInterface, Stringable |
| 39 | +{ |
| 40 | + use ConfigurationTrait; |
| 41 | + use StringableTrait; |
| 42 | + |
| 43 | + private StreamFactory $streamFactory; |
| 44 | + private Watcher $watcher; |
| 45 | + |
| 46 | + private array $services = []; |
| 47 | + |
| 48 | + |
| 49 | + /* ---------- Magic methods ------------------------------------------------------------------------------------ */ |
| 50 | + |
| 51 | + /** |
| 52 | + * @param StreamFactory|null $streamFactory |
| 53 | + * @param Watcher|null $watcher |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + StreamFactory|null $streamFactory = null, |
| 57 | + Watcher|null $watcher = null, |
| 58 | + Configuration|null $configuration = null, |
| 59 | + ) { |
| 60 | + $this->streamFactory = $streamFactory ?? new StreamFactory(); |
| 61 | + $this->watcher = $watcher ?? new Watcher($this->streamFactory->createStreamCollection()); |
| 62 | + $this->initConfiguration($configuration); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get string representation of instance. |
| 67 | + * @return string String representation |
| 68 | + */ |
| 69 | + public function __toString(): string |
| 70 | + { |
| 71 | + return $this->stringable(''); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + public function createServer(int $port = 80, bool $ssl = false, string|null $name = null): Server |
| 77 | + { |
| 78 | + $name = $name ?? "server:{$port}"; |
| 79 | + if (array_key_exists($name, $this->services)) { |
| 80 | + throw new ServerException("Server {$name} already attached."); |
| 81 | + } |
| 82 | + $server = new Server( |
| 83 | + port: $port, |
| 84 | + ssl: $ssl, |
| 85 | + streamFactory: $this->streamFactory, |
| 86 | + watcher: $this->watcher, |
| 87 | + configuration: $this->configuration, |
| 88 | + ); |
| 89 | + $this->services[$name] = $server; |
| 90 | + return $server; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set logger. |
| 95 | + * @param LoggerInterface $logger Logger implementation |
| 96 | + */ |
| 97 | + public function setLogger(LoggerInterface $logger): void |
| 98 | + { |
| 99 | + $this->configuration->setLogger($logger); |
| 100 | + } |
| 101 | + |
| 102 | + public function start() |
| 103 | + { |
| 104 | + foreach ($this->services as $service) { |
| 105 | + $service->start(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments