示例代码 - UDP命令解析
优质
小牛编辑
138浏览
2023-12-01
与TCP命令解析同理,直接上代码
解析器
namespace AppSock;
use CoreComponentSocketAbstractInterfaceAbstractClient;
use CoreComponentSocketAbstractInterfaceAbstractCommandParser;
use CoreComponentSocketCommonCommand;
class Parser extends AbstractCommandParser
{
function parser(Command $result, AbstractClient $client, $rawData)
{
// TODO: Implement parser() method.
$data = trim($rawData);
$data = explode(',',$data);
$result->setCommand(array_shift($data));
$result->setMessage(array_shift($data));
}
}
命令注册
namespace AppSock;
use CoreComponentLogger;
use CoreComponentSocketAbstractInterfaceAbstractCommandRegister;
use CoreComponentSocketClientUdpClient;
use CoreComponentSocketCommonCommand;
use CoreComponentSocketCommonCommandList;
use CoreComponentSocketResponse;
use CoreSwooleAsyncTaskManager;
class Register extends AbstractCommandRegister
{
function register(CommandList $commandList)
{
// TODO: Implement register() method.
$commandList->addCommandHandler('hello',function (Command $request,UdpClient $client){
$message = $request->getMessage();
Logger::getInstance()->console('message is '.$message,false);
AsyncTaskManager::getInstance()->add(function ()use($client){
sleep(2);
Response::response($client,"this is delay message for hellon");
});
return "response for hellon";
});
$commandList->setDefaultHandler(function (){
return "unkown commandn";
});
}
}
注意,UDP的回调客户端类型是UdpClient
事件监听
use AppSockParser;
use AppSockRegister;
use CoreComponentSocketDispatcher;
function beforeWorkerStart(swoole_server $server)
{
// TODO: Implement beforeWorkerStart() method.
$udp = $server->addlistener("0.0.0.0",9503,SWOOLE_UDP);
//udp 请勿用receive事件
$udp->on('packet',function(swoole_server $server, $data,$clientInfo){
Dispatcher::getInstance(Register::class,Parser::class)->dispatchUDP($data,$clientInfo);
});
}
测试代码
$client = new swoole_client(SWOOLE_SOCK_UDP);
if (!$client->connect('127.0.0.1', 9503, -1))
{
exit("connect failed. Error: {$client->errCode}n");
}
$client->send("hellon");
echo $client->recv();
$client->close();