Skip to content

Instantly share code, notes, and snippets.

@DaveRandom
Forked from J7mbo/gist:6823472
Last active December 24, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DaveRandom/6823861 to your computer and use it in GitHub Desktop.
Save DaveRandom/6823861 to your computer and use it in GitHub Desktop.
<?php
$loop = React\EventLoop\Factory::create();
$pusher = new Seedstream\Pusher($loop);
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
),
$webSock
);
$loop->run();
<?php
namespace Seedstream;
use React\EventLoop\LoopInterface,
React\Socket\ConnectionInterface;
class Pusher
{
private $loop;
public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
}
public function onSubscribe(ConnectionInterface $conn, $subscription)
{
list($subject, $userId, $token) = explode(':', $subscription->getId());
$this->loop->addPeriodicTimer(4, function() use ($conn, $subscription, $subject, $userId) {
$subscription->broadcast(['userId' => $userId, 'subject' => $subject, 'resourceId' => $conn->resourceId]);
});
echo sprintf("Client Connected: %s, userId: %d, subject: %s" . PHP_EOL, $conn->remoteAddress, $userId, $subject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment