Skip to content

Commit

Permalink
Merge pull request #3 from utopia-php/fix-swoole-send-nonblocking
Browse files Browse the repository at this point in the history
fix(swoole): send messages non-blocking
  • Loading branch information
eldadfux authored Jul 11, 2021
2 parents fdb82f0 + 9b74161 commit 808317e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
</ignoreFiles>
</projectFiles>
<stubs>
<file name="vendor/swoole/ide-helper/output/swoole/aliases.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/functions.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/constants.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/namespace/Server.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/namespace/Process.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/namespace/Http/Server.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/namespace/Http/Request.php"/>
<file name="vendor/swoole/ide-helper/output/swoole/namespace/WebSocket/Frame.php"/>
Expand Down
27 changes: 16 additions & 11 deletions src/WebSocket/Adapter/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(string $host = '0.0.0.0', int $port = 80)
parent::__construct($host, $port);

$this->server = new Server($this->host, $this->port);

// Set maximum connections to Swoole's limit of 1 Million
$this->config['max_connection'] = 1_000_000;
}

public function start(): void
Expand All @@ -42,16 +45,18 @@ public function shutdown(): void
public function send(array $connections, string $message): void
{
foreach ($connections as $connection) {
if ($this->server->exist($connection) && $this->server->isEstablished($connection)) {
$this->server->push(
$connection,
$message,
SWOOLE_WEBSOCKET_OPCODE_TEXT,
SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS
);
} else {
$this->server->close($connection);
}
go(function () use ($connection, $message) {
if ($this->server->exist($connection) && $this->server->isEstablished($connection)) {
$this->server->push(
$connection,
$message,
SWOOLE_WEBSOCKET_OPCODE_TEXT,
SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS
);
} else {
$this->server->close($connection);
}
});
}
}

Expand All @@ -65,7 +70,7 @@ public function onStart(callable $callback): self
$this->server->on('start', function () use ($callback) {
call_user_func($callback);

Process::signal(2, function () {
Process::signal('2', function () {
$this->shutdown();
});
});
Expand Down

0 comments on commit 808317e

Please sign in to comment.