2222 *
2323 * The main Chat controller for mysql_websocket_chat
2424 *
25- * PHP version 7.2
25+ * PHP version 7.2 and up.
26+ * Chat
2627 *
2728 * @category Configuration
2829 * @package Mysql_Websocket_Chat
2930 * @author Johnny Mast <mastjohnny@gmail.com>
3031 * @license https://opensource.org/licenses/MIT MIT
3132 * @link https://github.com/johnnymast/mysql_websocket_chat
32- * @since GIT: 1.0
33+ * @since 1.0
3334 */
3435class Chat implements MessageComponentInterface
3536{
3637 /**
3738 * This member keeps track of all
3839 * connected clients.
3940 *
40- * @var SplObjectStorage
41+ * @var \ SplObjectStorage
4142 */
4243 protected $ clients = null ;
4344
@@ -86,132 +87,131 @@ public function onOpen(ConnectionInterface $conn): void
8687 * @param string $msg The message being sent
8788 *
8889 * @return void
90+ * @throws \Exception
8991 */
9092 public function onMessage (ConnectionInterface $ from , $ msg ): void
9193 {
9294 foreach ($ this ->clients as $ client ) {
9395 $ package = json_decode ($ msg );
9496
95- if (is_object ($ package ) == true ) {
97+ if (is_object ($ package ) === true ) {
98+
9699 /**
97100 * We need to switch the message type because in the future
98101 * this could be a message or maybe a request for all chatters
99102 * in the chat. For now we only use the message type but we can
100103 * build on that later.
101104 */
102105 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 ) {
116109
117110 /**
118- * Non target users will not see this message
119- * on their screens.
111+ * Find the client to send the message to
120112 */
121- if ($ user ['user ' ]->id == $ package ->to_user ) {
122-
113+ foreach ($ this ->users as $ resourceId => $ user ) {
123114
124115 /**
125- * Defined in includes/config.php
116+ * Non target users will not see this message
117+ * on their screens.
126118 */
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+ }
140138 }
141- }
142139
143- $ targetClient = $ user ['client ' ];
144- $ targetClient ->send ($ msg );
145- return ;
140+ $ targetClient = $ user ['client ' ];
141+ $ targetClient ->send ($ msg );
142+ return ;
143+ }
146144 }
147- }
148- }
145+ } else {
149146
150147
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- ) {
158148 /**
159- * Insert private chat
149+ * Defined in includes/config.php
160150 */
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 );
167167 }
168168 }
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+ }
202199
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+ ];
208205
209- $ targetClient = $ user ['client ' ];
210- $ targetClient ->send ($ msg );
206+ $ targetClient = $ user ['client ' ];
207+ $ targetClient ->send ($ msg );
208+ }
211209 }
212210 }
213- }
214- break ;
211+ break ;
212+ default :
213+ throw new \Exception ('Unexpected value ' );
214+ break ;
215215 }
216216 }
217217 }
@@ -234,7 +234,7 @@ public function onClose(ConnectionInterface $conn): void
234234 * The onError callback. Will be called on you guessed it, an error :)
235235 *
236236 * @param ConnectionInterface $conn The unique connection identifier.
237- * @param Exception $e The raised exception
237+ * @param \ Exception $e The raised exception
238238 *
239239 * @return void
240240 */
0 commit comments