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

协程风格的TCP服务器问题,如何顺序处理事件 #5211

Closed
jinuary opened this issue Dec 7, 2023 · 2 comments
Closed

协程风格的TCP服务器问题,如何顺序处理事件 #5211

jinuary opened this issue Dec 7, 2023 · 2 comments
Labels

Comments

@jinuary
Copy link

jinuary commented Dec 7, 2023

Please answer these questions before submitting your issue.

协程风格的TCP服务器,如何顺序处理 Connect、Receive、Close 事件。如何像正常的tcp服务那样,分步处理start、receive、close事件
还有官方文档提供的完整示例有问题,找一个第三方socket客户端去连接直接就关闭了,提示errCode: 110, errMsg: Connection timed out,data==='' 和data===false 分别代表什么意思

  1. 来源官方文档:https://wiki.swoole.com/#/coroutine/server
use Swoole\Process;
use Swoole\Coroutine;
use Swoole\Coroutine\Server\Connection;

//多进程管理模块
$pool = new Process\Pool(2);
//让每个OnWorkerStart回调都自动创建一个协程
$pool->set(['enable_coroutine' => true]);
$pool->on('workerStart', function ($pool, $id) {
    //每个进程都监听9501端口
    $server = new Swoole\Coroutine\Server('127.0.0.1', 9501, false, true);

    //收到15信号关闭服务
    Process::signal(SIGTERM, function () use ($server) {
        $server->shutdown();
    });

    //接收到新的连接请求 并自动创建一个协程
    $server->handle(function (Connection $conn) {
        while (true) {
            //接收数据
            $data = $conn->recv(1);

            if ($data === '' || $data === false) {
                $errCode = swoole_last_error();
                $errMsg = socket_strerror($errCode);
                echo "errCode: {$errCode}, errMsg: {$errMsg}\n";
                $conn->close();
                break;
            }

            //发送数据
            $conn->send('hello');

            Coroutine::sleep(1);
        }
    });

    //开始监听端口
    $server->start();
});
$pool->start();

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.0.0
Built => Aug 29 2022 10:45:14
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
http2 => enabled
json => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

@matyhtf
Copy link
Member

matyhtf commented Dec 8, 2023

仔细看下文档吧,上面代码不是有顺序的吗,空字符串表示关闭,false表示错误但需检查错误码

@jinuary
Copy link
Author

jinuary commented Dec 13, 2023

仔细看下文档吧,上面代码不是有顺序的吗,空字符串表示关闭,false表示错误但需检查错误码

上面代码空和false哪有顺序啊,空字符串表示关闭这个没错,创建连接事件就是无法捕获。

image

我参考官网写的代码,客户端连接后发送数据。有得顺序是13,有得是31。13顺序下错误码是110,110代表连接超时,这错误码也不对啊

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

2 participants