0

遇到了我在编码过程中遇到的最奇怪的错误。

我正在使用:带有文件驱动程序的 laravel 棘轮 websocket Laravel Cache

我正在尝试使用来自工匠命令类的 laravel 缓存从其闭包函数中缓存棘轮 websocket 响应消息。

当我在 websocket 响应上使用 var_dump 时,我会在终端上打印出所有消息。但是当我尝试保存在缓存中时,它返回 true 但缓存为空。不被存储,不显示错误消息。

这是我的代码:

use Illuminate\Support\Facades\Cache as Cache;


class Ticker extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daemon:t';

    /**
     * The console command description.
     *
     * @var string
     */
    
    protected $description = 'Listens to websockets t';
    /**
     * Create a new command instance.
     *
     * @return void
     */
     protected $cah;
    public function __construct(Cache $Cache)
    {
        $this->cah = new $Cache();
        parent::__construct();
        

    }
    
  


    public function mini(callable $callback)
    {

        // phpunit can't cover async function
        \Ratchet\Client\connect('wss://stream......')->then(function ($ws) use ($callback) {
            $ws->on('message', function ($data) use ($ws, $callback) {
                
                $json = json_decode($data, true);

                return call_user_func($callback, $this->cah, $json);
            });
            $ws->on('close', function ($code = null, $reason = null) {
                // WPCS: XSS OK.
                echo "....: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
            });
        }, function ($e) {
            // WPCS: XSS OK.
            echo "....: Could not connect: {$e->getMessage()}" . PHP_EOL;
        });
        
    }
       
       
    // save 
    
    public function saveT($t){
      //$this->alert($t);
      
      try{
         foreach ($t as $obj) {
              $this->cah::put($obj['s'], $obj, now()->addSeconds(5));
          }
      // used in testing
      //print_r($t);
 
      } catch (\Exception $exception) {
          $this->alert($exception->getMessage());
      }
    }
    
    
    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws \Exception
     */
            
    public function handle()
    {
        
        $this->mini(function ($api,$json){
          
           // saving directly though the $api object 
           $api::put(['s' => $json], now()->addSeconds(10));
         
            // when savingnthrough saveT function 
          // $this->saveT($json);
          
         
         try{
           // call_user_func_array(array($this->cah::class,'put'),array('test','tested',now()->addSeconds(5)));
        } catch (\Exception $exception) {
             $this->alert($exception->getMessage());
        }
       },);
    }
}

4

1 回答 1

0

您发布的示例代码是一堆相互冲突的约定、名称和引用。一旦你理清了你真正需要的是什么,这里是实际构造和调用可调用对象的正确形式。

namespace foo {
    class Cache {
        public static function put_static($a, $b, $c='optional?') {
            echo "put_static: $a $b $c\n";
        }
        
        public function put_instance($a, $b, $c='optional?') {
            echo "put_instance: $a $b $c\n";
        }
    }
}

namespace {
    use foo\Cache as Cache;
    $c = new Cache();
    
    $callback_static          = [Cache::class, 'put_static'];
    $callback_instance        = [$c,           'put_instance'];
    $callback_instance_static = [$c::class,    'put_static'];
    
    $vars_a = ['a', 'b'];
    $vars_b = ['a', 'b', 'c'];
    
    call_user_func_array($callback_static, $vars_a);
    call_user_func_array($callback_static, $vars_b);
    
    call_user_func_array($callback_instance, $vars_a);
    call_user_func_array($callback_instance, $vars_b);
    
    call_user_func_array($callback_instance_static, $vars_a);
    call_user_func_array($callback_instance_static, $vars_b);
}

输出:

put_static: a b optional?
put_static: a b c
put_instance: a b optional?
put_instance: a b c
put_static: a b optional?
put_static: a b c

您还可以使用 splat 运算符保存一些击键以...将数组解压缩为参数,因此call_user_func_array($func, $args)变为$func(...$args)

参考:

于 2021-09-25T00:19:30.550 回答