|
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
|
|
|
protected $online; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
$this->clients = new \SplObjectStorage; |
|
20
|
|
|
$this->result = ''; |
|
|
|
|
|
|
21
|
|
|
$this->online = 0; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function onOpen(ConnectionInterface $conn) { |
|
25
|
|
|
$conn = $this->setID($conn); |
|
26
|
|
|
$this->clients->attach($conn); |
|
27
|
|
|
echo "New connection! ({$conn->resourceId})\n"; |
|
28
|
|
|
Online::setOnlineStatus($conn->userId); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setID($conn) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
var_dump($conn->WebSocket->request->getCookies()['PHPSESSID']); |
|
|
|
|
|
|
34
|
|
|
session_id($conn->WebSocket->request->getCookies()['PHPSESSID']); |
|
35
|
|
|
@session_start(); |
|
|
|
|
|
|
36
|
|
|
$conn->userId = $_SESSION['start']; |
|
37
|
|
|
session_write_close(); |
|
38
|
|
|
return $conn; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function onMessage(ConnectionInterface $from, $msg) { |
|
42
|
|
|
$sessionId = $from->WebSocket->request->getCookies()['PHPSESSID']; |
|
|
|
|
|
|
43
|
|
|
if($msg == 'OpenChat initiated..!') |
|
44
|
|
|
{ |
|
45
|
|
|
@$initial->initial = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
@$initial->conversation = json_decode( |
|
|
|
|
|
|
48
|
|
|
$this->onConversation( |
|
49
|
|
|
json_encode([ |
|
50
|
|
|
"username" => $initial->initial[0]->login_id, |
|
51
|
|
|
"load" => 10 |
|
52
|
|
|
]), True, $sessionId |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
@$initial->conversation[0]->login_status = $this->online; |
|
|
|
|
|
|
57
|
|
|
$from->send(json_encode($initial)); |
|
58
|
|
|
} |
|
59
|
|
|
elseif ($msg == 'Load Sidebar') |
|
60
|
|
|
{ |
|
61
|
|
|
@$initial->initial = json_decode($this->onSidebar($from->userId)); |
|
|
|
|
|
|
62
|
|
|
$from->send(json_encode($initial)); |
|
63
|
|
|
} |
|
64
|
|
|
elseif (@json_decode($msg)->newConversation == 'Initiated') |
|
65
|
|
|
{ |
|
66
|
|
|
@$result->conversation = json_decode($this->onConversation($msg, False, $sessionId)); |
|
|
|
|
|
|
67
|
|
|
$from->send(json_encode($result)); |
|
68
|
|
|
} |
|
69
|
|
|
elseif (@json_decode($msg)->search == 'search') |
|
70
|
|
|
{ |
|
71
|
|
|
$searchResult = $this->onSearch($msg, $sessionId); |
|
72
|
|
|
$from->send($searchResult); |
|
73
|
|
|
} |
|
74
|
|
|
elseif (@json_decode($msg)->Compose == 'Compose') |
|
75
|
|
|
{ |
|
76
|
|
|
$composeResult = $this->onCompose($msg, $sessionId); |
|
77
|
|
|
$from->send($composeResult); |
|
78
|
|
|
} |
|
79
|
|
|
else |
|
80
|
|
|
{ |
|
81
|
|
|
$this->onReply($msg, $sessionId); |
|
82
|
|
|
|
|
83
|
|
|
$msg = json_decode($msg); |
|
84
|
|
|
// $msg->from = $from->userId; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->clients as $client) |
|
87
|
|
|
{ |
|
88
|
|
|
if ($client->userId == $msg->name) |
|
89
|
|
|
{ |
|
90
|
|
|
@$result->sidebar = json_decode($this->onSidebar($client->userId)); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
@$result->conversation = json_decode( |
|
|
|
|
|
|
93
|
|
|
$this->onReceiver( |
|
94
|
|
|
json_encode([ |
|
95
|
|
|
"username" => $client->userId, |
|
96
|
|
|
"load" => 10 |
|
97
|
|
|
]), True, $sessionId |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$client->send(json_encode($result)); |
|
102
|
|
|
$this->online = 1; |
|
103
|
|
|
} |
|
104
|
|
|
elseif($client == $from) |
|
105
|
|
|
{ |
|
106
|
|
|
@$result->sidebar = json_decode($this->onSidebar($client->userId)); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
@$result->conversation = json_decode( |
|
|
|
|
|
|
109
|
|
|
$this->onConversation( |
|
110
|
|
|
json_encode([ |
|
111
|
|
|
"username" => $msg->name, |
|
112
|
|
|
"load" => 10 |
|
113
|
|
|
]), True, $sessionId |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$result->conversation[0]->login_status = $this->online; |
|
118
|
|
|
$client->send(json_encode($result)); |
|
119
|
|
|
$this->online = 0; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function onSidebar($data) |
|
127
|
|
|
{ |
|
128
|
|
|
$obSidebar = new Sidebar(); |
|
129
|
|
|
return $obSidebar->LoadSideBar($data); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function onConversation($data, $para, $sessionId) |
|
133
|
|
|
{ |
|
134
|
|
|
$obConversation = new Conversation($sessionId); |
|
135
|
|
|
return $obConversation->ConversationLoad($data, $para); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function onReceiver($data, $para, $sessionId) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$obReceiver = new Receiver($sessionId); |
|
141
|
|
|
return $obReceiver->ReceiverLoad($data, True); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function onSearch($data, $sessionId) |
|
145
|
|
|
{ |
|
146
|
|
|
$obSearch = new Search($sessionId); |
|
147
|
|
|
return $obSearch->SearchItem(json_decode($data)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function onCompose($data, $sessionId) |
|
151
|
|
|
{ |
|
152
|
|
|
$obCompose = new Compose($sessionId); |
|
153
|
|
|
return $obCompose->SelectUser(json_decode($data)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function onReply($data, $sessionId) |
|
157
|
|
|
{ |
|
158
|
|
|
$obReply = new Reply($sessionId); |
|
159
|
|
|
$obReply->replyTo($data); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function onClose(ConnectionInterface $conn) { |
|
163
|
|
|
Online::removeOnlineStatus($conn->userId); |
|
|
|
|
|
|
164
|
|
|
$this->clients->detach($conn); |
|
165
|
|
|
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) { |
|
168
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
|
169
|
|
|
$conn->close(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: