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 6faa135 commit 975b8f2
Showing 1 changed file with 92 additions and 90 deletions.
182 changes: 92 additions & 90 deletions includes/classes/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,114 +103,116 @@ public function onMessage(ConnectionInterface $from, $msg): void
* build on that later.
*/
switch ($package->type) {
case 'message':
if ($from !== $client) {
if (empty($package->to_user) == false && isset($package->to_user->id) == true) {
case 'message':
if ($from !== $client) {
if (empty($package->to_user) == false
&& isset($package->to_user->id) == true
) {

/**
* Find the client to send the message to
*/
foreach ($this->users as $resourceId => $user) {

/**
* Find the client to send the message to
* Non target users will not see this message
* on their screens.
*/
foreach ($this->users as $resourceId => $user) {
if ($user['user']->id === $package->to_user->id) {

/**
* Non target users will not see this message
* on their screens.
* Defined in includes/config.php
*/
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
);
}
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 {
}
} else {


/**
* Defined in includes/config.php
*/
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
);
}
/**
* Defined in includes/config.php
*/
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[] = $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;
default:
throw new \Exception('Unexpected value');
}
break;
default:
throw new \Exception('Unexpected value');
break;
}
}
Expand Down

0 comments on commit 975b8f2

Please sign in to comment.