ThinkPHP5.1 Workman如何调用写好的主动推送函数啊(重点是如何获取worker对象)

asdasdsdf

问题描述

ThinkPHP5.1 Workman如何调用写好的主动推送函数啊(重点是如何获取worker对象)

为此你搜索到了哪些方案及不适用的原因

$ChatDom = new Chat();
$ChatDom->ActivePrompt(【ThinkPHP5.1框架 这里填什么】,["mid" => 1327, "msg" => "注册成功", "url" => ""]);
<?php

namespace app\api\controller;

use think\worker\Server;

use app\api\middleware\ApiAuth;

use app\api\model\Notifylog;
use app\api\model\Member;

// BaseClass
use Workerman\Worker;
use Workerman\Crontab\Crontab;
use Workerman\Connection\TcpConnection;

class Chat extends Server
{

    // 消息格式 msg = ["code": 0=成功1=失败2=离线, "msg": "", "data": []];

    public $socket = 'websocket://0.0.0.0:2348';

    public function onConnect(TcpConnection $connection){
        // IP端口
        $Client = "{$connection->getRemoteIp()}:{$connection->getRemotePort()}\r\n";
        echo $Client;
    }

    public function onWebSocketConnect($connection, $http_buffer){
        // 账户检测
        $HeaderArr = $this->parseHeaders($http_buffer);
        $this->mark_($connection, $HeaderArr);
    }

    public function onMessage(TcpConnection $connection, $data){

    }

    public function onClose($connection){
        // 设置账户消息的通信ID
        Member::where(['notifyid' => $connection->id])->update(['notifyid' => -1]);
        $connection->close($this->Magstruct(2, "断开完毕"));
        echo $connection->id."已断开\n";
    }

    // 主动提醒, 如果有了信息消息。这个函数将 写入消息数据 并调用通知客户
    public function ActivePrompt($connection, $Arr){
        $query = new Notifylog();
        $query->notifylog_mid = $Arr['mid'];
        $query->notifylog_time = time();
        $query->notifylog_url = $Arr['url'];
        $query->notifylog_msg = $Arr['msg'];
        if($query->save()){
            $NotifyID = Member::where(["id" => $Arr['mid']])->value("notifyid");
            $connection->worker->connection[$NotifyID]->send($this->Magstruct(0, "New Message"));
            return;
        }
        print_r("ActivePrompt: MessAge WriteError");
    }

    //==========================================================================
    // 连接并构建
    public function mark_($connection, $Data){
        print_r($Data);
        $Data['token'] = "e1cf1ab4313f1d2b7e03a0df3958f8ce";
        $result = ApiAuth::checkToken($Data['token']);
        if(empty($result['user_id'])) {
            print_r("Invalid Token");
            $connection->close($this->Magstruct(1, "认证失败"));
            return false;
        }
        // 设置账户消息的通信ID
        Member::where(['id' => $result['user_id']])->update(['notifyid' => $connection->id]);
        $connection->send($this->Magstruct(0, "连接成功"));
        return true;
    }

    public function Magstruct($code, $msg, $data = []){
        return json_encode([
            "code" => $code,
            "msg" => $msg,
            "datas" => $data
        ]);
    }

    // 解析字符串头为数组
    public function parseHeaders($http_buffer) {
        $headers = array();

        $requestHeaders = explode("\r\n", $http_buffer);
        foreach ($requestHeaders as $header) {
            if ($header !== '') {
                $headerParts = explode(': ', $header, 2);
                $headerKey = str_replace('-', '_', strtoupper($headerParts[0]));
                $headerValue = isset($headerParts[1]) ? trim($headerParts[1]) : '';
                $headers[$headerKey] = $headerValue;
            }
        }
        return $headers;
    }

}
822 0 0
0个回答

年代过于久远,无法发表回答
×
🔝