Having the most strange error I've ever encountered in my coding journey.
I am using: laravel ratchet websocket Laravel Cache with file driver
I am trying to cache ratchet websocket response messages from within its closure function using laravel cache from an artisan command's class.
When I use var_dump on the websocket response, I get all the messages printed out on my terminal. But when I try to save in cache, it return true but the cache is empty. Not get stored, no error message is displayed.
This is my code:
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());
}
},);
}
}
The example code you've posted is a tangle of conflicting conventions, names, and references. Once you sort out what it is that you actually need, here are the correct forms for actually constructing and and invoking the callables.
Output:
You can also save some keystrokes with the splat operator
...to unpack the array as arguments, socall_user_func_array($func, $args)becomes$func(...$args)Ref: