PHP Classes
elePHPant
Icontem

File: server.php

Recommend this page to a friend!
  Classes of Johnny Mast  >  PHP MySQL Websocket Chat  >  server.php  >  Download  
File: server.php
Role: Application script
Content type: text/plain
Description: Application script
Class: PHP MySQL Websocket Chat
Websocket chat that stores messages in MySQL
Author: By
Last change: #2 Cleaned up the new php code in the server.php file.
mysql_websocket_chat-2 Chat does not work on windows 10. I have changed deprecated CHAT_SERVER_IP from now on forward we will use CHAT_SERVER_HOST because this is more flexible.
Date: 2 years ago
Size: 1,093 bytes
 

 

Contents

Class file image Download
<?php
require 'vendor/autoload.php';
require
'includes/config.php';
require
'includes/classes/Database.php';
require
'includes/classes/Chat.php';

use
Ratchet\Server\IoServer;
use
Ratchet\Http\HttpServer;
use
Ratchet\WebSocket\WsServer;


/**
 * Create a new connection to
 * the database that we can inject
 * to our chat class later on in
 * the code.
 */
if (ENABLE_DATABASE == true) {
   
$db = new Database(
       
DATABASE_USERNAME,
       
DATABASE_PASSWORD,
       
DATABASE_HOST,
       
DATABASE_DB
   
);
} else {
   
$db = null;
}

/**
 * Instantiate the chat server
 * on the configured port in
 * includes/config.php.
 *
 * The includes/classes/Chat.php class will
 * handle all the events and database interactions.
 */
$server = IoServer::factory(
    new
HttpServer(
        new
WsServer(
            new
Chat($db) /* This class will handle the chats. It is located in includes/classes/Chat.php */
       
)
    ),
   
CHAT_SERVER_PORT,
   
CHAT_SERVER_HOST
);

echo
"Server running at ".CHAT_SERVER_HOST.":".CHAT_SERVER_PORT."\n";

/**
 * Run the server
 */
$server->run();