-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat-server.php
More file actions
192 lines (171 loc) · 8.02 KB
/
chat-server.php
File metadata and controls
192 lines (171 loc) · 8.02 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
ini_set("log_errors", 1);
ini_set("error_log", "/dev/stderr");
require_once(dirname(__FILE__).'/vendor/autoload.php');
require_once('API/utils/Model.php');
use Ratchet\Server\IoServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
class MessageHandler implements MessageComponentInterface {
protected $clients;
protected $conn;
public function __construct() {
$this->clients = array();
$this->conn = GetDatabaseConnection();
}
public function onOpen(ConnectionInterface $conn) {
$url = parse_url($conn->httpRequest->getUri());
$path_elements = explode('/', $url['path'], 4);
error_log(json_encode($path_elements));
$client = (object)array();
$client->conn = $conn;
$client->isConsultant = $path_elements[1] === 'consultant';
$client->projectId = $path_elements[2];
if ($client->isConsultant) {
$client->storyId = intval($path_elements[3]);
}
error_log(json_encode($client));
$this->clients[$conn->resourceId] = $client;
error_log("got new connection {$conn->httpRequest->getUri()}");
}
public function onMessage(ConnectionInterface $conn, $messageString) {
error_log("from {$conn->resourceId}: $messageString");
$currentClient = $this->clients[$conn->resourceId];
$messageData = json_decode($messageString);
$type = $messageData->type;
if ($type === "text") {
$storyId = intval($messageData->storyId);
if ($storyId === null) {
$storyId = 0;
}
$slideNumber = $messageData->slideNumber;
$isConsultant = $currentClient->isConsultant;
$isTranscript = $messageData->isTranscript === true;
$text = $messageData->text;
$currentTimestamp = date('Y-m-d H:i:s');
error_log("from {$conn->resourceId}: $currentTimestamp");
$stmt = PrepareAndExecute($this->conn,
'INSERT INTO Messages (storyId, slideNumber, isConsultant, isUnread, isTranscript, text)
VALUES (?,?,?,true,?,?)',
array($storyId, $slideNumber, (int)$isConsultant, (int)$isTranscript, $text));
$message = json_encode(array(
'type' => 'text',
'storyId' => $storyId,
'slideNumber' => $slideNumber,
'isConsultant' => $isConsultant,
'isTranscript' => $isTranscript,
'timeSent' => $currentTimestamp,
'text' => $text,
));
foreach($this->clients as $client) {
if ($client->projectId === $currentClient->projectId &&
(!$client->isConsultant || $client->storyId === $storyId)) {
$client->conn->send($message);
}
}
} else if ($type === "catchup") {
$since = $messageData->since;
if (!$since) {
$since = '1970-01-01 00:00:00';
}
if ($currentClient->isConsultant) {
error_log("consultant: sending all messages for this story");
$messagesStatement = PrepareAndExecute($this->conn,
'SELECT * FROM Messages WHERE storyId = ? AND timeSent > ?',
array($currentClient->storyId, $since));
$approvalsStatement = PrepareAndExecute($this->conn,
'SELECT storyId, slideNumber, isApproved, lastApprovalChangeTime FROM Slide
WHERE storyId = ? AND lastApprovalChangeTime > ?',
array($currentClient->storyId, $since));
} else {
error_log("from phone: sending all messages for project {$currentClient->projectId}");
$messagesStatement = PrepareAndExecute($this->conn,
'SELECT * FROM Messages
JOIN Stories ON Messages.storyId = Stories.id
JOIN Projects on Stories.projectId = Projects.id
WHERE Projects.androidId = ? AND timeSent > ?',
array($currentClient->projectId, $since));
$approvalsStatement = PrepareAndExecute($this->conn,
'SELECT storyId, slideNumber, isApproved, lastApprovalChangeTime FROM Slide
JOIN Stories ON Slide.storyId = Stories.id
JOIN Projects on Stories.projectId = Projects.id
WHERE Projects.androidId = ? AND lastApprovalChangeTime > ?',
array($currentClient->projectId, $since));
}
while (($row = $messagesStatement->fetch(PDO::FETCH_ASSOC))) {
$message = json_encode(array(
'type' => 'text',
'storyId' => $row['storyId'],
'slideNumber' => $row['slideNumber'],
'isConsultant' => $row['isConsultant'] === '1' ? true : false,
'isTranscript' => $row['isTranscript'] === '1' ? true : false,
'timeSent' => $row['timeSent'],
'text' => $row['text']
));
error_log($message);
$currentClient->conn->send($message);
}
while (($row = $approvalsStatement->fetch(PDO::FETCH_ASSOC))) {
$message = json_encode(array(
'type' => 'approval',
'storyId' => $row['storyId'],
'slideNumber' => $row['slideNumber'],
'approvalStatus' => $row['isApproved'] == '1' ? true : false,
'timeSent' => $row['lastApprovalChangeTime'],
));
error_log($message);
$currentClient->conn->send($message);
}
} else if ($type === 'approval' && $currentClient->isConsultant) {
$slideNumber = $messageData->slideNumber;
$storyId = $currentClient->storyId;
$isApproved = $messageData->approvalStatus === true ? true : false;
$currentTimestamp = date('Y-m-d H:i:s');
PrepareAndExecute($this->conn,
"UPDATE Slide SET isApproved = ?, lastApprovalChangeTime = ?
WHERE slideNumber = ? AND storyId = ?",
array((int)$isApproved, $currentTimestamp, $slideNumber, $storyId));
$message = json_encode(array(
'type' => 'approval',
'storyId' => $storyId,
'slideNumber' => $slideNumber,
'approvalStatus' => $isApproved,
'timeSent' => $currentTimestamp,
));
foreach($this->clients as $client) {
if ($client->projectId === $currentClient->projectId &&
(!$client->isConsultant || $client->storyId === $storyId)) {
$client->conn->send($message);
}
}
}
}
public function onClose(ConnectionInterface $conn) {
unset($this->clients[$conn->resourceId]);
error_log("Connection to {$conn->resourceId} has closed");
}
public function onError(ConnectionInterface $conn, \Exception $e) {
error_log("Error: {$e->getMessage()}");
}
}
# $port = $GLOBALS['websocketPort'];
# $ioServer = IoServer::factory(new HttpServer(new WsServer(new MessageHandler())), $port);
# error_log("server started on port $port");
# $ioServer->run();
$port = $GLOBALS['websocketPort'];
$loop = React\EventLoop\Factory::create();
$server = new React\Socket\Server("0.0.0.0:$port", $loop);
$server->on('error', function(Exception $e) {
error_log("error: {$e->getMessage()}");
});
$secureServer = new React\Socket\SecureServer($server, $loop, [
'local_cert' => '<cert-path>',
'local_pk' => '<private-key-path>',
'allow_self_signed' => false,
'verify_peer' => false
]);
$ioServer = new Ratchet\Server\IoServer(new HttpServer(new WsServer(new MessageHandler())), $secureServer, $loop);
error_log("server started on port $port");
$ioServer->run();