Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage connections at 1 second intervals from within the websocket task, close connections etc. #5389

Closed
okoca55 opened this issue Jun 30, 2024 · 2 comments
Labels

Comments

@okoca55
Copy link

okoca55 commented Jun 30, 2024

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.
$server = new Swoole\Websocket\Server('0.0.0.0', 82);

$server->set([
    //'worker_num' => (int) swoole_cpu_num(),
    'task_worker_num' => 3,
    'task_enable_coroutine' => true,
]);

$server->on('Task', function ($server, $TaskInfo) {

    while (true) {
        Swoole\Coroutine::sleep(1);
        echo ">>> task ! \n";

        foreach ($server->connections as $Client) {

            echo ">> - -- connn!! \n";
            $ClientInfo = $server->getClientInfo($Client);

            if ($ClientInfo <> false) {
                echo "--2--\n";
                print_r($ClientInfo);
            }
        }
    }

});

$server->on('start', function ($server) {
    echo "Websocket Server is started at ws://127.0.0.1:9502\n";

    $server->task("");
});

$server->on('open', function ($server, $req) {
    echo "connection open: {$req->fd}\n";
});

$server->on('message', function ($server, $frame) {
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(['hello', 'world']));
});

$server->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();
  1. What did you expect to see?

the last v5.0.0.0-dev was working properly, I compiled it manually in centos 7.

but swoole-cli doesn't work anymore,
ineffective in server->connections task,
if I keep the FDs on the table, then
$server->getClientInfo($fd); -> always return false
so getClientInfo from task is not working,
I am closing the manual connection in task,
this code block is not working for this reason.

Data communication between tasks and processes created from this kind of code is extremely problematic and tedious, examples are very few, frankly, I'm struggling, it makes more sense to use structures like nodejs go, swoole should make good optimization in this regard and cancel the use of tables (because the table automatically reserves space from memory at startup and this space becomes unused, a big problem in microservices)
should improve access to variables, that is, if swoole does these messaging operations properly on its own side, the programmer does not have to deal with their mechanisms. (what I mean here is access to global variables and objects from the task and process, access to global variables and objects from the non-local, non-local function in any unit, write from function to global, etc.)

  1. What version of Swoole are you using (show your php --ri swoole)?

swoole-cli 5.1.3 on centos7

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

Linux F.x 3.10.0-1160.118.1.el7.x86_64 #1 SMP Wed Apr 24 16:01:50 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

@NathanFreeman
Copy link
Member

$server = new Swoole\Websocket\Server('0.0.0.0', 82, SWOOLE_PROCESS);

@okoca55
Copy link
Author

okoca55 commented Jul 2, 2024

$server = new Swoole\Websocket\Server('0.0.0.0', 82, SWOOLE_PROCESS);

Thanks for the reply and help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants