Skip to content

Instantly share code, notes, and snippets.

@Zazza
Created March 19, 2021 05:05
Show Gist options
  • Save Zazza/67076d096272af8bda4e229b662fe2e9 to your computer and use it in GitHub Desktop.
Save Zazza/67076d096272af8bda4e229b662fe2e9 to your computer and use it in GitHub Desktop.
<?php
namespace Library\WebsocketWeb;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Library\WebsocketWeb\Channel\Chat;
use Library\WebsocketWeb\Channel\ChatBattle;
use Library\WebsocketWeb\Channel\Notice;
use Library\WebsocketWeb\Logger;
use React\EventLoop\LoopInterface;
use \SplObjectStorage;
class Socket implements MessageComponentInterface
{
const CHANNELS = [
Chat::KEYWORD,
Notice::KEYWORD,
ChatBattle::KEYWORD
];
/** @var array */
private $ChannelClass;
/** @var ClientSessionStorage */
private $ClientSessionStorage;
/** @var SplObjectStorage */
protected $clients;
/**
* @var LoopInterface
*/
private $loop;
/**
* Socket constructor.
* @param $loopTimer
*/
public function __construct($loopTimer)
{
$this->clients = new SplObjectStorage();
$this->ClientSessionStorage = new ClientSessionStorage();
$this->loop = $loopTimer;
foreach (self::CHANNELS as $channel) {
$className = "\\Library\\WebsocketWeb\\Channel\\" . $channel;
$this->ChannelClass[$channel] = new $className();
}
}
/**
* @param ConnectionInterface $conn
*/
public function onOpen(ConnectionInterface $conn)
{
/** @var Channel\Channel $Channel */
foreach ($this->ChannelClass as $Channel) {
$conn = $Channel->onOpen($conn);
}
$this->clients->attach($conn);
}
/**
* @param ConnectionInterface $from
* @param string $msg
* @return bool False if error
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$msg = json_decode($msg);
list($controller, $action) = explode('/', $msg->method);
/** @var ClientSession $ClientSession */
$ClientSession = $this->ClientSessionStorage->getClientSession($msg->authKey);
if (!$ClientSession) {
$ClientSession = new ClientSession($msg->authKey);
$from->authKey = $msg->authKey;
foreach (self::CHANNELS as $channel) {
$className = "\\Library\\WebsocketWeb\\Controller\\" . $channel . "Controller";
$ClientSession->setController($channel, new $className($ClientSession, $this->loop, $channel));
}
$this->ClientSessionStorage->attach($msg->authKey, $ClientSession);
}
$ClientSession->setChannel($controller);
$ClientSession->setClient($from);
if (array_key_exists($controller, $this->ChannelClass)) {
$this->ChannelClass[$controller]->onMessage(
$this->ClientSessionStorage,
$ClientSession,
$action,
$msg,
$controller
);
}
}
/**
* @param string $channel
* @param string $val
*/
public function onResponse($channel, $val)
{
if (array_key_exists($channel, $this->ChannelClass)) {
$this->ChannelClass[$channel]->onResponse($this->ClientSessionStorage, $val, $channel);
}
}
/**
* @param ConnectionInterface $conn
*/
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
/** @var Channel\Channel $Channel */
foreach ($this->ChannelClass as $Channel) {
$Channel->onClose($this->ClientSessionStorage, $conn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment