PHP Classes

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: Update of server.php
Date: 1 year ago
Size: 1,131 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_PORT,
       
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 */
       
)
    ),
   
WEBSOCKET_SERVER_PORT,
   
WEBSOCKET_SERVER_IP
);

echo
"Server running at ".WEBSOCKET_SERVER_IP.":".WEBSOCKET_SERVER_PORT."\n";

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