You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.4 KiB

<?php
namespace websocket;
use app\Middleware\ApiAgente;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use SplObjectStorage;
class Servidorsocket implements MessageComponentInterface
{
/** @var ApiAgente $api api das função do agente */
protected $api;
/** @var SplObjectStorage $clients api das função do agente */
protected $clients;
public $connection = [];
public $shell = [];
public $conectado = [];
public $idConexion = [];
public $clientes = [];
public function __construct()
{
$this->clients = new SplObjectStorage();
}
public function onOpen(ConnectionInterface $conn)
{
try {
$headers = $conn->httpRequest->getHeaders();
$conn->send('att');
$this->clients->attach($conn);
$this->connection[$conn->resourceId] = null;
$this->shell[$conn->resourceId] = null;
$this->conectado[$conn->resourceId] = null;
$this->idConexion[$conn->resourceId] = null;
$this->clientes[$conn->resourceId] = [
"matricula" => 'testwe',
"conection" => $conn
];
} catch (\Throwable $th) {
logger('ws:onOpen')->debug(var_export($headers, true));
}
}
public function onMessage(ConnectionInterface $from, $msg)
{
try {
$mensagem = json_decode($msg, true);
// if ($mensagem['matricula']) {
// $this->clientes[$from->resourceId]['matricula'] = $mensagem['matricula'];
// } else {
foreach ($this->clientes as $key => $value) {
//if ($mensagem['event']['mensagem']['dst'] == $value['matricula']) {
$value['conection']->send($msg);
//}
}
// }
} catch (\Throwable $th) {
logger('ws:onMessage')->debug($th->getMessage());
}
}
public function onClose(ConnectionInterface $conn)
{
try {
$this->conectado[$conn->resourceId] = false;
$this->clients->detach($conn);
$this->api->logoff($this->clientes[$conn->resourceId], false);
} catch (\Throwable $th) {
logger('ws:onClose')->debug($th->getMessage());
}
}
public function onError(ConnectionInterface $conn, \Throwable $e)
{
$conn->close();
}
}