Skip to content

Instantly share code, notes, and snippets.

@Zazza
Created March 19, 2021 05:05
Show Gist options
  • Save Zazza/b8a19127d7fdaeda8ea36df0c6d267d5 to your computer and use it in GitHub Desktop.
Save Zazza/b8a19127d7fdaeda8ea36df0c6d267d5 to your computer and use it in GitHub Desktop.
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Library\WebsocketWeb\Socket;
use Phalcon\CLI\Task;
use React\EventLoop\StreamSelectLoop;
use Predis\Async\Client as PredisClient;
use React\Socket\Server as ReactSocket;
class RatchetTask extends Task
{
public function mainAction()
{
$Loop = new StreamSelectLoop();
$Socket = new Socket($Loop);
$client = new PredisClient(
$this->config->redis->protocol . '://' . $this->config->redis->host . ':' . $this->config->redis->port,
$Loop
);
$client->connect(function ($client) use ($Socket) {
$channels = array_map(function($channel){
return $this->config->site->HOST . $channel;
}, Socket::CHANNELS);
$client->pubSubLoop(['psubscribe' => $channels], function ($event) use ($Socket) {
foreach (Socket::CHANNELS as $channel) {
if ($event->channel === $this->config->site->HOST . $channel) {
$Socket->onResponse($channel, $event->payload);
}
}
});
});
$socket = new ReactSocket($Loop);
$socket->listen($this->config->ws->port, $this->config->ws->ip);
new IoServer(
new HttpServer(
new WsServer(
$Socket
)
),
$socket,
$Loop
);
$Loop->run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment