-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat-server_WS.php
More file actions
249 lines (228 loc) · 11.1 KB
/
chat-server_WS.php
File metadata and controls
249 lines (228 loc) · 11.1 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?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]);
} else {
$client->storyId = 0;
}
error_log(json_encode($client));
//Check to see if there is already a connection to this phone ($isConsultant = false). If so, remove from list
/*
if ($client->isConsultant == false) {
foreach($this->clients as $tmpClient) {
if ($tmpClient->projectId === $client->projectId &&
$tmpClient->isConsultant === $client->isConsultant &&
$tmpClient->storyId === $client->storyId) {
unset($this->clients[$tmpClient->conn->resourceId]);
$tmpClient->conn->close();
error_log("Removed duplicate connection " . $tmpClient->conn->resourceId . " to phone " . $client->projectId);
}
}
}
*/
$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);
error_log("Tmp2: $messageString");
$type = $messageData->type;
if ($type === "text") {
$storyId = intval($messageData->storyId);
$slideNumber = $messageData->slideNumber;
$isConsultant = $currentClient->isConsultant;
if (!empty($messageData->isTranscript)) {
$isTranscript = $messageData->isTranscript === true;
} else {
$isTranscript = false;
}
$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)) {
error_log("To: " . $client->conn->resourceId);
error_log("Tmp: $message");
error_log("Prj: $client->projectId");
error_log("Cns: $client->isConsultant");
error_log("Sty: $client->storyId");
$client->conn->send($message);
}
}
} else if ($type === "catchup") {
if (!empty($messageData->since)) {
$since = $messageData->since;
} else {
$since = 0;
}
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("To: " . $currentClient->conn->resourceId);
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("To: " . $currentClient->conn->resourceId);
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)) {
error_log("To: " . $client->conn->resourceId);
error_log($message);
$client->conn->send($message);
}
}
FCMSend($storyId, $currentClient->projectId);
}
}
public function onClose(ConnectionInterface $conn) {
error_log("onClose: projectId = " . $this->clients[$conn->resourceId]->projectId . " isConsultant = " . $this->clients[$conn->resourceId]->isConsultant);
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()}");
}
}
function FCMSend(int $storyId, $projectId) {
$conn = GetDatabaseConnection();
$SQL = 'SELECT SUM(isApproved) AS Approved, COUNT(isApproved) AS Total, title FROM Stories ' .
'LEFT JOIN Slide ON Stories.id = Slide.storyId WHERE Stories.id = ' . $storyId;
$Pct = PrepareAndExecute($conn, $SQL, array());
while (($row = $Pct->fetch(PDO::FETCH_ASSOC))) {
// according to Robin... the ROCC user needs to approve the song slide even if it is blank - 10/7/22
$PctApproved = (int)($row['Approved'] / ($row['Total'] - 0) * 100);
$Title = $row['title'];
}
if ($PctApproved == 100) { // 100% of slides approved
error_log("projectID: " . $projectId);
$SQL = 'SELECT fcmToken FROM Projects WHERE androidId = "' . $projectId . '"';
$FCMToken = PrepareAndExecute($conn, $SQL, array());
$fcmToken = "";
while (($row = $FCMToken->fetch(PDO::FETCH_ASSOC))) {
$fcmToken = $row['fcmToken'];
}
if ($fcmToken != "") {
time_nanosleep(0, 250000000);
$cmd = 'curl -X POST --header "Authorization: key=AAAAU8MDzIQ:APA91bEm-Xskg66XnJXnUe5MvFs60eHiq-14eCiZ3n7atak-mbYcz7idkWQ7OB1IDDsQV0TPWhixEX_StNGCZUemP805qd4vzKndmvuAMcvfmr35gZZTzN3qVeXsBnmB3lGHZB-9QdVT " --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"' . $fcmToken . '\",\"notification\":{\"title\":\"Story Producer Adv\",\"body\":\"Story - ' . $Title . ' - ' . $PctApproved . '% audio files approved.\"}}"';
$result = shell_exec($cmd);
error_log("100% approved, notification: " . $result);
}
}
}
# $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, [
//'allow_self_signed' => false,
//'verify_peer' => false
//]);
$ioServer = new Ratchet\Server\IoServer(new HttpServer(new WsServer(new MessageHandler())), $server, $loop);
error_log("server started on port $port");
$ioServer->run();