added responses to users in ws as json
This commit is contained in:
+6
-27
@@ -20,7 +20,6 @@ use \ComCen\Security\TokenHandler;
|
||||
class WebSocketServer implements MessageComponentInterface
|
||||
{
|
||||
private $connectionsData = [];
|
||||
private $chatGroups = [];
|
||||
|
||||
private function isSameConnection(ConnectionInterface $connection1, ConnectionInterface $connection2): bool
|
||||
{
|
||||
@@ -62,27 +61,11 @@ class WebSocketServer implements MessageComponentInterface
|
||||
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
|
||||
{
|
||||
$decodedMsg = json_decode($msg, true);
|
||||
if (!$decodedMsg) {
|
||||
$from->send("not or empty json");
|
||||
$from->send("{\"error\"}: \"not or empty json\"");
|
||||
return;
|
||||
}
|
||||
$index = $this->getConnectionIndex($from);
|
||||
@@ -92,14 +75,10 @@ class WebSocketServer implements MessageComponentInterface
|
||||
$msgContent = $decodedMsg["msg"] ?? null;
|
||||
if ($msgContent) {
|
||||
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
|
||||
$from->send("{\"success\"}: \"message send\"");
|
||||
return;
|
||||
}
|
||||
|
||||
$groupCreationRequest = $decodedMsg["createGroupWithName"] ?? null;
|
||||
if ($groupCreationRequest) {
|
||||
|
||||
}
|
||||
|
||||
$from->send("{\"error\"}: \"no message\"");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -108,13 +87,13 @@ class WebSocketServer implements MessageComponentInterface
|
||||
$tokenUser = TokenHandler::getTokenOwnership($token);
|
||||
if ($tokenUser) {
|
||||
$this->connectionsData[$index]["username"] = $tokenUser;
|
||||
$from->send("authenticated");
|
||||
$from->send("{\"success\"}: \"authenticated\"");
|
||||
return;
|
||||
}
|
||||
$from->send("invalid token");
|
||||
$from->send("{\"error\"}: \"invalid token\"");
|
||||
return;
|
||||
}
|
||||
$from->send("not authenticated");
|
||||
$from->send("{\"error\"}: \"you are not authenticated\"");
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $conn): void
|
||||
|
||||
Reference in New Issue
Block a user