-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_events.php
54 lines (44 loc) · 1.23 KB
/
game_events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require_once('redis_config.php');
header("Content-Type: text/event-stream\n\n");
// Don't stop the script after 30s.
set_time_limit(0);
// We need two clients as we want to query Redis during pubsub.
$psclient = create_redis_client();
$redis = create_redis_client();
// First, publish all currently active games.
$active_game_ids = $redis->smembers('league:active_games');
$active_games = $redis->pipeline(function($pipe) use ($active_game_ids) {
foreach ($active_game_ids as $game_id) {
$pipe->get("league:game:$game_id");
}
});
echo "event: init\n";
echo 'data: [';
echo implode(',', $active_games);
echo "]\n\n";
ob_flush();
flush();
unset($active_games);
unset($active_game_ids);
$pubsub = $psclient->pubSubLoop();
$pubsub->psubscribe('league:game:*');
foreach ($pubsub as $message) {
switch ($message->kind) {
case 'subscribe':
// No output.
break;
case 'pmessage':
// Skip league:game:
$type = substr($message->channel, 12);
$game_id = $message->payload;
$game = $redis->get("league:game:$game_id");
echo "event: $type\n";
echo "data: $game\n\n";
ob_flush();
flush();
break;
}
}
// We shouldn't get here, but just in case, do proper cleanup.
unset($pubsub);