1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Chat Class Doc Comment |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package OpenChat |
9
|
|
|
* @author Ankit Jain <[email protected]> |
10
|
|
|
* @license The MIT License (MIT) |
11
|
|
|
* @link https://github.com/ankitjain28may/openchat |
12
|
|
|
*/ |
13
|
|
|
namespace ChatApp; |
14
|
|
|
|
15
|
|
|
use Ratchet\MessageComponentInterface; |
16
|
|
|
use Ratchet\ConnectionInterface; |
17
|
|
|
// use ChatApp\Models\Message; |
18
|
|
|
use ChatApp\Reply; |
19
|
|
|
use ChatApp\Conversation; |
20
|
|
|
use ChatApp\Receiver; |
21
|
|
|
use ChatApp\SideBar; |
22
|
|
|
use ChatApp\Search; |
23
|
|
|
use ChatApp\Compose; |
24
|
|
|
use ChatApp\Online; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This Class handles the all the main functionalities for this ChatApp. |
28
|
|
|
* |
29
|
|
|
* @category PHP |
30
|
|
|
* @package OpenChat |
31
|
|
|
* @author Ankit Jain <[email protected]> |
32
|
|
|
* @license The MIT License (MIT) |
33
|
|
|
* @link https://github.com/ankitjain28may/openchat |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
class Chat implements MessageComponentInterface |
37
|
|
|
{ |
38
|
|
|
/* |
39
|
|
|
|-------------------------------------------------------------------------- |
40
|
|
|
| Chat Class |
41
|
|
|
|-------------------------------------------------------------------------- |
42
|
|
|
| |
43
|
|
|
| This Class handles the all the main functionalities for this ChatApp. |
44
|
|
|
| |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
protected $clients; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new class instance. |
51
|
|
|
* |
52
|
|
|
* @return void |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public function __construct() |
55
|
|
|
{ |
56
|
|
|
$this->clients = new \SplObjectStorage; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Open the Socket Connection and get client connection |
61
|
|
|
* |
62
|
|
|
* @param ConnectionInterface $conn To store client details |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function onOpen(ConnectionInterface $conn) |
67
|
|
|
{ |
68
|
|
|
$conn = $this->setID($conn); |
69
|
|
|
$this->clients->attach($conn); |
70
|
|
|
echo "New connection! ({$conn->resourceId})\n"; |
|
|
|
|
71
|
|
|
Online::setOnlineStatus($conn->userId); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set Session Id in Connection object |
76
|
|
|
* |
77
|
|
|
* @param ConnectionInterface $conn To store client details |
78
|
|
|
* |
79
|
|
|
* @return $conn |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function setID($conn) |
82
|
|
|
{ |
83
|
|
|
session_id($conn->WebSocket->request->getCookies()['PHPSESSID']); |
|
|
|
|
84
|
|
|
@session_start(); |
|
|
|
|
85
|
|
|
$conn->userId = $_SESSION['start']; |
|
|
|
|
86
|
|
|
session_write_close(); |
87
|
|
|
return $conn; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Send Messages to Clients |
92
|
|
|
* |
93
|
|
|
* @param ConnectionInterface $from To store client details |
94
|
|
|
* @param string $msg To store message |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function onMessage(ConnectionInterface $from, $msg) |
99
|
|
|
{ |
100
|
|
|
$msg = (object)json_decode($msg); |
101
|
|
|
if ($msg->type == 'OpenChat initiated..!') { |
102
|
|
|
$initial = (object)array(); |
103
|
|
|
$initial->initial = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if ($initial->initial != null) { |
106
|
|
|
$initial->conversation = json_decode( |
107
|
|
|
$this->onConversation( |
108
|
|
|
json_encode( |
109
|
|
|
[ |
110
|
|
|
"details" => $initial->initial[0]->login_id, |
111
|
|
|
"load" => 20, |
112
|
|
|
"userId" => $from->userId |
|
|
|
|
113
|
|
|
] |
114
|
|
|
), true |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
$from->send(json_encode($initial)); |
119
|
|
|
} else if ($msg->type == 'Load Sidebar') { |
120
|
|
|
$sidebar = (object)array(); |
121
|
|
|
$sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
122
|
|
|
$from->send(json_encode($sidebar)); |
123
|
|
|
} else if ($msg->type == 'Initiated') { |
124
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
125
|
|
|
$result = (object)array(); |
126
|
|
|
$result->conversation = json_decode( |
127
|
|
|
$this->onConversation(json_encode($msg), false) |
128
|
|
|
); |
129
|
|
|
$from->send(json_encode($result)); |
130
|
|
|
} else if ($msg->type == 'Search') { |
131
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
132
|
|
|
$searchResult = $this->onSearch($msg); |
133
|
|
|
$from->send($searchResult); |
134
|
|
|
} else if ($msg->type == 'Compose') { |
135
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
136
|
|
|
$composeResult = $this->onCompose($msg); |
137
|
|
|
$from->send($composeResult); |
138
|
|
|
} else { |
139
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
140
|
|
|
$msg->name = convert_uudecode(hex2bin($msg->name)); |
141
|
|
|
|
142
|
|
|
$getReturn = $this->onReply($msg); |
143
|
|
|
echo $getReturn; |
144
|
|
|
|
145
|
|
|
$receiveResult = (object)array(); |
146
|
|
|
$sentResult = (object)array(); |
147
|
|
|
foreach ($this->clients as $client) { |
148
|
|
|
if ($client->userId == $msg->name) { |
149
|
|
|
$receiveResult->sidebar = json_decode( |
150
|
|
|
$this->onSidebar($client->userId) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$receiveResult->reply = json_decode( |
154
|
|
|
$this->onReceiver( |
155
|
|
|
json_encode( |
156
|
|
|
[ |
157
|
|
|
"details" => $client->userId, |
158
|
|
|
"load" => 20, |
159
|
|
|
"userId" => $from->userId |
|
|
|
|
160
|
|
|
] |
161
|
|
|
), true |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$client->send(json_encode($receiveResult)); |
166
|
|
|
} else if ($client == $from) { |
167
|
|
|
$sentResult->sidebar = json_decode( |
168
|
|
|
$this->onSidebar($client->userId) |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$sentResult->conversation = json_decode( |
172
|
|
|
$this->onConversation( |
173
|
|
|
json_encode( |
174
|
|
|
[ |
175
|
|
|
"details" => bin2hex(convert_uuencode($msg->name)), |
176
|
|
|
"load" => 20, |
177
|
|
|
"userId" => $from->userId |
|
|
|
|
178
|
|
|
] |
179
|
|
|
), true |
180
|
|
|
) |
181
|
|
|
); |
182
|
|
|
$client->send(json_encode($sentResult)); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* To Call SideBar Class |
191
|
|
|
* |
192
|
|
|
* @param string $data To store data |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function onSidebar($data) |
197
|
|
|
{ |
198
|
|
|
$obSidebar = new SideBar(); |
199
|
|
|
return $obSidebar->loadSideBar($data); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* To Call Conversation Class |
204
|
|
|
* |
205
|
|
|
* @param string $data to store data |
206
|
|
|
* @param boolean $para to store True/False |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function onConversation($data, $para) |
211
|
|
|
{ |
212
|
|
|
$obConversation = new Conversation(); |
213
|
|
|
return $obConversation->conversationLoad($data, $para); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* To Call Receiver Class |
218
|
|
|
* |
219
|
|
|
* @param string $data to store data |
220
|
|
|
* @param boolean $para to store True/False |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function onReceiver($data, $para) |
225
|
|
|
{ |
226
|
|
|
$obReceiver = new Receiver(); |
227
|
|
|
return $obReceiver->receiverLoad($data, $para); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* To Call Search Class |
232
|
|
|
* |
233
|
|
|
* @param string $data to store data |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function onSearch($data) |
238
|
|
|
{ |
239
|
|
|
$obSearch = new Search(); |
240
|
|
|
return $obSearch->searchItem($data); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* To Call Compose Class |
245
|
|
|
* |
246
|
|
|
* @param string $data to store data |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function onCompose($data) |
251
|
|
|
{ |
252
|
|
|
$obCompose = new Compose(); |
253
|
|
|
return $obCompose->selectUser($data); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* To Call Reply Class |
258
|
|
|
* |
259
|
|
|
* @param string $data to store data |
260
|
|
|
* |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
public function onReply($data) |
264
|
|
|
{ |
265
|
|
|
$obReply = new Reply(); |
266
|
|
|
return $obReply->replyTo($data); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* To Call Online Class |
271
|
|
|
* |
272
|
|
|
* @param ConnectionInterface $conn To store client details |
273
|
|
|
* |
274
|
|
|
* @return void |
275
|
|
|
*/ |
276
|
|
|
public function onClose(ConnectionInterface $conn) |
277
|
|
|
{ |
278
|
|
|
Online::removeOnlineStatus($conn->userId); |
|
|
|
|
279
|
|
|
$this->clients->detach($conn); |
280
|
|
|
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* To Show error due to any problem occured |
285
|
|
|
* |
286
|
|
|
* @param ConnectionInterface $conn To store client details |
287
|
|
|
* @param \Exception $e To store exception |
288
|
|
|
* |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) |
292
|
|
|
{ |
293
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
294
|
|
|
$conn->close(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.