-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
7b3f874
commit 6faa135
Showing
5 changed files
with
11,686 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,22 +22,23 @@ | |
* | ||
* The main Chat controller for mysql_websocket_chat | ||
* | ||
* PHP version 7.2 | ||
* PHP version 7.2 and up. | ||
* Chat | ||
* | ||
* @category Configuration | ||
* @package Mysql_Websocket_Chat | ||
* @author Johnny Mast <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/johnnymast/mysql_websocket_chat | ||
* @since GIT:1.0 | ||
* @since 1.0 | ||
*/ | ||
class Chat implements MessageComponentInterface | ||
{ | ||
/** | ||
* This member keeps track of all | ||
* connected clients. | ||
* | ||
* @var SplObjectStorage | ||
* @var \SplObjectStorage | ||
*/ | ||
protected $clients = null; | ||
|
||
|
@@ -86,132 +87,131 @@ public function onOpen(ConnectionInterface $conn): void | |
* @param string $msg The message being sent | ||
* | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function onMessage(ConnectionInterface $from, $msg): void | ||
{ | ||
foreach ($this->clients as $client) { | ||
$package = json_decode($msg); | ||
|
||
if (is_object($package) == true) { | ||
if (is_object($package) === true) { | ||
|
||
/** | ||
* We need to switch the message type because in the future | ||
* this could be a message or maybe a request for all chatters | ||
* in the chat. For now we only use the message type but we can | ||
* build on that later. | ||
*/ | ||
switch ($package->type) { | ||
case 'message': | ||
if ($from != $client) { | ||
if (empty($package->to_user) == false) { | ||
|
||
|
||
/** | ||
* Find the client to send the message to | ||
*/ | ||
foreach ($this->users as $resourceId => $user) { | ||
if ($resourceId == $from->resourceId) { | ||
continue; | ||
} | ||
|
||
case 'message': | ||
if ($from !== $client) { | ||
if (empty($package->to_user) == false && isset($package->to_user->id) == true) { | ||
|
||
/** | ||
* Non target users will not see this message | ||
* on their screens. | ||
* Find the client to send the message to | ||
*/ | ||
if ($user['user']->id == $package->to_user) { | ||
|
||
foreach ($this->users as $resourceId => $user) { | ||
|
||
/** | ||
* Defined in includes/config.php | ||
* Non target users will not see this message | ||
* on their screens. | ||
*/ | ||
if (ENABLE_DATABASE == true) { | ||
if (isset($package->user) | ||
and is_object($package->user) == true | ||
) { | ||
/** | ||
* Insert channel chat | ||
*/ | ||
$this->db->insert( | ||
$package->to_user->id, | ||
$package->user->id, | ||
$package->message, | ||
$client->remoteAddress | ||
); | ||
if ($user['user']->id === $package->to_user->id) { | ||
|
||
/** | ||
* Defined in includes/config.php | ||
*/ | ||
if (ENABLE_DATABASE == true) { | ||
if (isset($package->user) | ||
&& is_object($package->user) == true | ||
) { | ||
/** | ||
* Insert private chat | ||
*/ | ||
$this->db->insert( | ||
$package->to_user->id, | ||
$package->user->id, | ||
$package->message, | ||
$client->remoteAddress | ||
); | ||
} | ||
} | ||
} | ||
|
||
$targetClient = $user['client']; | ||
$targetClient->send($msg); | ||
return; | ||
$targetClient = $user['client']; | ||
$targetClient->send($msg); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
|
||
|
||
/** | ||
* Defined in includes/config.php | ||
*/ | ||
if (ENABLE_DATABASE == true) { | ||
if (isset($package->user) | ||
and is_object($package->user) == true | ||
) { | ||
/** | ||
* Insert private chat | ||
* Defined in includes/config.php | ||
*/ | ||
$this->db->insert( | ||
$package->to_user->id, | ||
$package->user->id, | ||
$package->message, | ||
$client->remoteAddress | ||
); | ||
if (ENABLE_DATABASE == true) { | ||
if (isset($package->user) | ||
and is_object($package->user) == true | ||
) { | ||
/** | ||
* Insert channel chat | ||
*/ | ||
$this->db->insert( | ||
null, | ||
$package->user->id, | ||
$package->message, | ||
$client->remoteAddress | ||
); | ||
} | ||
} | ||
$client->send($msg); | ||
} | ||
} | ||
$client->send($msg); | ||
} | ||
break; | ||
case 'registration': | ||
$this->users[$from->resourceId] = [ | ||
'user' => $package->user, | ||
'client' => $from | ||
]; | ||
break; | ||
case 'userlist': | ||
$list = []; | ||
foreach ($this->users as $resourceId => $value) { | ||
$list[$resourceId] = $value['user']; | ||
} | ||
$new_package = [ | ||
'users' => $list, | ||
'type' => 'userlist' | ||
]; | ||
$new_package = json_encode($new_package); | ||
$client->send($new_package); | ||
break; | ||
|
||
case 'typing': | ||
if ($from != $client) { | ||
|
||
if (empty($package->user) == false) { | ||
/** | ||
* Find the client to send the message to | ||
*/ | ||
foreach ($this->users as $resourceId => $user) { | ||
if ($resourceId == $from->resourceId) { | ||
continue; | ||
} | ||
break; | ||
case 'registration': | ||
$this->users[$from->resourceId] = [ | ||
'user' => $package->user, | ||
'client' => $from | ||
]; | ||
break; | ||
case 'userlist': | ||
$list = []; | ||
foreach ($this->users as $resourceId => $value) { | ||
$list[] = $value['user']; | ||
} | ||
$new_package = [ | ||
'users' => $list, | ||
'type' => 'userlist' | ||
]; | ||
$new_package = json_encode($new_package); | ||
$client->send($new_package); | ||
break; | ||
|
||
case 'typing': | ||
if ($from != $client) { | ||
if (empty($package->user) == false) { | ||
/** | ||
* Find the client to send the message to | ||
*/ | ||
foreach ($this->users as $resourceId => $user) { | ||
if ($resourceId == $from->resourceId) { | ||
continue; | ||
} | ||
|
||
$new_package = [ | ||
'user' => $package->user, | ||
'type' => 'typing', | ||
'value' => $package->value, | ||
]; | ||
$new_package = [ | ||
'user' => $package->user, | ||
'type' => 'typing', | ||
'value' => $package->value, | ||
]; | ||
|
||
$targetClient = $user['client']; | ||
$targetClient->send($msg); | ||
$targetClient = $user['client']; | ||
$targetClient->send($msg); | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
break; | ||
default: | ||
throw new \Exception('Unexpected value'); | ||
break; | ||
} | ||
} | ||
} | ||
|
@@ -234,7 +234,7 @@ public function onClose(ConnectionInterface $conn): void | |
* The onError callback. Will be called on you guessed it, an error :) | ||
* | ||
* @param ConnectionInterface $conn The unique connection identifier. | ||
* @param Exception $e The raised exception | ||
* @param \Exception $e The raised exception | ||
* | ||
* @return void | ||
*/ | ||
|
Oops, something went wrong.