Skip to content

Commit

Permalink
Fixed a bug enabling the database. This fixes issue #54 thanks to @ba…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymast committed May 19, 2021
1 parent 7b3f874 commit 6faa135
Show file tree
Hide file tree
Showing 5 changed files with 11,686 additions and 144 deletions.
1 change: 1 addition & 0 deletions CHANELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.4.1 Fixed the failing database layer

- Manually reviewed the database changes.
- Enabling database should now work fixing #54 thanks to @badguyp.
- Fixed a bug in includes/classes/Database.php
- Fixed a bug in includes/classes/Chat.php
Expand Down
196 changes: 98 additions & 98 deletions includes/classes/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mastjohnny@gmail.com>
* @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;

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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
*/
Expand Down
Loading

0 comments on commit 6faa135

Please sign in to comment.