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 63f5f35 commit 7b3f874
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 55 deletions.
7 changes: 7 additions & 0 deletions CHANELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.4.1 Fixed the failing database layer

- 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
- Updated the database import

## 1.4 Helping developers to build from the project

This release will be more about helping developers with useful boiler plate functions. This will assist them
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ in the [includes/config.php](https://github.com/johnnymast/mysql_websocket_chat/
| Flag | Description |
| --- | --- |
| DATABASE_HOST | The database username goes in here. By default this has been set to <code>root</code>. |
| DATABASE_PORT | The database port goes in here. By default this has been set to <code>3306</code>. |
| DATABASE_USERNAME | The database username goes in here. By default this has been set to <code>root</code>.|
| DATABASE_PASSWORD | Enter the password to access the database there. By default this has been set to <code>root</code>.|
| DATABASE_DB | Enter the name of the database here. By default this has been set to <code>socket_chat</code>.|
| ENABLE_DATABASE | This flag will turn using the database on or off by setting its value to <code>true</code> or <code>false</code>.|


***Please note*** if you enable the database make sure you update the credentials as well (see table above). Also if you enable the database make sure you have imported [database.sql](https://github.com/johnnymast/mysql_websocket_chat/blob/master/database.sql) into your database.
***Please note*** if you enable the database make sure you update the credentials as well (see table above). Also, if you enable the database make sure you have imported [database.sql](https://github.com/johnnymast/mysql_websocket_chat/blob/master/database.sql) into your database.


# Step 4: Fire up the WebSocket server
Expand Down Expand Up @@ -127,7 +128,7 @@ Oh and if you've come down this far, you might as well [follow me](https://twitt

MIT License

Copyright (c) 2020 Johnny Mast
Copyright (c) 2021 Johnny Mast

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
62 changes: 29 additions & 33 deletions database.sql
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@

-- phpMyAdmin SQL Dump
-- version 4.4.10
-- http://www.phpmyadmin.net
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Sep 18, 2016 at 03:50 PM
-- Server version: 5.5.42
-- PHP Version: 7.0.0
-- Host: db
-- Gegenereerd op: 27 apr 2021 om 17:26
-- Serverversie: 10.3.28-MariaDB-1:10.3.28+maria~focal
-- PHP-versie: 7.4.16

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `socket_chat`
--
CREATE DATABASE IF NOT EXISTS `socket_chat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `socket_chat`;

-- --------------------------------------------------------

--
-- Table structure for table `chat_interactions`
-- Tabelstructuur voor tabel `chat_interactions`
--

CREATE TABLE `chat_interactions` (
`message_id` int(11) NOT NULL,
`to_id` varchar(255) NOT NULL,
`from_id` varchar(255) NOT NULL,
`message` text NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ip_address` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `chat_interactions` (
`message_id` int(11) NOT NULL AUTO_INCREMENT,
`to_id` varchar(255) DEFAULT NULL,
`from_id` varchar(255) NOT NULL,
`message` text NOT NULL,
`time` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`ip_address` varchar(255) NOT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `chat_interactions`
--
ALTER TABLE `chat_interactions`
ADD PRIMARY KEY (`message_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `chat_interactions`
--
ALTER TABLE `chat_interactions`
MODIFY `message_id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
10 changes: 8 additions & 2 deletions includes/classes/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ public function onMessage(ConnectionInterface $from, $msg): void
if (isset($package->user)
and is_object($package->user) == true
) {
/**
* Insert channel chat
*/
$this->db->insert(
$package->to_user,
$package->to_user->id,
$package->user->id,
$package->message,
$client->remoteAddress
Expand All @@ -152,8 +155,11 @@ public function onMessage(ConnectionInterface $from, $msg): void
if (isset($package->user)
and is_object($package->user) == true
) {
/**
* Insert private chat
*/
$this->db->insert(
$package->to_user,
$package->to_user->id,
$package->user->id,
$package->message,
$client->remoteAddress
Expand Down
38 changes: 21 additions & 17 deletions includes/classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*
* The main configuration file for mysql_websocket_chat
*
* PHP version 7.2
* PHP version 7.2 and up.
*
* @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
*/

/**
Expand All @@ -24,22 +24,27 @@
* @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 Database extends PDO
class Database extends \PDO
{

/**
* Database constructor.
*
* @param string $username The username for the database
* @param string $password The password for the database
* @param string $host The hostname for the database
* @param string $db The database name
* @param string $username The username for the database
* @param string $password The password for the database
* @param string $host The hostname for the database
* @param integer $port The port for the database
* @param string $db The database name
*/
public function __construct($username = '', $password = '', $host = '', $db = '')
{
$dsn = 'mysql:dbname='.$db.';host='.$host;
public function __construct($username = '',
$password = '',
$host = '',
$port = 3306,
$db = ''
) {
$dsn = 'mysql:dbname=' . $db . ';host=' . $host . ':' . $port;
parent::__construct($dsn, $username, $password);
}

Expand Down Expand Up @@ -68,14 +73,13 @@ public function insert(
ip_address = :ip_address"
);


$statement->execute(
[
'to_id' => $to_id,
'from_id' => $from_id,
'message' => $message,
'ip_address' => $ip_address
'to_id' => $to_id,
'from_id' => $from_id,
'message' => $message,
'ip_address' => $ip_address
]
);
}
}
}
2 changes: 2 additions & 0 deletions includes/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
date_default_timezone_set('EUROPE/AMSTERDAM');

define('DATABASE_HOST', 'localhost');
define('DATABASE_PORT', 3306);
define('DATABASE_USERNAME', 'root');
define('DATABASE_PASSWORD', 'root');
define('DATABASE_DB', 'socket_chat');
define('ENABLE_DATABASE', false);


/**
* The host can either be an IP or a hostname
* on this machine. The port is just the port
Expand Down
2 changes: 1 addition & 1 deletion server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;


/**
* Create a new connection to
* the database that we can inject
Expand All @@ -20,6 +19,7 @@
DATABASE_USERNAME,
DATABASE_PASSWORD,
DATABASE_HOST,
DATABASE_PORT,
DATABASE_DB
);
} else {
Expand Down

0 comments on commit 7b3f874

Please sign in to comment.