refactored some code, started creating groups
This commit is contained in:
+41
-14
@@ -20,6 +20,7 @@ use \ComCen\Security\TokenHandler;
|
||||
class WebSocketServer implements MessageComponentInterface
|
||||
{
|
||||
private $connectionsData = [];
|
||||
private $chatGroups = [];
|
||||
|
||||
private function isSameConnection(ConnectionInterface $connection1, ConnectionInterface $connection2): bool
|
||||
{
|
||||
@@ -61,6 +62,22 @@ 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);
|
||||
@@ -72,21 +89,32 @@ class WebSocketServer implements MessageComponentInterface
|
||||
if ($index === null) return;
|
||||
|
||||
if ($this->connectionsData[$index]["username"]) {
|
||||
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $decodedMsg["message"]);
|
||||
} else {
|
||||
$token = $decodedMsg["token"] ?? null;
|
||||
if ($token) {
|
||||
$tokenUser = TokenHandler::getTokenOwnership($token);
|
||||
if ($tokenUser) {
|
||||
$this->connectionsData[$index]["username"] = $tokenUser;
|
||||
$from->send("authenticated");
|
||||
} else {
|
||||
$from->send("invalid token");
|
||||
}
|
||||
} else {
|
||||
$from->send("not authenticated");
|
||||
$msgContent = $decodedMsg["msg"] ?? null;
|
||||
if ($msgContent) {
|
||||
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
|
||||
return;
|
||||
}
|
||||
|
||||
$groupCreationRequest = $decodedMsg["createGroupWithName"] ?? null;
|
||||
if ($groupCreationRequest) {
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$token = $decodedMsg["token"] ?? null;
|
||||
if ($token) {
|
||||
$tokenUser = TokenHandler::getTokenOwnership($token);
|
||||
if ($tokenUser) {
|
||||
$this->connectionsData[$index]["username"] = $tokenUser;
|
||||
$from->send("authenticated");
|
||||
return;
|
||||
}
|
||||
$from->send("invalid token");
|
||||
return;
|
||||
}
|
||||
$from->send("not authenticated");
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $conn): void
|
||||
@@ -113,7 +141,6 @@ $server = IoServer::factory(
|
||||
8080
|
||||
);
|
||||
|
||||
Handler::getInstance()->init();
|
||||
echo "Server running on http://localhost:8080\n";
|
||||
|
||||
$server->run();
|
||||
Reference in New Issue
Block a user