遇到了我在编码过程中遇到的最奇怪的错误。
我正在使用:带有文件驱动程序的 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());
}
},);
}
}