refactored some code, started creating groups
This commit is contained in:
Generated
+12
@@ -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
@@ -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,22 +89,33 @@ 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) {
|
||||||
|
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$groupCreationRequest = $decodedMsg["createGroupWithName"] ?? null;
|
||||||
|
if ($groupCreationRequest) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$token = $decodedMsg["token"] ?? null;
|
$token = $decodedMsg["token"] ?? null;
|
||||||
if ($token) {
|
if ($token) {
|
||||||
$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("authenticated");
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
$from->send("invalid token");
|
$from->send("invalid token");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$from->send("not authenticated");
|
$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();
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user