PABX da Simples IP
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.
 
 
 
 
 
 

89 lines
2.7 KiB

<?php
namespace websocket;
use app\Middleware\ApiAgente;
use app\Models\Atendimento;
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;
/** @var Atendimento $atendimento api das função do agente */
protected $atendimento;
public $connection = [];
public $shell = [];
public $conectado = [];
public $idConexion = [];
public $clientes = [];
public function __construct()
{
$this->clients = new SplObjectStorage();
$this->api = new ApiAgente();
$this->atendimento = new Atendimento();
}
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 (\Exception $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 (\Exception $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 (\Exception $th) {
logger('ws:onClose')->debug($th->getMessage());
}
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
$conn->close();
}
}