22
22
*
23
23
* The main Chat controller for mysql_websocket_chat
24
24
*
25
- * PHP version 7.2
25
+ * PHP version 7.2 and up.
26
+ * Chat
26
27
*
27
28
* @category Configuration
28
29
* @package Mysql_Websocket_Chat
29
30
* @author Johnny Mast <[email protected] >
30
31
* @license https://opensource.org/licenses/MIT MIT
31
32
* @link https://github.com/johnnymast/mysql_websocket_chat
32
- * @since GIT: 1.0
33
+ * @since 1.0
33
34
*/
34
35
class Chat implements MessageComponentInterface
35
36
{
36
37
/**
37
38
* This member keeps track of all
38
39
* connected clients.
39
40
*
40
- * @var SplObjectStorage
41
+ * @var \ SplObjectStorage
41
42
*/
42
43
protected $ clients = null ;
43
44
@@ -86,132 +87,131 @@ public function onOpen(ConnectionInterface $conn): void
86
87
* @param string $msg The message being sent
87
88
*
88
89
* @return void
90
+ * @throws \Exception
89
91
*/
90
92
public function onMessage (ConnectionInterface $ from , $ msg ): void
91
93
{
92
94
foreach ($ this ->clients as $ client ) {
93
95
$ package = json_decode ($ msg );
94
96
95
- if (is_object ($ package ) == true ) {
97
+ if (is_object ($ package ) === true ) {
98
+
96
99
/**
97
100
* We need to switch the message type because in the future
98
101
* this could be a message or maybe a request for all chatters
99
102
* in the chat. For now we only use the message type but we can
100
103
* build on that later.
101
104
*/
102
105
switch ($ package ->type ) {
103
- case 'message ' :
104
- if ($ from != $ client ) {
105
- if (empty ($ package ->to_user ) == false ) {
106
-
107
-
108
- /**
109
- * Find the client to send the message to
110
- */
111
- foreach ($ this ->users as $ resourceId => $ user ) {
112
- if ($ resourceId == $ from ->resourceId ) {
113
- continue ;
114
- }
115
-
106
+ case 'message ' :
107
+ if ($ from !== $ client ) {
108
+ if (empty ($ package ->to_user ) == false && isset ($ package ->to_user ->id ) == true ) {
116
109
117
110
/**
118
- * Non target users will not see this message
119
- * on their screens.
111
+ * Find the client to send the message to
120
112
*/
121
- if ($ user ['user ' ]->id == $ package ->to_user ) {
122
-
113
+ foreach ($ this ->users as $ resourceId => $ user ) {
123
114
124
115
/**
125
- * Defined in includes/config.php
116
+ * Non target users will not see this message
117
+ * on their screens.
126
118
*/
127
- if (ENABLE_DATABASE == true ) {
128
- if (isset ($ package ->user )
129
- and is_object ($ package ->user ) == true
130
- ) {
131
- /**
132
- * Insert channel chat
133
- */
134
- $ this ->db ->insert (
135
- $ package ->to_user ->id ,
136
- $ package ->user ->id ,
137
- $ package ->message ,
138
- $ client ->remoteAddress
139
- );
119
+ if ($ user ['user ' ]->id === $ package ->to_user ->id ) {
120
+
121
+ /**
122
+ * Defined in includes/config.php
123
+ */
124
+ if (ENABLE_DATABASE == true ) {
125
+ if (isset ($ package ->user )
126
+ && is_object ($ package ->user ) == true
127
+ ) {
128
+ /**
129
+ * Insert private chat
130
+ */
131
+ $ this ->db ->insert (
132
+ $ package ->to_user ->id ,
133
+ $ package ->user ->id ,
134
+ $ package ->message ,
135
+ $ client ->remoteAddress
136
+ );
137
+ }
140
138
}
141
- }
142
139
143
- $ targetClient = $ user ['client ' ];
144
- $ targetClient ->send ($ msg );
145
- return ;
140
+ $ targetClient = $ user ['client ' ];
141
+ $ targetClient ->send ($ msg );
142
+ return ;
143
+ }
146
144
}
147
- }
148
- }
145
+ } else {
149
146
150
147
151
- /**
152
- * Defined in includes/config.php
153
- */
154
- if (ENABLE_DATABASE == true ) {
155
- if (isset ($ package ->user )
156
- and is_object ($ package ->user ) == true
157
- ) {
158
148
/**
159
- * Insert private chat
149
+ * Defined in includes/config.php
160
150
*/
161
- $ this ->db ->insert (
162
- $ package ->to_user ->id ,
163
- $ package ->user ->id ,
164
- $ package ->message ,
165
- $ client ->remoteAddress
166
- );
151
+ if (ENABLE_DATABASE == true ) {
152
+ if (isset ($ package ->user )
153
+ and is_object ($ package ->user ) == true
154
+ ) {
155
+ /**
156
+ * Insert channel chat
157
+ */
158
+ $ this ->db ->insert (
159
+ null ,
160
+ $ package ->user ->id ,
161
+ $ package ->message ,
162
+ $ client ->remoteAddress
163
+ );
164
+ }
165
+ }
166
+ $ client ->send ($ msg );
167
167
}
168
168
}
169
- $ client ->send ($ msg );
170
- }
171
- break ;
172
- case 'registration ' :
173
- $ this ->users [$ from ->resourceId ] = [
174
- 'user ' => $ package ->user ,
175
- 'client ' => $ from
176
- ];
177
- break ;
178
- case 'userlist ' :
179
- $ list = [];
180
- foreach ($ this ->users as $ resourceId => $ value ) {
181
- $ list [$ resourceId ] = $ value ['user ' ];
182
- }
183
- $ new_package = [
184
- 'users ' => $ list ,
185
- 'type ' => 'userlist '
186
- ];
187
- $ new_package = json_encode ($ new_package );
188
- $ client ->send ($ new_package );
189
- break ;
190
-
191
- case 'typing ' :
192
- if ($ from != $ client ) {
193
-
194
- if (empty ($ package ->user ) == false ) {
195
- /**
196
- * Find the client to send the message to
197
- */
198
- foreach ($ this ->users as $ resourceId => $ user ) {
199
- if ($ resourceId == $ from ->resourceId ) {
200
- continue ;
201
- }
169
+ break ;
170
+ case 'registration ' :
171
+ $ this ->users [$ from ->resourceId ] = [
172
+ 'user ' => $ package ->user ,
173
+ 'client ' => $ from
174
+ ];
175
+ break ;
176
+ case 'userlist ' :
177
+ $ list = [];
178
+ foreach ($ this ->users as $ resourceId => $ value ) {
179
+ $ list [] = $ value ['user ' ];
180
+ }
181
+ $ new_package = [
182
+ 'users ' => $ list ,
183
+ 'type ' => 'userlist '
184
+ ];
185
+ $ new_package = json_encode ($ new_package );
186
+ $ client ->send ($ new_package );
187
+ break ;
188
+
189
+ case 'typing ' :
190
+ if ($ from != $ client ) {
191
+ if (empty ($ package ->user ) == false ) {
192
+ /**
193
+ * Find the client to send the message to
194
+ */
195
+ foreach ($ this ->users as $ resourceId => $ user ) {
196
+ if ($ resourceId == $ from ->resourceId ) {
197
+ continue ;
198
+ }
202
199
203
- $ new_package = [
204
- 'user ' => $ package ->user ,
205
- 'type ' => 'typing ' ,
206
- 'value ' => $ package ->value ,
207
- ];
200
+ $ new_package = [
201
+ 'user ' => $ package ->user ,
202
+ 'type ' => 'typing ' ,
203
+ 'value ' => $ package ->value ,
204
+ ];
208
205
209
- $ targetClient = $ user ['client ' ];
210
- $ targetClient ->send ($ msg );
206
+ $ targetClient = $ user ['client ' ];
207
+ $ targetClient ->send ($ msg );
208
+ }
211
209
}
212
210
}
213
- }
214
- break ;
211
+ break ;
212
+ default :
213
+ throw new \Exception ('Unexpected value ' );
214
+ break ;
215
215
}
216
216
}
217
217
}
@@ -234,7 +234,7 @@ public function onClose(ConnectionInterface $conn): void
234
234
* The onError callback. Will be called on you guessed it, an error :)
235
235
*
236
236
* @param ConnectionInterface $conn The unique connection identifier.
237
- * @param Exception $e The raised exception
237
+ * @param \ Exception $e The raised exception
238
238
*
239
239
* @return void
240
240
*/
0 commit comments