Workerman + ThinkPHP 8 结合使用

安装

bash 复制代码
composer require workerman/workerman

创建命令

text 复制代码
php think make:command 命令文件名 命令名

实例一:秒级定时任务

文件位置

text 复制代码
app/command/TimerTest.php

代码

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use Workerman\Worker;
use Workerman\Timer;
use app\common\model\Order;

class TimerTest extends Command
{
    protected function configure()
    {
        $this->setName('timer');
    }

    protected function execute(Input $input, Output $output)
    {
        $worker = new Worker();
        $worker->onWorkerStart = function() {
            // 每秒任务
            Timer::add(1, function() {
                $count = Order::where('status', 0)
                    ->where('create_time', '<', time() - 1800)
                    ->update(['status' => -1]);
                if ($count) echo "自动关闭 {$count} 个超时订单\n";
            });

            // 每5秒任务
            Timer::add(5, function() {
                echo "5秒任务执行: " . date('Y-m-d H:i:s') . "\n";
            });

            // 每60秒任务
            Timer::add(60, function() {
                cache('visit_count', 0);
                echo "已重置访问计数\n";
            });
        };

        Worker::runAll();
    }
}

配置 config/console.php

php 复制代码
<?php
return [
    'commands' => [
        'timer' => 'app\command\TimerTest',
    ],
];

启动

bash 复制代码
# Linux 后台运行
php think timer start -d

# Windows 开发
php think timer start

实例二:WebSocket 实时通信

文件位置

shell 复制代码
app/command/WebSocket.php

代码

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use Workerman\Worker;

class WebSocket extends Command
{
    protected function configure()
    {
        $this->setName('websocket');
    }

    protected function execute(Input $input, Output $output)
    {
        $ws = new Worker('websocket://0.0.0.0:8282');
        $ws->count = 4;

        $ws->onMessage = function($connection, $data) {
            // 广播消息
            foreach(Worker::$connections as $conn) {
                $conn->send($data);
            }
        };

        Worker::runAll();
    }
}

配置 config/console.php

php 复制代码
<?php
return [
    'commands' => [
        'websocket' => 'app\command\WebSocket',
    ],
];

启动

bash 复制代码
php think websocket start -d

实例三:TP8 接口中触发 WebSocket 推送(内部 Text 端口)

设计思路

WebSocket 服务运行在独立进程中,TP8 HTTP 接口无法直接调用。解决方案:在同一进程内再监听一个内部端口(8281),接口通过 TCP 短连接发送消息。

text 复制代码
TP8 接口 → TCP 8281 (Text协议: 消息+换行符) → WebSocket 8282 → 客户端

文件位置

text 复制代码
app/command/PushServer.php

代码

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use Workerman\Worker;

class PushServer extends Command
{
    protected function configure()
    {
        $this->setName('push-server');
    }

    protected function execute(Input $input, Output $output)
    {
        // WebSocket 服务(对外,8282端口)
        $ws = new Worker('websocket://0.0.0.0:8282');
        $ws->count = 4;

        $ws->onMessage = function($connection, $data) {
            foreach(Worker::$connections as $conn) {
                $conn->send($data);
            }
        };

        // 内部推送服务(对内,8281端口,Text协议)
        $internal = new Worker('text://0.0.0.0:8281');

        $internal->onMessage = function($connection, $data) {
            // 收到内部推送消息,广播给所有 WebSocket 客户端
            foreach(Worker::$connections as $conn) {
                $conn->send($data);
            }

            // 回应客户端
            $connection->send("OK\n");
        };

        Worker::runAll();
    }
}

配置 config/console.php

php 复制代码
<?php
return [
    'commands' => [
        'push-server' => 'app\command\PushServer',
    ],
];

TP8 接口中推送

php 复制代码
<?php
namespace app\api\controller;

use think\Controller;
use app\common\model\Message;

class Api extends Controller
{
    public function send()
    {
        $content = $this->request->post('content');

        // 连接到内部推送端口
        $socket = stream_socket_client('tcp://127.0.0.1:8281',$errno, $errmsg, 3);

        if ($socket) {
            // 发送消息(换行符结尾表示一条完整消息)
            fwrite($socket, $content . "\n");

            // 读取回应(可选)
            $response = fgets($socket);
            fclose($socket);

            return json(['code' => 0, 'msg' => '发送成功']);
        }

        return json(['code' => 1, 'msg' => '推送失败: ' . $errstr]);
    }
}

启动

bash 复制代码
php think push-server start -d

命令速查

bash 复制代码
# 开发
php think timer start

# 生产
php think timer start -d

# 停止
php think timer stop

# 重启
php think timer restart

# 平滑重启(不断连)
php think timer reload
相关推荐
niucloud-admin1 天前
PHP V6 单商户常见问题——如何修改访问域名默认跳转端口
php
catchadmin1 天前
使用 PHP TrueAsync 改造 Laravel 协程异步化的可行路径
开发语言·php·laravel
郑州光合科技余经理1 天前
同城O2O海外版二次开发实战:从支付网关到配送算法
开发语言·前端·后端·算法·架构·uni-app·php
niucloud-admin1 天前
PHP V6 单商户常见问题——在线升级版本失败后如何回退版本数据
php
0xR3lativ1ty1 天前
关闭公网IP的两种方式
网络协议·tcp/ip·php
白晨并不是很能熬夜1 天前
【PRC】第 2 篇:Netty 通信层 — NIO 模型 + 自定义协议 + 心跳
java·开发语言·后端·面试·rpc·php·nio
2401_873479401 天前
固件升级如何按地区分批推送?IP地址查询定位决定升级策略
网络协议·tcp/ip·php
阿桂有点桂1 天前
Laravel队列,使用redis驱动器
php·laravel
淘矿人2 天前
2026年4月-DeepSeek V4 vs GPT-5.5深度对比测评:weelinking一键切换实测
服务器·数据库·人工智能·python·gpt·学习·php
森总20202 天前
如何优雅处理 DB 事务提交后的不可控后置逻辑?记一次订单流程的架构重构
php