ThinkPHP6.2+Workerman实战:构建高并发WebSocket即时通讯系统

2025-07-19 0 358

ThinkPHP6.2+Workerman实战:构建高并发WebSocket即时通讯系统

一、技术方案对比

传统轮询方案

  • HTTP短连接请求
  • 3-5秒延迟
  • 服务器压力大
  • 资源消耗高

WebSocket方案

  • 长连接双向通信
  • 毫秒级延迟
  • 低服务器负载
  • 支持二进制传输

二、核心系统实现

1. Workerman服务集成

// config/websocket.php
return [
    'socket' => 'websocket://0.0.0.0:2346',
    'context' => [
        'ssl' => [
            'local_cert'  => '/path/to/ssl.pem',
            'local_pk'    => '/path/to/ssl.key'
        ]
    ],
    'process' => 4  // 根据CPU核心数设置
];

// 自定义服务类
class SocketService
{
    public function __construct() {
        $this->worker = new Worker(config('websocket.socket'));
        $this->worker->count = config('websocket.process');
        
        $this->worker->onConnect = [$this, 'onConnect'];
        $this->worker->onMessage = [$this, 'onMessage'];
        $this->worker->onClose = [$this, 'onClose'];
    }
    
    public function onConnect($connection) {
        // 身份验证逻辑
    }
    
    public function onMessage($connection, $data) {
        // 消息处理逻辑
    }
    
    public function onClose($connection) {
        // 连接关闭处理
    }
}

2. 消息广播机制

class MessageBroadcast
{
    protected static $connections = [];
    
    public static function addConnection($userId, $connection) {
        self::$connections[$userId] = $connection;
    }
    
    public static function sendToUser($userId, $message) {
        if(isset(self::$connections[$userId])) {
            self::$connections[$userId]->send($message);
        }
    }
    
    public static function broadcast($message, $exclude = []) {
        foreach(self::$connections as $uid => $conn) {
            if(!in_array($uid, $exclude)) {
                $conn->send($message);
            }
        }
    }
}

3. ThinkPHP业务集成

// 控制器处理Web业务
class ChatController
{
    public function send() {
        $userId = request()->userId;
        $targetId = input('target_id');
        $content = input('content');
        
        // 存储消息到数据库
        $message = MessageModel::create([
            'from_id' => $userId,
            'to_id' => $targetId,
            'content' => $content
        ]);
        
        // 通过HTTP接口推送消息到WS服务
        $client = new GuzzleHttpClient();
        $client->post('http://127.0.0.1:2346/push', [
            'json' => [
                'action' => 'send',
                'target' => $targetId,
                'data' => $message
            ]
        ]);
        
        return json(['code' => 200]);
    }
}

三、高级功能实现

1. 消息已读回执

// WebSocket消息处理
public function onMessage($connection, $data) {
    $data = json_decode($data, true);
    
    if($data['type'] == 'read_receipt') {
        MessageModel::where('id', $data['msg_id'])
            ->update(['is_read' => 1]);
    }
}

// 前端发送已读回执
socket.send(JSON.stringify({
    type: 'read_receipt',
    msg_id: '123456'
}));

2. 在线状态管理

class OnlineStatus
{
    public static function update($userId, $status) {
        cache('user_online_' . $userId, $status, 3600);
        
        // 通知好友列表
        $friends = FriendModel::where('user_id', $userId)
            ->column('friend_id');
        
        foreach($friends as $friendId) {
            MessageBroadcast::sendToUser($friendId, json_encode([
                'type' => 'status_change',
                'user_id' => $userId,
                'status' => $status
            ]));
        }
    }
}

四、性能优化方案

  • 连接复用:保持长连接减少握手开销
  • 消息压缩:对文本消息进行Gzip压缩
  • 心跳检测:定时Ping/Pong保持连接
  • 集群部署:使用Redis发布订阅跨进程通信

性能测试数据

测试环境:4核8G云服务器
并发连接数:15,000
消息吞吐量:28,000条/秒
平均延迟:35ms
内存占用:1.2GB
ThinkPHP6.2+Workerman实战:构建高并发WebSocket即时通讯系统
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

淘吗网 thinkphp ThinkPHP6.2+Workerman实战:构建高并发WebSocket即时通讯系统 https://www.taomawang.com/server/thinkphp/518.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务