Всем привет, подскажите, сильно ли такой код является говнокодом и как будет с нагрузкой, на сайте при 10 000 - 20 000 подключений.
Хотел реалpовать WampServer, но чет не получается, поэтому сделал только Ws.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class WebSocketController extends Controller implements MessageComponentInterface
{
protected $connections = [];
protected $rooms = [];
public function onOpen(ConnectionInterface $conn)
{
$this->connections[$conn->resourceId] = $conn;
}
public function onMessage(ConnectionInterface $conn, $json)
{
$data = json_decode($json);
if( isset( $data ) && isset( $data->channel_id ) && $data->channel_id )
{
$this->rooms[$data->channel_id][$conn->resourceId] = $conn->resourceId;
if( isset( $this->rooms[$data->channel_id] ) )
{
$conn->send(json_encode(['online' => count( $this->rooms[$data->channel_id] )]));
}
else
{
$conn->send(json_encode(['online' => 0]));
}
}
elseif( isset( $data ) && isset( $data->channels ) && $data->channels )
{
if( count( $data->channels ) )
{
$arrayCount = [];
foreach( $data->channels as $items )
{
if( isset( $this->rooms[$items] ) && $this->rooms[$items] )
{
$arrayCount[$items] = count($this->rooms[$items]);
}
else
{
$arrayCount[$items] = 0;
}
}
if( count( $arrayCount ) )
{
$conn->send(json_encode($arrayCount));
}
}
}
}
public function onClose(ConnectionInterface $conn)
{
foreach( $this->rooms as $key => $items )
{
foreach( $items as $id => $item )
{
if( $id == $conn->resourceId )
{
unset( $this->rooms[$key][$id] );
}
}
}
unset($this->connections[$conn->resourceId]);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
$conn->close();
}
}