auf php websocket

This commit is contained in:
2025-02-28 07:31:00 +01:00
parent ed4e6dba81
commit afa24aea7f
2 changed files with 21 additions and 37 deletions

View File

@@ -1,7 +1,5 @@
<?php
require __DIR__ . '/vendor/autoload.php'; // Autoload-Datei einbinden
class HauptManager extends IPSModule
{
public function Create()
@@ -53,54 +51,35 @@ class HauptManager extends IPSModule
private function StartWebSocketServer()
{
$server = new \Ratchet\App('localhost', 8080);
$server->route('/ws', new class($this) implements \Ratchet\MessageComponentInterface {
private $module;
public function __construct($module)
{
$this->module = $module;
}
public function onOpen(\Ratchet\ConnectionInterface $conn)
{
$this->module->AddClient($conn);
}
public function onClose(\Ratchet\ConnectionInterface $conn)
{
$this->module->RemoveClient($conn);
}
public function onError(\Ratchet\ConnectionInterface $conn, \Exception $e)
{
$conn->close();
}
public function onMessage(\Ratchet\ConnectionInterface $from, $msg)
{
// Nachrichtenverarbeitung implementieren
}
}, ['*']);
$server->run();
$server = new PHPWebSocket();
$server->bind('message', function($clientID, $message, $messageLength, $binary) {
$this->onMessage($clientID, $message);
});
$server->bind('open', function($clientID) {
$this->AddClient($clientID);
});
$server->bind('close', function($clientID) {
$this->RemoveClient($clientID);
});
$server->startServer('localhost', 8080);
}
public function AddClient($conn)
public function AddClient($clientID)
{
$clients = json_decode($this->GetValue("Clients"), true);
if ($clients === null) {
$clients = [];
}
$clients[] = (string)$conn->resourceId;
$clients[] = (string)$clientID;
$this->SetValue("Clients", json_encode($clients));
$this->UpdateClientList();
}
public function RemoveClient($conn)
public function RemoveClient($clientID)
{
$clients = json_decode($this->GetValue("Clients"), true);
if ($clients !== null) {
$index = array_search((string)$conn->resourceId, $clients);
$index = array_search((string)$clientID, $clients);
if ($index !== false) {
unset($clients[$index]);
$clients = array_values($clients); // Reindizieren des Arrays
@@ -122,5 +101,10 @@ class HauptManager extends IPSModule
}
$this->UpdateFormField('ClientList', 'values', json_encode($clientList));
}
public function onMessage($clientID, $message)
{
// Nachrichtenverarbeitung implementieren
}
}
?>

View File

@@ -6,7 +6,7 @@
"compatibility": {
"version": "7.1"
},
"version": "1.103",
"version": "1.104",
"build": 0,
"date": 0
}