如何在process中创建TCP服务

lshfong
<?php

namespace process;

use Workerman\Worker;

class WorkermanTcpService
{
    public function onWorkerStart()
    {
        // 创建一个 TCP Worker 监听指定端口
        $tcp_worker = new Worker('tcp://0.0.0.0:2345');
        $tcp_worker->onWorkerStart = function (Worker $worker) {
            echo "Worker {$worker->id} starting...\n";
        };
        // 当有客户端连接时触发
        $tcp_worker->onConnect = function ($connection) {
            echo "New connection\n";
        };

        // 当接收到数据时触发
        $tcp_worker->onMessage = function ($connection, $data) {
            // 处理接收到的数据
            $connection->send('Received: ' . $data);
        };

        // 当连接关闭时触发
        $tcp_worker->onClose = function ($connection) {
            echo "Connection closed\n";
        };

        Worker::runAll();
    }
}

启动后一直狂闪,

284 1 0
1个回答

walkor 打赏
  • lshfong 2024-09-19

    我是在webman的process中创建应该怎么做呢

  • Gin 2024-09-20

    狂闪什么 贴出来信息

×
🔝