Files
Testing/mongolian-og/source/server/bin/chat-server.php
sean 0e8a8b3028
Some checks are pending
deploy-testing / deploy (push) Waiting to run
Initial testing snapshot
2026-11-17 16:15:01 -05:00

230 lines
5.9 KiB
PHP

<?php
/**
* @author Magnoliyan
* @version 1.17.0
*
*/
$version = array(
'main' => '1.18.0',
'build' => '$LastChangedRevision: 282 $'
);
use Ratchet\Server\IpBlackList;
use Ratchet\Http\HttpServer;
use Ratchet\Http\OriginCheck;
use Ratchet\Http\HttpServerInterface;
use Ratchet\WebSocket\WsServer;
use MgRTC\Chat;
use MgRTC\Daemon;
use MgRTC\Session\SessionProvider;
use MgRTC\Router;
$autoLoad = dirname(__DIR__) . '/vendor/autoload.php';
if(!file_exists($autoLoad)){
echo "\nThe system is not properly installed.\nPlease run [composer install] from [server] folder.\n";
exit(1);
}
require $autoLoad;
require __DIR__ . '/config.php';
/**
* Create WS server
*
* @global Ratchet\Server\IoServer $server
* @global array $config
*/
$cwd=getcwd();
chdir("/home/httpd/html/mojo.bateworld.com/public_html/");
include_once("wssheader.php");
chdir($cwd);
//$database->database_query("DELETE FROM bateworldcom_zips.chatlist");
function createServer() {
global $server, $config;
// TURN setup
$turnConfig = $config['turn'] ?? [];
$turnAdapterClass = $turnConfig['adapter'] ?? '\MgRTC\Turn\TurnApi';
$turn = new $turnAdapterClass($turnConfig);
$app = new Chat($config, $turn);
// IP blacklist
if (!empty($config['IpBlackList'])) {
$app = new IpBlackList($app);
foreach ($config['IpBlackList'] as $ip) {
$app->blockAddress($ip);
}
}
// Session
$session = new SessionProvider($app, $config);
// WebSocket
$wsServer = new WsServer($session);
// Origin check
if (!empty($config['allowedOrigins'])) {
$wsServer = new OriginCheck($wsServer, $config['allowedOrigins']);
}
// Router
$router = getRouter($wsServer, $app);
// NEW: ReactPHP 1.x loop
$loop = React\EventLoop\Factory::create();
// NEW: ReactPHP 1.x socket server
$socket = new React\Socket\SocketServer(
"0.0.0.0:{$config['port']}",
[],
$loop
);
// NEW: Ratchet 0.5 IoServer
$server = new Ratchet\Server\IoServer(
new HttpServer($router),
$socket,
$loop
);
// Heartbeat
$loop->addPeriodicTimer(30, function() use ($app) {
if(is_array($app->getClients)){
foreach ($app->getClients as $connId => $client) {
if (method_exists($client['conn'], 'ping')) {
$client['conn']->ping();
} else {
$client['conn']->send(json_encode(['type' => 'ping']));
}
}
}
});
}
/*
function createServer(){
global $server, $config;
//turn setup
$turnConfig = isset($config['turn'])? $config['turn'] : [];
$turnAdapterClass = isset($turnConfig['adapter'])? $turnConfig['adapter']:'\MgRTC\Turn\TurnApi';
$turn = new $turnAdapterClass($turnConfig);
$app = new Chat($config, $turn);
//ip blacklist
if(isset ($config['IpBlackList']) && is_array($config['IpBlackList'])){
$app = new IpBlackList($app);
foreach ($config['IpBlackList'] as $ipBlackList) {
$app->blockAddress($ipBlackList);
}
}
//session
$session = new SessionProvider($app, $config);
//websocket
$wsServer = new WsServer($session);
//limit origins
if(isset ($config['allowedOrigins']) && is_array($config['allowedOrigins'])){
$wsServer = new OriginCheck($wsServer, $config['allowedOrigins']);
}
//router
$router = getRouter($wsServer, $app);
try {
$server = MgRTC\Server::factory(new HttpServer($router), $config['port']);
} catch (\Exception $exc) {
echo sprintf("\nServer was not able to start with error message [%s]\n"
. "\nMost likely the port [%d] you are trying to run the system is already taken by some other service.\n"
. "Please find available free port and configure it in [config.php] and restart.\n\n", $exc->getMessage(), $config['port']);
exit(1);
}
// Assuming your factory exposes the loop
$loop = $server->loop; // or however MgRTC exposes it
$loop->addPeriodicTimer(30, function() use ($app) {
foreach ($app->getClients as $connId => $client) {
if (method_exists($client['conn'], 'ping')) {
$client['conn']->ping(); // protocol ping frame
} else {
$client['conn']->send(json_encode(['type' => 'ping']));
}
}
});
}*/
/**
*
* @global array $config
* @param HttpServerInterface $wsServer
* @param Chat $app
* @return HttpServerInterface
*/
function getRouter(HttpServerInterface $wsServer, Chat $app)
{
global $config;
//no routes defined
if(!isset($config['routes']) || !is_array($config['routes']) || !count($config['routes'])){
return $wsServer;
}
//create router and setup routes
$router = new Router();
$router->route('/', $wsServer);
foreach ($config['routes'] as $route) {
echo "\nadding route [{$route['path']}] for server class [{$route['server_class']}]\n";
$router->route($route['path'], new $route['server_class']($app));
}
return $router;
}
/**
* Start server
*
* @global Ratchet\Server\IoServer $server
*/
function onRun(){
global $server, $config, $version;
preg_match('/\d+/', $version['build'], $match);
$build = $match[0];
createServer();
echo sprintf("\nServer version: %s.%s, listening on port: %d\n", $version['main'], $build, $config['port']);
$server->run();
}
/**
* Stop server
*
* @global Ratchet\Server\IoServer $server
*/
function onStop(){
global $server;
try {
$server->socket->shutdown();
}catch (Exception $exc) {
//echo $exc->getMessage();
}
$server->loop->stop();
}
//get command
$command = isset ($argv[1])? $argv[1] : 'execute';
//Deamon igniter
Daemon::getInstance()->run(array(
'daemon_name' => 'mg-chat-server',
'run' => 'onRun',
'stop' => 'onStop',
'pid_file' => __DIR__ . '/mg-chat-server.pid'
),$command);