Created
December 3, 2012 23:33
-
-
Save romainneutron/4199061 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 | |
require 'vendor/autoload.php'; | |
use Ratchet\MessageComponentInterface; | |
use Ratchet\ConnectionInterface; | |
use Ratchet\Server\IoServer; | |
use Ratchet\WebSocket\WsServer; | |
use Ratchet\Wamp\WampServer; | |
use Ratchet\ConnectionInterface as Conn; | |
use Ratchet\Session\SessionProvider; | |
use Ratchet\Wamp\WampServerInterface; | |
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler; | |
class PubSub implements WampServerInterface | |
{ | |
protected $subscribedTopics = array(); | |
public function __construct() | |
{ | |
$this->subscribedTopics['monitor'] = array(); | |
} | |
public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible) | |
{ | |
echo "onPublish\n"; | |
$topic->broadcast($event); | |
} | |
public function onCall(Conn $conn, $id, $topic, array $params) | |
{ | |
echo "onCall\n"; | |
$conn->callError($id, $topic, 'RPC not supported on this demo'); | |
} | |
// No need to anything, since WampServer adds and removes subscribers to Topics automatically | |
public function onSubscribe(Conn $conn, $topic) | |
{ | |
echo "onSubscribe\n"; | |
// When a visitor subscribes to a topic link the Topic object in a lookup array | |
if (! array_key_exists($topic->getId(), $this->subscribedTopics)) { | |
$this->subscribedTopics[$topic->getId()] = $topic; | |
} | |
} | |
public function onUnSubscribe(Conn $conn, $topic) | |
{ | |
echo "onUnSubscribe\n"; | |
} | |
public function onOpen(Conn $conn) | |
{ | |
echo($conn->Session->get('plop')) . "\n"; | |
echo "onOpen\n"; | |
} | |
public function onClose(Conn $conn) | |
{ | |
echo "onClose\n"; | |
} | |
public function onError(Conn $conn, \Exception $e) | |
{ | |
echo "onError\n"; | |
} | |
} | |
$memcache = new Memcache; | |
$memcache->connect('localhost', 11211); | |
$server = IoServer::factory( | |
new WsServer( | |
new SessionProvider( | |
new WampServer( | |
new PubSub | |
) | |
, new MemcacheSessionHandler($memcache) | |
) | |
), 9990 | |
); | |
$server->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment