Created
March 19, 2021 05:05
-
-
Save Zazza/b8a19127d7fdaeda8ea36df0c6d267d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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