-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
228 lines (185 loc) · 6.49 KB
/
Server.php
File metadata and controls
228 lines (185 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
namespace Octamp\Server;
use Octamp\Server\Adapter\AdapterInterface;
use Octamp\Server\Connection\Connection;
use Octamp\Server\Connection\ConnectionStorage;
use Octamp\Server\Generator\DefaultIDGenerator;
use Octamp\Server\Generator\IDGeneratorInterface;
use Octamp\Server\Generator\ServerIdGeneratorInterface;
use OpenSwoole\Constant;
use OpenSwoole\Http\Request;
use OpenSwoole\WebSocket\Frame;
use OpenSwoole\WebSocket\Server as WebsocketServer;
class Server implements ServerInterface
{
public ?string $serverId = null;
private ?AdapterInterface $adapter = null;
protected array $callbacks = [];
protected ?ConnectionStorage $connectionStorage = null;
protected ?ServerIdGeneratorInterface $idGenerator = null;
protected bool $ready = false;
public static function createWebsocketServer(string $host, int $port, array $options = []): WebsocketServer
{
\OpenSwoole\Coroutine::set(['hook_flags' => \OpenSwoole\Runtime::HOOK_ALL]);
\OpenSwoole\Runtime::enableCoroutine(true);
$server = new WebsocketServer($host, $port, WebsocketServer::POOL_MODE, Constant::SOCK_TCP);
$server->set($options);
return $server;
}
public function __construct(private WebsocketServer $server)
{
$this->initWebsocket();
}
private function initWebsocket(): void
{
$this->server->on("Start", function(WebsocketServer $server) {
echo 'Server Started:' . $server->host . ':' . $server->port . PHP_EOL;
$this->dispatch('serverStart');
});
$this->server->on('Open', function(WebsocketServer $server, Request $request) {
$this->onOpen($request);
});
$this->server->on('Message', function(WebsocketServer $server, Frame $frame) {
$this->onMessage($frame);
});
$this->server->on('Close', function(WebsocketServer $server, int $fd) {
$this->onClose($fd);
});
$this->server->on('WorkerStart', function (WebsocketServer $server, int $workerId) {
$this->onStart();
});
}
public function start(): void
{
$this->server->start();
}
protected function onStart(): void
{
if ($this->ready) {
return;
}
$this->dispatch('beforeStart');
if ($this->serverId === null) {
if ($this->idGenerator === null) {
$this->setGenerator(new DefaultIDGenerator());
}
$this->serverId = $this->idGenerator->generateServerId($this->server->worker_id);
}
echo 'Server Worker ID: ' . $this->serverId . PHP_EOL;
if ($this->adapter === null) {
$this->server->stop($this->server->worker_id);
throw new \RuntimeException('Need to have adapter');
}
$this->adapter->start($this->serverId);
$this->connectionStorage = new ConnectionStorage($this, $this->adapter);
$this->adapter->subscribe('client:push', function (?string $fromServerId, string $serverId, int $fd, string $data) {
if ($this->serverId === $serverId) {
$connection = $this->connectionStorage->getUsingServerFd($serverId, $fd);
$connection->send($data);
}
});
$this->dispatch('afterStart');
$this->ready = true;
}
protected function onOpen(Request $request): void
{
if (!$this->ready) {
$this->server->close($request->fd);
return;
}
$connection = new Connection($request, $this);
$this->connectionStorage->save($connection);
$this->dispatch('open', $connection);
}
protected function onMessage(Frame $frame): void
{
if (!$this->ready) {
$this->server->close($frame->fd);
return;
}
$connection = $this->connectionStorage->getUsingServerFd($this->serverId, $frame->fd);
$this->dispatch('message', $connection, $frame);
}
protected function onClose(int $fd): void
{
if (!$this->ready) {
return;
}
$connection = $this->connectionStorage->getUsingServerFd($this->serverId, $fd);
if ($connection === null) {
return;
}
$this->connectionStorage->remove($connection);
$this->dispatch('close', $connection);
}
public function sendMessage(string $serverId, int $fd, ?string $data = null, int $opcode = WebsocketServer::WEBSOCKET_OPCODE_TEXT): void
{
if ($serverId === $this->serverId) {
if ($this->server->exists($fd)) {
$this->server->push($fd, $data, $opcode);
}
} else {
$this->adapter->publish('client:push', [$serverId, $fd, $data], $serverId, $this->serverId);
}
}
public function pong(int $fd, ?string $data = null): void
{
$frame = new Frame();
$frame->opcode = WebsocketServer::WEBSOCKET_OPCODE_PONG;
$frame->data = $data;
$this->server->push($fd, $frame);
}
public function ping(int $fd, ?string $data = null): void
{
$frame = new Frame();
$frame->opcode = WebsocketServer::WEBSOCKET_OPCODE_PING;
$frame->data = $data;
$this->server->push($fd, $frame);
}
public function getServerId(): string
{
return $this->serverId;
}
public function getAdapter(): AdapterInterface
{
return $this->adapter;
}
public function getConnectionStorage(): ConnectionStorage
{
return $this->connectionStorage;
}
/**
* Replaced the events
*
* @param string $event
* @param callable $callback
* @return void
*/
public function on(string $event, callable $callback): void
{
$this->callbacks[$event] = $callback;
}
public function dispatch(string $event, ...$args): void
{
if (isset($this->callbacks[$event]) && is_callable($this->callbacks[$event])) {
$this->callbacks[$event]($this, ...$args);
}
}
public function setAdapter(AdapterInterface $adapter): void
{
$this->adapter = $adapter;
}
public function closeConnection(Connection $connection): void
{
$this->server->close($connection->getFd());
}
public function setGenerator(ServerIdGeneratorInterface $generator): void
{
$this->idGenerator = $generator;
}
public function getGenerator(): ?ServerIdGeneratorInterface
{
return $this->idGenerator;
}
}