Skip to content

Commit 02ffae3

Browse files
committed
Fixup
1 parent 6e607e0 commit 02ffae3

4 files changed

Lines changed: 116 additions & 24 deletions

File tree

docs/Migrate_3_4.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
# Websocket: Migration v3 -> v4
44

5-
Version 4.x has few changes compared to previous version.
5+
Version 4.x has few breaking changes compared to previous version.
66

77
## Breaking changes
88

9+
* Client `setContext()` - no longer accepts array input
10+
* Server `setContext()` - no longer accepts array input
911

10-
## Removed deprecated code
12+
## Removed deprecated methods
1113

1214
* Client `getMeta()` - removed
1315
* Client `onConnect()` - use `onHandshake()` instead
14-
* Client `setContext()` - no longer accepts array input
1516
* Server `onConnect()` - use `onHandshake()` instead
16-
* Server `setContext()` - no longer accepts array input
1717

1818
## Configuration management
1919

20-
v4 uses the Configuration class to propagate configurations throughout internal classes.
20+
`v4` uses the `Configuration` class to propagate configurations throughout internal classes.
2121

2222
This affects the following classes;
2323
* Connection
@@ -26,7 +26,7 @@ This affects the following classes;
2626
* MiddlewareHandler
2727
* All middlewares
2828

29-
He following methods (when applicable) are removed;
29+
The following methods (when applicable) are removed;
3030
* `setLogger()`
3131
* `getTimeout()`
3232
* `setTimeout()`
@@ -48,3 +48,5 @@ $source->setConfiguration($clonedConfiguration);
4848

4949
## Extending
5050

51+
Increased modularization affects many internal classes and methods.
52+
If you rely on using these directly (extending or adapting) your code may be incompatible.

src/Client.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,6 @@ public function disconnect(): void
540540

541541
/* ---------- Connection wrapper methods ----------------------------------------------------------------------- */
542542

543-
public function getIdentity(): string
544-
{
545-
return sprintf(
546-
'client/%s/%s',
547-
$this->getName() ?: 'closed',
548-
$this->getRemoteName() ?: 'closed',
549-
);
550-
}
551-
552543
/**
553544
* Get name of local socket, or null if not connected.
554545
* @return string|null

src/Cluster.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

src/Connection.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,6 @@ public function closeWrite(): self
197197

198198
/* ---------- Connection state --------------------------------------------------------------------------------- */
199199

200-
public function getIdentity(): string
201-
{
202-
return sprintf(
203-
'connection/%s/%s',
204-
$this->getName() ?: 'closed',
205-
$this->getRemoteName() ?: 'closed',
206-
);
207-
}
208-
209200
/**
210201
* Get name of local socket, or null if not connected.
211202
* @return string

0 commit comments

Comments
 (0)