第一种方式
$task_connection = stream_socket_client('tcp://127.0.0.1:8721',$errno,$errstr);
$task_data = array(
'route' => 'demo/test',
);
fwrite($task_connection, json_encode($task_data));
if(!empty($errno) || !empty($errstr)){
$errmsg = "async_error-errno:{$errno},errstr:{$errstr}" ;
$errarr = [
"input" => $task_data ,
"errmsg" => $errmsg ,
] ;
}
第二种方式
$client = new \swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
try{
$client->connect('127.0.0.1', 8721);
if( !$client->isConnected())
{
exit("connect failed\n");
}
$data=json_encode(['route'=>'demo/test']);
// $data='close';
$client->send($data);
$client->close(true);
}catch (\Exception $e){
return $e->getMessage();
}