'Сodeigniter4 giving MySQL server gone away error on WS

I used CodeIgniter4 latest version and Websokets on Workerman (Project for websockets). But later 4 hour i have problem. MySQL gone away error. Code example:

<?php

//BASE CLASS FOR ALL MODELS
//namespace App\Models;

//use Exception;

//class CodeIgniterDataBaseModel
//{
//    public $dataBase = null;
//
//    public function __construct(string $nameDataBase)
//    {
//        $this->SetDataBase($nameDataBase);    
//    }


//
//    public function SetDataBase(string $nameDataBase)
//    {
//        $this->dataBase = db_connect($nameDataBase);
//    }

//    public function Query(string $sql, array $array = [])
//    {
//        $result = [];
//        try
//        {
//            $query = $this->dataBase->query($sql, $array);
//        }
//        catch(Exception $e)
//        {
//            $this->Reconnect();
//            $query = $this->dataBase->query($sql, $array);
//        }
//        $this->PrepareQueryResult($query,$result);
//        return $result;
//    }

//    public function IsConnected()
//    {
//        return !empty( $this->dataBase->Query("SELECT VERSION();") );
//    }

//    public function Reconnect()
//    {
//        $this->dataBase->reconnect();
//    }

//    public function Close()
//    {
//        $this->dataBase->close();
//    }   
//};

namespace App\Services\BoostServer;
require_once __DIR__.'/../framework.php';
require_once __DIR__.'/../../Libraries/workerman/Worker.php';
require_once __DIR__.'/../configLoader.php';

use App\Models\SessionModel;
use App\Models\RoomModel;
use Workerman\Worker;
use Exception;

//...
$webSocket = new Worker( "websocket://localhost:9090, $options);


$webSocket->onWorkerStart = function($task)
{
    SessionModel::SetDataBase('default');
    RoomModel::SetDataBase('default');
};

$webSocket->onConnect = function ($connection)
{
    $connection->onWebSocketConnect = function($connection)
    {
          //NOT HELPING - I CATCH ERROR MySQL GONE AWAY
        try
        {
            if(
                SessionModel::$dataBase->IsConnected() == false ||
                RoomModel::$dataBase->IsConnected() == false
            )
            {
                SessionModel::SetDataBaseNS('default');
                RoomModel::SetDataBaseNS('default');
            }
        }
        catch(Exception $error)
        {
            SessionModel::SetDataBaseNS('default');
            RoomModel::SetDataBaseNS('default');
        }
          
          // Mysql Requests
        //...
    };
};

$webSocket->onMessage = function ($connection, $message){};


$webSocket->onClose = function ($connection){}

Worker::runAll();
$server->run();

?>

I know mysql server make timeout but on CI4 cant working reconncet and setting database. I run this application on console "php app/Services/BostServer/BoostServer.php start". Later 4 hours any request giving "MySQL server gone away" and request results is null. Reconnect and reset db can't giving result (check my code). How fix this problem? P.S sry my english not good



Solution 1:[1]

Try it without the port number (9090 in your case)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sami