-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive.php
44 lines (40 loc) · 966 Bytes
/
receive.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
<?php
/**
* receive.php
* Called by the client to long poll for new messages.
* The client needs to send:
* - user name
*
* @author Paul
*/
require_once 'conn.php';
$ret = null;
$pubsub = null;
if (isset($_POST['name'])) {
$name = $_POST['name'];
$channels = $redis->smembers("channels:$name");
$pubsub = $redis->pubSub();
$pubsub->subscribe($channels);
foreach ($pubsub as $message) {
switch ($message->kind) {
case 'subscribe':
// Do nothing
break;
case 'message':
if ($message->payload == 'CANCEL') {
$ret = array('status' => 'CANCEL');
} else {
$ret = json_decode($message->payload);
}
$pubsub->unsubscribe();
break;
}
}
} else {
$ret = array('err' => 'You do not have a user, use the /me command');
}
// this needs to be called for the connection to deinit properly
unset($pubsub);
header('Content-Type: application/json');
echo json_encode($ret);
?>