-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
47 lines (43 loc) · 1.08 KB
/
chat.php
File metadata and controls
47 lines (43 loc) · 1.08 KB
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
<?php
/**
* chat.php
* Sends a chat message.
* The client needs to send:
* - user name
* - message
* - channel name (optional)
*
* @author Paul
*/
require_once 'conn.php';
$ret = null;
if (isset($_POST['name'])) {
$name = $_POST['name'];
if (isset($_POST['message'])) {
$message = $_POST['message'];
// the channel defaults to the 'all' channel
if (isset($_POST['channel'])) {
$channel = $_POST['channel'];
} else {
$channel = 'all';
}
// the message is stored as a serialized JSON object
$message = array(
'name' => $name,
'message' => $message,
'channel' => $channel
);
// publish the message, refresh the expiration on the user
$ret = array('status' => 'OK');
$redis->publish("channel:$channel", json_encode($message));
$redis->expire("user:$name", 60 * 3);
$redis->expire("channels:$name", 60 * 3);
} else {
$ret = array('err' => 'Missing message');
}
} else {
$ret = array('err' => 'You do not have a user, use the /me command');
}
header('Content-Type: application/json');
echo json_encode($ret);
?>