|
| 1 | +[Documentation](Index.md) / Migration v1 -> v2 |
| 2 | + |
| 3 | +# Websocket: Migration v1 -> v2 |
| 4 | + |
| 5 | +Version `2.x` has significant changes compared to previous version. |
| 6 | +Amongst other things, it introduces callback listeners, middleware support, and the Server now support multiple connections. |
| 7 | + |
| 8 | +## Constructor and configuration |
| 9 | + |
| 10 | +Where `1.x` was configured by using an array of options on the constructor, |
| 11 | +`2.x` provides a number of configuration methods instead. |
| 12 | +Replace configuration with a call to corresponding method instead. |
| 13 | + |
| 14 | +```php |
| 15 | +new WebSocket\Client(string $uri) |
| 16 | +new WebSocket\Server(bool $ssl, int $port) |
| 17 | + |
| 18 | +setLogger(Psr\Log\LoggerInterface $logger) // logger |
| 19 | +setTimeout(int $timeout) // timeout |
| 20 | +setFrameSize(int $frameSize) // fragment_size |
| 21 | +setPersistent(bool $persistent) // persistent |
| 22 | +setContext(array $context_as_array) // context |
| 23 | +addHeader(string $name, string $value) // headers |
| 24 | +``` |
| 25 | + |
| 26 | +The `filter` and `return_obj` are no longer valid. |
| 27 | + |
| 28 | +## Middlewares |
| 29 | + |
| 30 | +`2.x` introduces middlewares to extend functionality to Client and Server. |
| 31 | +Two of the middlewares cover functionality built-in in `1.x` and should be added unless you create your own code instead. |
| 32 | + |
| 33 | +```php |
| 34 | +addMiddleware(new WebSocket\Middleware\CloseHandler()) |
| 35 | +addMiddleware(new WebSocket\Middleware\PingResponder()) |
| 36 | +``` |
| 37 | + |
| 38 | +## Receiving messages |
| 39 | + |
| 40 | +The Client `receive()` method always return an instance of Message (Text, Binary, Ping, Pong or Close). |
| 41 | +This corresponds to setting `return_obj: true` in `1.x` configuration. By default `1.x` returned string. |
| 42 | + |
| 43 | +You are encouraged to read incoming messages using [listener callback methods](https://github.com/sirn-se/websocket-php/blob/2.0.0/docs/Client.md#subscribe-operation) instead. |
| 44 | + |
| 45 | +The Server no longer has the `receive()` method. |
| 46 | +Receiving, use [listener callback methods](https://github.com/sirn-se/websocket-php/blob/2.0.0/docs/Server.md#message-listeners). |
| 47 | + |
| 48 | +## Sending messages |
| 49 | + |
| 50 | +The `send(...)` method no longer accepts message and opcode as strings, |
| 51 | +but only accepts an instance of Message (Text, Binary, Ping, Pong or Close). |
| 52 | +However, there are convenience methods available as `text(...)`, `binary(...)`, `ping(...)`, `pong(...)` and `close(...)`. |
| 53 | + |
| 54 | +## More? |
| 55 | + |
| 56 | +As `2.x` has significant internal code changes, other classes and methods are likely to have changed. |
0 commit comments