2020-10-16 20:31:09 +13:00
|
|
|
<?php
|
|
|
|
|
2020-10-21 23:50:11 +13:00
|
|
|
require_once __DIR__.'/init.php';
|
2020-10-20 04:09:53 +13:00
|
|
|
require_once __DIR__.'/../vendor/autoload.php';
|
|
|
|
|
2020-10-21 23:50:11 +13:00
|
|
|
use Appwrite\Swoole\Request as SwooleRequest;
|
2020-10-16 20:31:09 +13:00
|
|
|
use Swoole\WebSocket\Server;
|
2020-10-20 04:09:53 +13:00
|
|
|
use Swoole\Http\Request;
|
2020-10-21 02:22:46 +13:00
|
|
|
use Swoole\Process;
|
2020-10-16 20:31:09 +13:00
|
|
|
use Swoole\WebSocket\Frame;
|
2020-10-21 02:22:46 +13:00
|
|
|
use Utopia\App;
|
2020-10-19 00:51:16 +13:00
|
|
|
use Utopia\CLI\Console;
|
2020-10-21 02:22:46 +13:00
|
|
|
use Utopia\Route;
|
2020-10-16 20:31:09 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO List
|
|
|
|
*
|
|
|
|
* - Abuse Control / x mesages per connection
|
|
|
|
* - CORS Validation
|
|
|
|
* - Limit payload size
|
|
|
|
* - Message structure: { status: "ok"|"error", event: EVENT_NAME, data: <any arbitrary data> }
|
|
|
|
* - JWT Authentication (in path / or in message)
|
2020-10-17 18:48:03 +13:00
|
|
|
*
|
2020-10-21 02:22:46 +13:00
|
|
|
* Protocols Support:
|
|
|
|
* - Websocket support: https://www.swoole.co.uk/docs/modules/swoole-websocket-server
|
|
|
|
* - MQTT support: https://www.swoole.co.uk/docs/modules/swoole-mqtt-server
|
|
|
|
* - SSE support: https://github.com/hhxsv5/php-sse
|
|
|
|
* - Socket.io support: https://github.com/shuixn/socket.io-swoole-server
|
2020-10-16 20:31:09 +13:00
|
|
|
*/
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
ini_set('default_socket_timeout', -1);
|
|
|
|
Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
|
2020-10-19 00:51:16 +13:00
|
|
|
|
2020-10-16 20:31:09 +13:00
|
|
|
$server = new Server("0.0.0.0", 80);
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$connections = [];
|
2020-10-21 23:50:11 +13:00
|
|
|
$subscriptions = [];
|
2020-10-19 00:51:16 +13:00
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$server->on("workerStart", function ($server, $workerId) use (&$connections) {
|
2020-10-19 00:51:16 +13:00
|
|
|
Console::success('Worker '.++$workerId.' started succefully');
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$attempts = 0;
|
|
|
|
$start = time();
|
|
|
|
|
2020-10-20 09:38:49 +13:00
|
|
|
while ($attempts < 300) {
|
2020-10-20 04:09:53 +13:00
|
|
|
try {
|
2020-10-20 09:38:49 +13:00
|
|
|
if($attempts > 0) {
|
|
|
|
Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart in 5 seconds (attempt #'.$attempts.')');
|
|
|
|
sleep(5); // 1 sec delay between connection attempts
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$redis = new Redis();
|
|
|
|
$redis->connect('redis', 6379);
|
|
|
|
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
|
|
|
|
|
2020-10-20 09:38:49 +13:00
|
|
|
if($redis->ping(true)) {
|
2020-10-20 04:09:53 +13:00
|
|
|
$attempts = 0;
|
2020-10-20 07:56:02 +13:00
|
|
|
Console::success('Connection established');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Console::error('Connection failed');
|
2020-10-19 00:51:16 +13:00
|
|
|
}
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$redis->subscribe(['realtime'], function($redis, $channel, $message) use ($server, $workerId, &$connections) {
|
|
|
|
$message = 'Message from worker #'.$workerId.'; '.$message;
|
2020-10-21 02:22:46 +13:00
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
foreach($connections as $fd) {
|
|
|
|
if ($server->exist($fd)
|
|
|
|
&& $server->isEstablished($fd)
|
|
|
|
) {
|
|
|
|
Console::info('Sending message: '.$message.' (user: '.$fd.', worker: '.$workerId.')');
|
|
|
|
|
|
|
|
$server->push($fd, $message, SWOOLE_WEBSOCKET_OPCODE_TEXT,
|
|
|
|
SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS);
|
|
|
|
}
|
|
|
|
else {
|
2020-10-21 23:50:11 +13:00
|
|
|
$server->close($fd);
|
2020-10-20 04:09:53 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (\Throwable $th) {
|
2020-10-20 09:38:49 +13:00
|
|
|
Console::error('Connection error: '.$th->getMessage());
|
2020-10-20 04:09:53 +13:00
|
|
|
$attempts++;
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-20 09:38:49 +13:00
|
|
|
|
|
|
|
$attempts++;
|
2020-10-20 04:09:53 +13:00
|
|
|
}
|
2020-10-19 00:51:16 +13:00
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
Console::error('Failed to restart connection...');
|
2020-10-19 00:51:16 +13:00
|
|
|
});
|
|
|
|
|
2020-10-16 20:31:09 +13:00
|
|
|
$server->on("start", function (Server $server) {
|
2020-10-19 00:51:16 +13:00
|
|
|
Console::success('Server started succefully');
|
2020-10-21 02:22:46 +13:00
|
|
|
|
|
|
|
Console::info("Master pid {$server->master_pid}, manager pid {$server->manager_pid}");
|
|
|
|
|
|
|
|
// listen ctrl + c
|
|
|
|
Process::signal(2, function () use ($server) {
|
|
|
|
Console::log('Stop by Ctrl+C');
|
|
|
|
$server->shutdown();
|
|
|
|
});
|
2020-10-16 20:31:09 +13:00
|
|
|
});
|
|
|
|
|
2020-10-20 04:09:53 +13:00
|
|
|
$server->on('open', function(Server $server, Request $request) use (&$connections) {
|
|
|
|
$connections[] = $request->fd;
|
|
|
|
|
|
|
|
Console::info("Connection open (user: {$request->fd}, worker: {$server->getWorkerId()})");
|
|
|
|
Console::info('Total connections: '.count($connections));
|
2020-10-19 00:51:16 +13:00
|
|
|
|
2020-10-21 23:50:11 +13:00
|
|
|
$connection = $request->fd;
|
2020-10-21 02:22:46 +13:00
|
|
|
$app = new App('Asia/Tel_Aviv');
|
2020-10-21 23:50:11 +13:00
|
|
|
$request = new SwooleRequest($request);
|
2020-10-21 02:22:46 +13:00
|
|
|
|
2020-10-21 23:50:11 +13:00
|
|
|
App::setResource('request', function () use ($request) {
|
|
|
|
return $request;
|
|
|
|
});
|
|
|
|
|
|
|
|
App::setResource('response', function () {
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$user = App::getResource('user');
|
|
|
|
$project = App::getResource('project');
|
|
|
|
|
|
|
|
/** @var Appwrite\Database\Document $user */
|
|
|
|
/** @var Appwrite\Database\Document $project */
|
|
|
|
|
|
|
|
var_dump($project->getId());
|
|
|
|
var_dump($project->getAttribute('name'));
|
|
|
|
var_dump($user->getId());
|
|
|
|
var_dump($user->getAttribute('name'));
|
|
|
|
|
|
|
|
if(!isset($subscriptions[$project->getId()])) { // Init Project
|
|
|
|
$subscriptions[$project->getId()] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($subscriptions[$project->getId()][$user->getId()])) { // Close previous connection
|
|
|
|
$server->close($subscriptions[$project->getId()][$user->getId()]['connection']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscriptions[$project->getId()][$user->getId()] = [
|
|
|
|
'channels' => [],
|
|
|
|
'connection' => $connection,
|
|
|
|
];
|
2020-10-21 02:22:46 +13:00
|
|
|
|
2020-10-21 23:50:11 +13:00
|
|
|
$server->push($connection, json_encode(["hello", count($connections)]));
|
2020-10-16 20:31:09 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
$server->on('message', function(Server $server, Frame $frame) {
|
2020-10-20 04:09:53 +13:00
|
|
|
if($frame->data === 'reload') {
|
|
|
|
$server->reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
Console::info('Recieved message: '.$frame->data.' (user: '.$frame->fd.', worker: '.$server->getWorkerId().')');
|
|
|
|
|
2020-10-19 00:51:16 +13:00
|
|
|
$server->push($frame->fd, json_encode(["hello, worker_id:".$server->getWorkerId(), time()]));
|
2020-10-16 20:31:09 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
$server->on('close', function(Server $server, int $fd) {
|
2020-10-20 04:09:53 +13:00
|
|
|
Console::error('Connection close: '.$fd);
|
2020-10-16 20:31:09 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
$server->start();
|