added responses to users in ws as json

This commit is contained in:
GitProtogen
2026-03-07 20:17:40 +01:00
parent 09523a5509
commit ced7937583
+6 -27
View File
@@ -20,7 +20,6 @@ use \ComCen\Security\TokenHandler;
class WebSocketServer implements MessageComponentInterface class WebSocketServer implements MessageComponentInterface
{ {
private $connectionsData = []; private $connectionsData = [];
private $chatGroups = [];
private function isSameConnection(ConnectionInterface $connection1, ConnectionInterface $connection2): bool private function isSameConnection(ConnectionInterface $connection1, ConnectionInterface $connection2): bool
{ {
@@ -62,27 +61,11 @@ class WebSocketServer implements MessageComponentInterface
echo "New connection: {$conn->resourceId}\n"; echo "New connection: {$conn->resourceId}\n";
} }
public function createGroup(string $owner, string $name, string ...$whitelist): bool
{
foreach ($whitelist as $user) {
if (strlen($user) > 32) {
return false;
}
}
$chatGroups[] = [
"owner" => $owner,
"name" => $name,
"whitelist" => $whitelist
];
return true;
}
public function onMessage(ConnectionInterface $from, $msg): void public function onMessage(ConnectionInterface $from, $msg): void
{ {
$decodedMsg = json_decode($msg, true); $decodedMsg = json_decode($msg, true);
if (!$decodedMsg) { if (!$decodedMsg) {
$from->send("not or empty json"); $from->send("{\"error\"}: \"not or empty json\"");
return; return;
} }
$index = $this->getConnectionIndex($from); $index = $this->getConnectionIndex($from);
@@ -92,14 +75,10 @@ class WebSocketServer implements MessageComponentInterface
$msgContent = $decodedMsg["msg"] ?? null; $msgContent = $decodedMsg["msg"] ?? null;
if ($msgContent) { if ($msgContent) {
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent); $this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
$from->send("{\"success\"}: \"message send\"");
return; return;
} }
$from->send("{\"error\"}: \"no message\"");
$groupCreationRequest = $decodedMsg["createGroupWithName"] ?? null;
if ($groupCreationRequest) {
}
return; return;
} }
@@ -108,13 +87,13 @@ class WebSocketServer implements MessageComponentInterface
$tokenUser = TokenHandler::getTokenOwnership($token); $tokenUser = TokenHandler::getTokenOwnership($token);
if ($tokenUser) { if ($tokenUser) {
$this->connectionsData[$index]["username"] = $tokenUser; $this->connectionsData[$index]["username"] = $tokenUser;
$from->send("authenticated"); $from->send("{\"success\"}: \"authenticated\"");
return; return;
} }
$from->send("invalid token"); $from->send("{\"error\"}: \"invalid token\"");
return; return;
} }
$from->send("not authenticated"); $from->send("{\"error\"}: \"you are not authenticated\"");
} }
public function onClose(ConnectionInterface $conn): void public function onClose(ConnectionInterface $conn): void