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>
+41 -14
View File
@@ -20,6 +20,7 @@ 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
{ {
@@ -61,6 +62,22 @@ 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);
@@ -72,21 +89,32 @@ class WebSocketServer implements MessageComponentInterface
if ($index === null) return; if ($index === null) return;
if ($this->connectionsData[$index]["username"]) { if ($this->connectionsData[$index]["username"]) {
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $decodedMsg["message"]); $msgContent = $decodedMsg["msg"] ?? null;
} else { if ($msgContent) {
$token = $decodedMsg["token"] ?? null; $this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
if ($token) { return;
$tokenUser = TokenHandler::getTokenOwnership($token);
if ($tokenUser) {
$this->connectionsData[$index]["username"] = $tokenUser;
$from->send("authenticated");
} else {
$from->send("invalid token");
}
} else {
$from->send("not authenticated");
} }
$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 public function onClose(ConnectionInterface $conn): void
@@ -113,7 +141,6 @@ $server = IoServer::factory(
8080 8080
); );
Handler::getInstance()->init();
echo "Server running on http://localhost:8080\n"; echo "Server running on http://localhost:8080\n";
$server->run(); $server->run();
+14 -4
View File
@@ -21,16 +21,26 @@ class Handler
{ {
$this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite'); $this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function init(): void
{
$this->pdo->exec(" $this->pdo->exec("
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
username VARCHAR(32) NOT NULL UNIQUE, username VARCHAR(32) NOT NULL UNIQUE,
password CHAR(255) NOT NULL 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 public function addUser(string $username, string $password): void
View File