refactored some code, started creating groups

This commit is contained in:
GitProtogen
2026-03-07 11:32:17 +01:00
parent 51a94fea19
commit 09523a5509
4 changed files with 67 additions and 18 deletions
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="database.sqlite" uuid="b6993544-8eca-46b7-9a38-b649d2563add">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/storage/database.sqlite</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
+34 -7
View File
@@ -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,22 +89,33 @@ class WebSocketServer implements MessageComponentInterface
if ($index === null) return;
if ($this->connectionsData[$index]["username"]) {
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $decodedMsg["message"]);
} else {
$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");
} else {
return;
}
$from->send("invalid token");
return;
}
} else {
$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();
+14 -4
View File
@@ -21,16 +21,26 @@ class Handler
{
$this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function init(): void
{
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS users (
username VARCHAR(32) NOT NULL UNIQUE,
password CHAR(255) NOT NULL
)
");
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS chat_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL UNIQUE
)
");
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS group_members (
group_id INTEGER NOT NULL,
username VARCHAR(32) NOT NULL,
PRIMARY KEY (group_id, username),
FOREIGN KEY (username) REFERENCES users(username)
)
");
}
public function addUser(string $username, string $password): void
View File