Reload WebSocket on control source changes

This commit is contained in:
seo
2026-06-07 18:10:50 +09:00
parent 5d366f75a5
commit 525f299aba
2 changed files with 27 additions and 1 deletions
+25
View File
@@ -260,9 +260,34 @@ final class ControlWebSocket implements MessageComponentInterface
$address = getenv('CONTROL_WS_ADDRESS') ?: '127.0.0.1:8088';
$loop = Loop::get();
$app = new ControlWebSocket();
$watchFiles = [
__FILE__,
__DIR__ . '/../public/api.php',
__DIR__ . '/../config/config.php',
];
$watchSignature = static function (array $files): string {
$parts = [];
foreach ($files as $file) {
$parts[] = is_file($file)
? $file . ':' . filemtime($file) . ':' . filesize($file)
: $file . ':missing';
}
return implode('|', $parts);
};
$startedSignature = $watchSignature($watchFiles);
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastStatus());
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastDmesg());
$loop->addPeriodicTimer(15.0, static function () use ($watchFiles, $watchSignature, $startedSignature): void {
if ($watchSignature($watchFiles) === $startedSignature) {
return;
}
fwrite(STDOUT, 'Control WebSocket source changed; restarting' . PHP_EOL);
exit(0);
});
$socket = new SocketServer($address, [], $loop);
$server = new IoServer(