I am developing a web application where I need real-time behavior for the following scenario,
The application will have two types of users: Player and Spectator . Players can join the game, while spectators can simply see.
The game will be initialized by the administrator user.
The viewer is basically someone who can see a list of people who have joined the game. Of course, this should be in real time when a player disconnects or a new player joins a game viewer who sees a real-time list.
To summarize, consider the example below
Spectator_1 joins Clan_101 Spectator_2 joins Clan_201 Player_1 joins Clan_101 // Need to broadcast this event to Spectator_1 Player_2 joins Clan_101 // Need to broadcast this event to Spectator_1 Player_1 disconnects Clan_101 // // Need to broadcast this event to Spectator_1 Player_11 joins Clan_201 // Need to broadcast this event to Spectator_2 Player_12 joins Clan_201 // // Need to broadcast this event to Spectator_2
Given the current game as a theme / channel ( Ratchet\Wamp\Topic ), I need to transfer the following player join and player left events to the game in the game / theme that the audience has subscribed to.
I am using Ratchet WebSockets for PHP on the server side and autobahn js on the client side
Below is the code. While I can send information to the server (from the client), when the player joins / disconnects the game. But how do I transmit this information to the audience (end of client) when a player joins or disconnects.
player.html
<script src="scripts/autobahn.js" type="text/javascript"></script> <script src="scripts/jquery-1.11.2.min.js" type="text/javascript"></script> <script> ab.connect( 'ws://localhost:8080', function (session) { appSession = session; $('#btnJoinGame').on('click',function(){ session.publish('joingame', ['data','GAME_ID']); }); }); </script>
spectator.html
<script> var conn = new ab.Session( 'ws://localhost:8080', function() { conn.subscribe('spectator_GAME_ID', function(topic, data) { console.log(topic); console.log(data); }); }, function() { console.warn('WebSocket connection closed'); } ); </script>
Server.php
require __DIR__ . '/vendor/autoload.php'; use Ratchet\Wamp\WampServerInterface; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface as Conn; class EventHandler implements WampServerInterface, MessageComponentInterface{ public function __construct(React\EventLoop\StreamSelectLoop $loop){ $this->loop = $loop; } public function onSubscribe(Conn $conn, $subscription, $params = array()){ $subscription->broadcast($this->data); } public function onPublish(Conn $conn, $topic, $params, array $exclude, array $eligible) { if($topic->getId() === 'joingame'){ if(!isset($this->data[$params[1]])){ $this->data[$params[1]] = array($params[0]); }else{ array_push($this->data[$params[1]], $params[0]); } } $newtopic = new Ratchet\Wamp\Topic('spectator_GAME_ID'); $this->onSubscribe($conn,$newtopic); } } $loop = React\EventLoop\Factory::create(); $webSock = new React\Socket\Server($loop); $webSock->listen(8080, '0.0.0.0');