自定义协议类实现Workerma/ProtocolInterface接口启动时报找不到

chenStudy

问题描述

自定义协议类实现ProtocolInterface接口启动时报找不到Workerman下的ProtocolInterface这个接口实际上是有的

程序代码

<?php

namespace Protocols;

use App\Models\DeviceInfo;
use App\Service\Admin\DeviceInfoService;
use App\Utils\CRC16Util;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Workerman\Connection\ConnectionInterface;
use Workerman\Protocols\ProtocolInterface;

class DeviceHandle implements ProtocolInterface
{
    public
    static function input($recv_buffer, ConnectionInterface $connection)
    {
        $tempdata = bin2hex($recv_buffer);
        echo "=========================收到上报数据==================== " . PHP_EOL . $tempdata . PHP_EOL;
        if (self::isMagicStr($tempdata) && strlen($tempdata) < 24) {
            return 0;
        }
        $messageData = $tempdata;
        // 是否以7d7d开头--用于粘包处理
        while (self::isMagicStr($messageData)) {
            // 获取报文长度
            $length = substr($messageData, 18, 4);
            $dataLength = (hexdec($length) * 2);//获取命令字节到报文结束的长度(除去CRC校验的两字节)
            //截取数据报文
            $data = substr($messageData, 22, $dataLength + 4); // 拆包传输问题
            $dataLen = strlen($data);
            if ($dataLen < $dataLength) {
                // 接收的不是一个完整的报文
                return 0;
            }
            $checkData = substr($messageData, 4, 14 + $dataLen); // 获取需要校验的数据
            $crcCheckResult = dechex(CRC16Util::crc16($checkData, "hex", 0x1021, 0, 0, true, true));
            $crcJoin = DeviceInfoService::crcLengthJoin($crcCheckResult);
            $crcCheckResult = $crcJoin . $crcCheckResult;
            echo "CRC校验码为:" . $crcCheckResult . PHP_EOL;
            if (substr($data, $dataLen - 4) != $crcCheckResult) {
                return false;
            }
            // 截取固定消息头
            $fixation = substr($messageData, 0, 22);
            Log::info("截取报文数据===> " . $fixation . $data);
            return (strlen($fixation) + $dataLen) / 2;
        }
        return false;
    }

    /**
     * 判断是否7d7d开头
     * @param $message
     * @return bool
     */
    public static function isMagicStr($message): bool
    {
        $stateStr = substr($message, 0, 4);
        return strcasecmp($stateStr, "7d7d") == 0;
    }

    public
    static function decode($recv_buffer, ConnectionInterface $connection)
    {
        $tempdata = bin2hex($recv_buffer);
        return $tempdata;
    }

    public
    static function encode($data, ConnectionInterface $connection)
    {
        // TODO: Implement encode() method.
        return $data;
    }
}

截图报错信息里报错文件相关代码

截图

753 1 0
1个回答

静默

引入composer的自动加载

require_once __DIR__ . '/vendor/autoload.php';
  • 暂无评论
年代过于久远,无法发表回答
×
🔝