Skip to content

Commit 7b3f874

Browse files
committed
Fixed a bug enabling the database. This fixes issue #54 thanks to @badguyp.
1 parent 63f5f35 commit 7b3f874

File tree

7 files changed

+71
-55
lines changed

7 files changed

+71
-55
lines changed

CHANELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.4.1 Fixed the failing database layer
2+
3+
- Enabling database should now work fixing #54 thanks to @badguyp.
4+
- Fixed a bug in includes/classes/Database.php
5+
- Fixed a bug in includes/classes/Chat.php
6+
- Updated the database import
7+
18
## 1.4 Helping developers to build from the project
29

310
This release will be more about helping developers with useful boiler plate functions. This will assist them

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ in the [includes/config.php](https://github.com/johnnymast/mysql_websocket_chat/
6363
| Flag | Description |
6464
| --- | --- |
6565
| DATABASE_HOST | The database username goes in here. By default this has been set to <code>root</code>. |
66+
| DATABASE_PORT | The database port goes in here. By default this has been set to <code>3306</code>. |
6667
| DATABASE_USERNAME | The database username goes in here. By default this has been set to <code>root</code>.|
6768
| DATABASE_PASSWORD | Enter the password to access the database there. By default this has been set to <code>root</code>.|
6869
| DATABASE_DB | Enter the name of the database here. By default this has been set to <code>socket_chat</code>.|
6970
| ENABLE_DATABASE | This flag will turn using the database on or off by setting its value to <code>true</code> or <code>false</code>.|
7071

7172

72-
***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.
73+
***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.
7374

7475

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

128129
MIT License
129130

130-
Copyright (c) 2020 Johnny Mast
131+
Copyright (c) 2021 Johnny Mast
131132

132133
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:
133134

database.sql

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1+
12
-- phpMyAdmin SQL Dump
2-
-- version 4.4.10
3-
-- http://www.phpmyadmin.net
3+
-- version 5.1.0
4+
-- https://www.phpmyadmin.net/
45
--
5-
-- Host: localhost:3306
6-
-- Generation Time: Sep 18, 2016 at 03:50 PM
7-
-- Server version: 5.5.42
8-
-- PHP Version: 7.0.0
6+
-- Host: db
7+
-- Gegenereerd op: 27 apr 2021 om 17:26
8+
-- Serverversie: 10.3.28-MariaDB-1:10.3.28+maria~focal
9+
-- PHP-versie: 7.4.16
910

1011
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
12+
START TRANSACTION;
1113
SET time_zone = "+00:00";
1214

15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
1321
--
1422
-- Database: `socket_chat`
1523
--
24+
CREATE DATABASE IF NOT EXISTS `socket_chat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
25+
USE `socket_chat`;
1626

1727
-- --------------------------------------------------------
1828

1929
--
20-
-- Table structure for table `chat_interactions`
30+
-- Tabelstructuur voor tabel `chat_interactions`
2131
--
2232

23-
CREATE TABLE `chat_interactions` (
24-
`message_id` int(11) NOT NULL,
25-
`to_id` varchar(255) NOT NULL,
26-
`from_id` varchar(255) NOT NULL,
27-
`message` text NOT NULL,
28-
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
29-
`ip_address` varchar(255) NOT NULL
30-
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
33+
CREATE TABLE IF NOT EXISTS `chat_interactions` (
34+
`message_id` int(11) NOT NULL AUTO_INCREMENT,
35+
`to_id` varchar(255) DEFAULT NULL,
36+
`from_id` varchar(255) NOT NULL,
37+
`message` text NOT NULL,
38+
`time` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
39+
`ip_address` varchar(255) NOT NULL,
40+
PRIMARY KEY (`message_id`)
41+
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
3142

32-
--
33-
-- Indexes for dumped tables
34-
--
3543

36-
--
37-
-- Indexes for table `chat_interactions`
38-
--
39-
ALTER TABLE `chat_interactions`
40-
ADD PRIMARY KEY (`message_id`);
41-
42-
--
43-
-- AUTO_INCREMENT for dumped tables
44-
--
45-
46-
--
47-
-- AUTO_INCREMENT for table `chat_interactions`
48-
--
49-
ALTER TABLE `chat_interactions`
50-
MODIFY `message_id` int(11) NOT NULL AUTO_INCREMENT;
44+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
45+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
46+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

includes/classes/Chat.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ public function onMessage(ConnectionInterface $from, $msg): void
128128
if (isset($package->user)
129129
and is_object($package->user) == true
130130
) {
131+
/**
132+
* Insert channel chat
133+
*/
131134
$this->db->insert(
132-
$package->to_user,
135+
$package->to_user->id,
133136
$package->user->id,
134137
$package->message,
135138
$client->remoteAddress
@@ -152,8 +155,11 @@ public function onMessage(ConnectionInterface $from, $msg): void
152155
if (isset($package->user)
153156
and is_object($package->user) == true
154157
) {
158+
/**
159+
* Insert private chat
160+
*/
155161
$this->db->insert(
156-
$package->to_user,
162+
$package->to_user->id,
157163
$package->user->id,
158164
$package->message,
159165
$client->remoteAddress

includes/classes/Database.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
*
55
* The main configuration file for mysql_websocket_chat
66
*
7-
* PHP version 7.2
7+
* PHP version 7.2 and up.
88
*
99
* @category Configuration
1010
* @package Mysql_Websocket_Chat
1111
* @author Johnny Mast <mastjohnny@gmail.com>
1212
* @license https://opensource.org/licenses/MIT MIT
1313
* @link https://github.com/johnnymast/mysql_websocket_chat
14-
* @since GIT:1.0
14+
* @since 1.0
1515
*/
1616

1717
/**
@@ -24,22 +24,27 @@
2424
* @author Johnny Mast <mastjohnny@gmail.com>
2525
* @license https://opensource.org/licenses/MIT MIT
2626
* @link https://github.com/johnnymast/mysql_websocket_chat
27-
* @since GIT:1.0
27+
* @since 1.0
2828
*/
29-
class Database extends PDO
29+
class Database extends \PDO
3030
{
3131

3232
/**
3333
* Database constructor.
3434
*
35-
* @param string $username The username for the database
36-
* @param string $password The password for the database
37-
* @param string $host The hostname for the database
38-
* @param string $db The database name
35+
* @param string $username The username for the database
36+
* @param string $password The password for the database
37+
* @param string $host The hostname for the database
38+
* @param integer $port The port for the database
39+
* @param string $db The database name
3940
*/
40-
public function __construct($username = '', $password = '', $host = '', $db = '')
41-
{
42-
$dsn = 'mysql:dbname='.$db.';host='.$host;
41+
public function __construct($username = '',
42+
$password = '',
43+
$host = '',
44+
$port = 3306,
45+
$db = ''
46+
) {
47+
$dsn = 'mysql:dbname=' . $db . ';host=' . $host . ':' . $port;
4348
parent::__construct($dsn, $username, $password);
4449
}
4550

@@ -68,14 +73,13 @@ public function insert(
6873
ip_address = :ip_address"
6974
);
7075

71-
7276
$statement->execute(
7377
[
74-
'to_id' => $to_id,
75-
'from_id' => $from_id,
76-
'message' => $message,
77-
'ip_address' => $ip_address
78+
'to_id' => $to_id,
79+
'from_id' => $from_id,
80+
'message' => $message,
81+
'ip_address' => $ip_address
7882
]
7983
);
8084
}
81-
}
85+
}

includes/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
date_default_timezone_set('EUROPE/AMSTERDAM');
1919

2020
define('DATABASE_HOST', 'localhost');
21+
define('DATABASE_PORT', 3306);
2122
define('DATABASE_USERNAME', 'root');
2223
define('DATABASE_PASSWORD', 'root');
2324
define('DATABASE_DB', 'socket_chat');
2425
define('ENABLE_DATABASE', false);
2526

27+
2628
/**
2729
* The host can either be an IP or a hostname
2830
* on this machine. The port is just the port

server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Ratchet\Http\HttpServer;
99
use Ratchet\WebSocket\WsServer;
1010

11-
1211
/**
1312
* Create a new connection to
1413
* the database that we can inject
@@ -20,6 +19,7 @@
2019
DATABASE_USERNAME,
2120
DATABASE_PASSWORD,
2221
DATABASE_HOST,
22+
DATABASE_PORT,
2323
DATABASE_DB
2424
);
2525
} else {

0 commit comments

Comments
 (0)