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
|
|
|
$sessionId = $from->WebSocket->request->getCookies()['PHPSESSID']; |
|
|
|
|
39
|
|
|
if($msg == 'OpenChat initiated..!') |
40
|
|
|
{ |
41
|
|
|
$initial = (object) array(); |
42
|
|
|
$initial->initial = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if($initial->initial != null) { |
45
|
|
|
$initial->conversation = json_decode( |
46
|
|
|
$this->onConversation( |
47
|
|
|
json_encode([ |
48
|
|
|
"username" => $initial->initial[0]->login_id, |
49
|
|
|
"load" => 10, |
50
|
|
|
"userId" => $from->userId |
|
|
|
|
51
|
|
|
]), True |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
$from->send(json_encode($initial)); |
56
|
|
|
} |
57
|
|
|
elseif ($msg == 'Load Sidebar') |
58
|
|
|
{ |
59
|
|
|
$sidebar = (object) array(); |
60
|
|
|
$sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
61
|
|
|
$from->send(json_encode($sidebar)); |
62
|
|
|
} |
63
|
|
|
elseif (@json_decode($msg)->newConversation == 'Initiated') |
64
|
|
|
{ |
65
|
|
|
$msg = json_decode($msg); |
66
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
67
|
|
|
$result = (object) array(); |
68
|
|
|
$result->conversation = json_decode($this->onConversation(json_encode($msg), False)); |
69
|
|
|
$from->send(json_encode($result)); |
70
|
|
|
} |
71
|
|
|
elseif (@json_decode($msg)->search == 'search') |
72
|
|
|
{ |
73
|
|
|
$msg = json_decode($msg); |
74
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
75
|
|
|
$searchResult = $this->onSearch($msg); |
76
|
|
|
$from->send($searchResult); |
77
|
|
|
} |
78
|
|
|
elseif (@json_decode($msg)->Compose == 'Compose') |
79
|
|
|
{ |
80
|
|
|
$msg = json_decode($msg); |
81
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
82
|
|
|
$composeResult = $this->onCompose($msg); |
83
|
|
|
$from->send($composeResult); |
84
|
|
|
} |
85
|
|
|
else |
86
|
|
|
{ |
87
|
|
|
$msg = (object) json_decode($msg); |
88
|
|
|
$msg->userId = $from->userId; |
|
|
|
|
89
|
|
|
$getReturn = $this->onReply($msg); |
90
|
|
|
echo $getReturn; |
91
|
|
|
|
92
|
|
|
$receiveResult = (object) array(); |
93
|
|
|
$sentResult = (object) array(); |
94
|
|
|
foreach ($this->clients as $client) |
95
|
|
|
{ |
96
|
|
|
if ($client->userId == $msg->name) |
97
|
|
|
{ |
98
|
|
|
$receiveResult->sidebar = json_decode($this->onSidebar($client->userId)); |
99
|
|
|
|
100
|
|
|
$receiveResult->reply = json_decode( |
101
|
|
|
$this->onReceiver( |
102
|
|
|
json_encode([ |
103
|
|
|
"username" => $client->userId, |
104
|
|
|
"load" => 10, |
105
|
|
|
"userId" => $from->userId |
|
|
|
|
106
|
|
|
]), True |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$client->send(json_encode($receiveResult)); |
111
|
|
|
} |
112
|
|
|
elseif($client == $from) |
113
|
|
|
{ |
114
|
|
|
$sentResult->sidebar = json_decode($this->onSidebar($client->userId)); |
115
|
|
|
|
116
|
|
|
$sentResult->conversation = json_decode( |
117
|
|
|
$this->onConversation( |
118
|
|
|
json_encode([ |
119
|
|
|
"username" => $msg->name, |
120
|
|
|
"load" => 10, |
121
|
|
|
"userId" => $from->userId |
|
|
|
|
122
|
|
|
]), True |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$client->send(json_encode($sentResult)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function onSidebar($data) |
134
|
|
|
{ |
135
|
|
|
$obSidebar = new Sidebar(); |
136
|
|
|
return $obSidebar->loadSideBar($data); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function onConversation($data, $para) |
140
|
|
|
{ |
141
|
|
|
$obConversation = new Conversation(); |
142
|
|
|
return $obConversation->conversationLoad($data, $para); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function onReceiver($data, $para) |
146
|
|
|
{ |
147
|
|
|
$obReceiver = new Receiver(); |
148
|
|
|
return $obReceiver->receiverLoad($data, $para); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function onSearch($data) |
152
|
|
|
{ |
153
|
|
|
$obSearch = new Search(); |
154
|
|
|
return $obSearch->searchItem($data); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function onCompose($data) |
158
|
|
|
{ |
159
|
|
|
$obCompose = new Compose(); |
160
|
|
|
return $obCompose->selectUser($data); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function onReply($data) |
164
|
|
|
{ |
165
|
|
|
$obReply = new Reply(); |
166
|
|
|
return $obReply->replyTo($data); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function onClose(ConnectionInterface $conn) { |
170
|
|
|
Online::removeOnlineStatus($conn->userId); |
|
|
|
|
171
|
|
|
$this->clients->detach($conn); |
172
|
|
|
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) { |
175
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
176
|
|
|
$conn->close(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
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: