1
|
|
|
<?php |
2
|
|
|
namespace ChatApp; |
3
|
|
|
use Ratchet\MessageComponentInterface; |
4
|
|
|
use Ratchet\ConnectionInterface; |
5
|
|
|
use ChatApp\Models\Message; |
6
|
|
|
use ChatApp\Reply; |
7
|
|
|
use ChatApp\Conversation; |
8
|
|
|
use ChatApp\Receiver; |
9
|
|
|
use ChatApp\SideBar; |
10
|
|
|
use ChatApp\Search; |
11
|
|
|
use ChatApp\Compose; |
12
|
|
|
use ChatApp\Online; |
13
|
|
|
|
14
|
|
|
class Chat implements MessageComponentInterface { |
15
|
|
|
protected $clients; |
16
|
|
|
|
17
|
|
|
public function __construct() { |
18
|
|
|
$this->clients = new \SplObjectStorage; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function onOpen(ConnectionInterface $conn) { |
22
|
|
|
$conn = $this->setID($conn); |
23
|
|
|
$this->clients->attach($conn); |
24
|
|
|
echo "New connection! ({$conn->resourceId})\n"; |
25
|
|
|
Online::setOnlineStatus($conn->userId); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setID($conn) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
session_id($conn->WebSocket->request->getCookies()['PHPSESSID']); |
31
|
|
|
@session_start(); |
|
|
|
|
32
|
|
|
$conn->userId = $_SESSION['start']; |
33
|
|
|
session_write_close(); |
34
|
|
|
return $conn; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function onMessage(ConnectionInterface $from, $msg) |
38
|
|
|
{ |
39
|
|
|
$msg = (object) json_decode($msg); |
40
|
|
|
if($msg->type == 'OpenChat initiated..!') |
41
|
|
|
{ |
42
|
|
|
$initial = (object) array(); |
43
|
|
|
$initial->initial = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
44
|
|
|
|
45
|
|
|
if($initial->initial != null) |
46
|
|
|
{ |
47
|
|
|
$initial->conversation = json_decode( |
48
|
|
|
$this->onConversation( |
49
|
|
|
json_encode([ |
50
|
|
|
"username" => $initial->initial[0]->login_id, |
51
|
|
|
"load" => 10, |
52
|
|
|
"userId" => $from->userId |
|
|
|
|
53
|
|
|
]), True |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
$from->send(json_encode($initial)); |
58
|
|
|
} |
59
|
|
|
elseif ($msg->type == 'Load Sidebar') |
60
|
|
|
{ |
61
|
|
|
$sidebar = (object) array(); |
62
|
|
|
$sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
63
|
|
|
$from->send(json_encode($sidebar)); |
64
|
|
|
} |
65
|
|
|
elseif ($msg->type == 'Initiated') |
66
|
|
|
{ |
67
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
68
|
|
|
$result = (object) array(); |
69
|
|
|
$result->conversation = json_decode($this->onConversation(json_encode($msg), False)); |
70
|
|
|
$from->send(json_encode($result)); |
71
|
|
|
} |
72
|
|
|
elseif ($msg->type == 'Search') |
73
|
|
|
{ |
74
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
75
|
|
|
$searchResult = $this->onSearch($msg); |
76
|
|
|
$from->send($searchResult); |
77
|
|
|
} |
78
|
|
|
elseif ($msg->type == 'Compose') |
79
|
|
|
{ |
80
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
81
|
|
|
$composeResult = $this->onCompose($msg); |
82
|
|
|
$from->send($composeResult); |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
87
|
|
|
$getReturn = $this->onReply($msg); |
88
|
|
|
echo $getReturn; |
89
|
|
|
|
90
|
|
|
$receiveResult = (object) array(); |
91
|
|
|
$sentResult = (object) array(); |
92
|
|
|
foreach ($this->clients as $client) |
93
|
|
|
{ |
94
|
|
|
if ($client->userId == $msg->name) |
95
|
|
|
{ |
96
|
|
|
$receiveResult->sidebar = json_decode($this->onSidebar($client->userId)); |
97
|
|
|
|
98
|
|
|
$receiveResult->reply = json_decode( |
99
|
|
|
$this->onReceiver( |
100
|
|
|
json_encode([ |
101
|
|
|
"username" => $client->userId, |
102
|
|
|
"load" => 10, |
103
|
|
|
"userId" => $from->userId |
|
|
|
|
104
|
|
|
]), True |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$client->send(json_encode($receiveResult)); |
109
|
|
|
} |
110
|
|
|
elseif($client == $from) |
111
|
|
|
{ |
112
|
|
|
$sentResult->sidebar = json_decode($this->onSidebar($client->userId)); |
113
|
|
|
|
114
|
|
|
$sentResult->conversation = json_decode( |
115
|
|
|
$this->onConversation( |
116
|
|
|
json_encode([ |
117
|
|
|
"username" => $msg->name, |
118
|
|
|
"load" => 10, |
119
|
|
|
"userId" => $from->userId |
|
|
|
|
120
|
|
|
]), True |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$client->send(json_encode($sentResult)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function onSidebar($data) |
132
|
|
|
{ |
133
|
|
|
$obSidebar = new Sidebar(); |
134
|
|
|
return $obSidebar->loadSideBar($data); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function onConversation($data, $para) |
138
|
|
|
{ |
139
|
|
|
$obConversation = new Conversation(); |
140
|
|
|
return $obConversation->conversationLoad($data, $para); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function onReceiver($data, $para) |
144
|
|
|
{ |
145
|
|
|
$obReceiver = new Receiver(); |
146
|
|
|
return $obReceiver->receiverLoad($data, $para); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function onSearch($data) |
150
|
|
|
{ |
151
|
|
|
$obSearch = new Search(); |
152
|
|
|
return $obSearch->searchItem($data); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function onCompose($data) |
156
|
|
|
{ |
157
|
|
|
$obCompose = new Compose(); |
158
|
|
|
return $obCompose->selectUser($data); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function onReply($data) |
162
|
|
|
{ |
163
|
|
|
$obReply = new Reply(); |
164
|
|
|
return $obReply->replyTo($data); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function onClose(ConnectionInterface $conn) { |
168
|
|
|
Online::removeOnlineStatus($conn->userId); |
|
|
|
|
169
|
|
|
$this->clients->detach($conn); |
170
|
|
|
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) { |
173
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
174
|
|
|
$conn->close(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: