|
| 1 | +Classes: [Client](Classes/Client.md) • Server |
| 2 | + |
| 3 | +# Server class |
| 4 | + |
| 5 | +Websocket Server class. Support multiple connections through the `listen()` method. |
| 6 | + |
| 7 | +## Class synopsis |
| 8 | + |
| 9 | +```php |
| 10 | +WebSocket\Server implements Psr\Log\LoggerAwareInterface { |
| 11 | + |
| 12 | + // Magic methods |
| 13 | + public __construct(array $options = []) |
| 14 | + public __toString() : string |
| 15 | + |
| 16 | + // Server operations |
| 17 | + public listen(Closure $callback) : mixed |
| 18 | + public stop(): void |
| 19 | + |
| 20 | + // Server option functions |
| 21 | + public getPort() : int |
| 22 | + public setTimeout(int $seconds) : void |
| 23 | + public setFragmentSize(int $fragment_size) : self |
| 24 | + public getFragmentSize() : int |
| 25 | + |
| 26 | + // Connection broadcast operations |
| 27 | + public text(string $payload) : void |
| 28 | + public binary(string $payload) : void |
| 29 | + public ping(string $payload = '') : void |
| 30 | + public pong(string $payload = '') : void |
| 31 | + public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void |
| 32 | + public close(int $status = 1000, mixed $message = 'ttfn') : void |
| 33 | + public disconnect() : void |
| 34 | + public receive() : mixed |
| 35 | + |
| 36 | + // Provided by Psr\Log\LoggerAwareTrait |
| 37 | + public setLogger(Psr\Log\LoggerInterface $logger) : void |
| 38 | + |
| 39 | + // Deprecated functions |
| 40 | + public accept() : bool |
| 41 | + public getPath() : string |
| 42 | + public getRequest() : array |
| 43 | + public getHeader(string $header_name) : string|null |
| 44 | + public getLastOpcode() : string |
| 45 | + public getCloseStatus() : int |
| 46 | + public isConnected() : bool |
| 47 | + public getName() : string|null |
| 48 | + public getPeer() : string|null |
| 49 | + public getPier() : string|null |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## __construct |
| 54 | + |
| 55 | +Constructor for Websocket Server. |
| 56 | + |
| 57 | +#### Description |
| 58 | + |
| 59 | +```php |
| 60 | +public function __construct(array $options = []) |
| 61 | +``` |
| 62 | + |
| 63 | +#### Parameters |
| 64 | + |
| 65 | +###### `options` |
| 66 | + |
| 67 | +An optional array of parameters. |
| 68 | +| Name | Type | | Default | Description | |
| 69 | +| --- | --- | --- | --- [ |
| 70 | +| `filter` | `array` | `['text', 'binary']` | Array of opcodes to return on receive and listen functions |
| 71 | +| `fragment_size` | `int` | `4096` | Maximum payload size |
| 72 | +| `logger` | `Psr\Log\LoggerInterface` | `Psr\Log\NullLogger` |A [PSR-3](https://www.php-fig.org/psr/psr-3/) compatible logger |
| 73 | +| `port` | `int` | `8000` | The server port to listen to |
| 74 | +| `return_obj` | `bool` | `false` | Return a [Message](Classes/Message.md) instance on receive function |
| 75 | +| `timeout` | `int` | `5` | Time out in seconds |
| 76 | + |
| 77 | +#### Return Values |
| 78 | + |
| 79 | +Returns a new WebSocket\Server instance. |
| 80 | + |
| 81 | +#### Errors/Exceptions |
| 82 | + |
| 83 | +Emits [ConnectionException](Classes/ConnectionException.md) on failure. |
| 84 | + |
| 85 | +#### Examples |
| 86 | + |
| 87 | +```php |
| 88 | +<?php |
| 89 | + |
| 90 | +// Without options |
| 91 | +$server = new WebSocket\Server(); |
| 92 | + |
| 93 | +// With options |
| 94 | +$server = new WebSocket\Server(['port' => 8080, 'timeout' => 60]); |
| 95 | + |
| 96 | +?> |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +## __toString |
| 101 | + |
| 102 | +Get string representation of instance. |
| 103 | + |
| 104 | +#### Description |
| 105 | + |
| 106 | +```php |
| 107 | +public function __toString() : string |
| 108 | +``` |
| 109 | + |
| 110 | +#### Return Values |
| 111 | + |
| 112 | +Returns a string to represent current instance. |
| 113 | + |
| 114 | + |
| 115 | +## listen |
| 116 | + |
| 117 | +Set server to listen to incoming requests. |
| 118 | + |
| 119 | +#### Description |
| 120 | + |
| 121 | +```php |
| 122 | +public function listen(Closure $callback) : mixed |
| 123 | +``` |
| 124 | + |
| 125 | +#### Parameters |
| 126 | + |
| 127 | +###### `callback` |
| 128 | + |
| 129 | +A callback function that is triggered whenever the server receives a message matching the filter. |
| 130 | + |
| 131 | +The callback takes two parameters; |
| 132 | +* The [Message](Classes/Message/Message.md) that has been received |
| 133 | +* The [Connection](Classes/Connection.md) the server has receievd on, can be `null` if connection is closed |
| 134 | + |
| 135 | +If callback function returns non-null value, the listener will halt and return that value. |
| 136 | +Otherwise it will continue listening and propagating messages. |
| 137 | + |
| 138 | +#### Return Values |
| 139 | + |
| 140 | +Returns any non-null value returned by callback function. |
| 141 | + |
| 142 | +#### Errors/Exceptions |
| 143 | + |
| 144 | +Emits [ConnectionException](Classes/ConnectionException.md) on failure. |
| 145 | + |
| 146 | +#### Examples |
| 147 | + |
| 148 | +Minimal setup that continuously listens to incoming text and binary messages. |
| 149 | +```php |
| 150 | +<?php |
| 151 | + |
| 152 | +$server = new WebSocket\Server(); |
| 153 | +$server->listen(function ($message, $connection) { |
| 154 | + echo $message->getContent(); |
| 155 | +}); |
| 156 | +?> |
| 157 | +``` |
| 158 | + |
| 159 | +Listen to all incoming message types and respond with a text message. |
| 160 | +```php |
| 161 | +<?php |
| 162 | + |
| 163 | +$server = new WebSocket\Server(['filter' => ['text', 'binary', 'ping', 'pong', 'close']]); |
| 164 | +$server->listen(function ($message, $connection) { |
| 165 | + if (!$connection) { |
| 166 | + $connection->text("Confirm " . $message->getOpcode()); |
| 167 | + } |
| 168 | +}); |
| 169 | +?> |
| 170 | +``` |
| 171 | + |
| 172 | +Halt listener and return a value to calling code. |
| 173 | +```php |
| 174 | +<?php |
| 175 | + |
| 176 | +$server = new WebSocket\Server(['filter' => ['text', 'binary', 'ping', 'pong', 'close']]); |
| 177 | +$content = $server->listen(function ($message, $connection) { |
| 178 | + return $message->getContent(); |
| 179 | +}); |
| 180 | +echo $content; |
| 181 | +?> |
| 182 | +``` |
| 183 | + |
| 184 | +## stop |
| 185 | + |
| 186 | +Tell server to stop listening to incoming requests. |
| 187 | + |
| 188 | +#### Description |
| 189 | + |
| 190 | +```php |
| 191 | +public function stop(): void |
| 192 | +``` |
| 193 | + |
| 194 | +#### Examples |
| 195 | + |
| 196 | +Use stop() in listener. |
| 197 | +```php |
| 198 | +<?php |
| 199 | + |
| 200 | +$server = new WebSocket\Server(); |
| 201 | +while (true) { |
| 202 | + $server->listen(function ($message, $connection) use ($server) { |
| 203 | + echo $message->getContent(); |
| 204 | + $server->stop(); |
| 205 | + }); |
| 206 | + // Do things, listener will be restarted in next loop. |
| 207 | +} |
| 208 | +?> |
| 209 | +``` |
| 210 | + |
| 211 | +## getPort |
| 212 | + |
| 213 | +#### Description |
| 214 | + |
| 215 | +```php |
| 216 | +public function getPort(): void |
| 217 | +``` |
| 218 | + |
| 219 | +## setTimeout |
| 220 | + |
| 221 | +#### Description |
| 222 | + |
| 223 | +```php |
| 224 | +public function setTimeout(int $seconds): void |
| 225 | +``` |
| 226 | + |
| 227 | +## setFragmentSize |
| 228 | + |
| 229 | +#### Description |
| 230 | + |
| 231 | +```php |
| 232 | +public function setFragmentSize(int $fragment_size): self |
| 233 | +``` |
| 234 | + |
| 235 | +## getFragmentSize |
| 236 | + |
| 237 | +#### Description |
| 238 | + |
| 239 | +```php |
| 240 | +public function getFragmentSize(): int |
| 241 | +``` |
| 242 | + |
| 243 | +## text |
| 244 | + |
| 245 | +#### Description |
| 246 | + |
| 247 | +```php |
| 248 | +public function text(string $payload) : void |
| 249 | +``` |
| 250 | + |
| 251 | +## binary |
| 252 | + |
| 253 | +#### Description |
| 254 | + |
| 255 | +```php |
| 256 | +public function binary(string $payload) : void |
| 257 | +``` |
| 258 | + |
| 259 | +## ping |
| 260 | + |
| 261 | +#### Description |
| 262 | + |
| 263 | +```php |
| 264 | +public function ping(string $payload = '') : void |
| 265 | +``` |
| 266 | + |
| 267 | +## pong |
| 268 | + |
| 269 | +#### Description |
| 270 | + |
| 271 | +```php |
| 272 | +public function pong(string $payload = '') : void |
| 273 | +``` |
| 274 | + |
| 275 | +## send |
| 276 | + |
| 277 | +#### Description |
| 278 | + |
| 279 | +```php |
| 280 | +public function send(mixed $payload, string $opcode = 'text', bool $masked = true) : void |
| 281 | +``` |
| 282 | + |
| 283 | +## close |
| 284 | + |
| 285 | +#### Description |
| 286 | + |
| 287 | +```php |
| 288 | +public function close(int $status = 1000, mixed $message = 'ttfn') : void |
| 289 | +``` |
| 290 | + |
| 291 | +## disconnect |
| 292 | + |
| 293 | +#### Description |
| 294 | + |
| 295 | +```php |
| 296 | +public function disconnect() : void |
| 297 | +``` |
| 298 | + |
| 299 | +## receive |
| 300 | + |
| 301 | +#### Description |
| 302 | + |
| 303 | +```php |
| 304 | +public function receive() : mixed |
| 305 | +``` |
| 306 | + |
| 307 | +## setLogger |
| 308 | + |
| 309 | +#### Description |
| 310 | + |
| 311 | +```php |
| 312 | +public setLogger(Psr\Log\LoggerInterface $logger) : void |
| 313 | +``` |
0 commit comments