Implemented notify

This commit is contained in:
2024-03-01 15:20:54 +01:00
parent f849ac481a
commit 6f49b69a66
4 changed files with 60 additions and 4 deletions

View File

@ -109,6 +109,16 @@ class CommandBus implements CommandBusInterface
*/
public function notify(string $event, array $data): void
{
$this->emit(self::EVENT_NOTIFY, [ $event, $data ]);
$json = (new Message(Message::MSGTYPE_NOTIFY, [
'event' => $event,
'data' => $data
]))->toJson();
foreach ($this->connections as $client) {
$client->write($json."\n");
}
}
}

View File

@ -84,7 +84,7 @@ class CommandBusClient implements CommandBusInterface
private function onServerMessage(Message $message): void
{
// fprintf(STDERR, "onServerMessage: %s\n", $message->toJson());
//fprintf(STDERR, "onServerMessage: %s\n", $message->toJson());
switch ($message->getType()) {
case Message::MSGTYPE_EXECUTE: // server call to execute command
// TODO implement me
@ -97,8 +97,10 @@ class CommandBusClient implements CommandBusInterface
unset($this->pending[$uuid]);
}
break;
case Message::MSGTYPE_REGISTRY: // command registry actions
// TODO implement me
case Message::MSGTYPE_NOTIFY: // notify
$event = $message->getData()['event']??null;
$data = (array)($message->getData()['data']??[]);
$this->emit(self::EVENT_NOTIFY, [ $event, $data ]);
break;
default:
$this->connection->end('{"msg":"error","data":{"error":"Unexpected message type"}}');