Skip to content

Instantly share code, notes, and snippets.

@bazo
Created June 16, 2013 14:41
Show Gist options
  • Save bazo/5792263 to your computer and use it in GitHub Desktop.
Save bazo/5792263 to your computer and use it in GitHub Desktop.
ab.connect("ws://localhost:8080",
// WAMP session was established
function(connection) {
// things to do once the session has been established
window.connection = connection;
connection.subscribe('deploy', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
console.log(data); //this prints out broadcasted data
});
connection.call("deploy", {'application': 'app'}).then(function(result){
console.log(result); //doesnt print anything, it's not even executed
ab.log;
});
},
// WAMP session is gone
function(code, reason) {
connection = null;
console.log(reason);
}
);
class Server implements MessageComponentInterface, WampServerInterface
{
/** @var ConsoleOutput */
private $output;
/** @var \SplObjectStorage */
private $clients;
/** @var \SplObjectStorage */
private $topics;
public function __construct(ConsoleOutput $output)
{
$this->output = $output;
$this->clients = new \SplObjectStorage;
$this->topics = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $connection)
{
$this->clients->attach($connection);
$this->output->writeln('client connected');
}
public function onMessage(ConnectionInterface $from, $message){}
public function onClose(ConnectionInterface $connection)
{
$this->clients->detach($connection);
}
public function onError(ConnectionInterface $connection, \Exception $e){}
public function onCall(ConnectionInterface $connection, $id, $topic, array $params)
{
if($topic->getId() === 'deploy') {
$this->output->writeln('request deploy');
$topic->broadcast('test'); //only this line sends data
$connection->callResult(json_encode(['test']));
$connection->send(json_encode(['test']));
}
$connection->event('deploy', json_encode(['test']));
return "test";
}
public function onPublish(ConnectionInterface $connection, $topic, $event, array $exclude, array $eligible){}
public function onSubscribe(ConnectionInterface $connection, $topic)
{
$this->output->write((string)$topic);
if(!$this->topics->contains($topic)) {
$this->output->writeln('client attached to topic:'.$topic);
$this->topics->attach($topic);
}
if(!$topic->has($connection)) {
$topic->add($connection);
}
}
public function onUnSubscribe(ConnectionInterface $connection, $topic)
{
if($topic->has($connection)) {
$topic->remove($connection);
}
if($topic->count() === 0) {
$this->topics->detach($topic);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment