TCP
EasySwoole
创建TCP
服务器,有两种以下方式:
主服务
修改配置文件MAIN_SERVER.SERVER_TYPE
为EASYSWOOLE_SERVER
。
EasySwooleEvent
中mainServerCreate事件进行回调注册:
public static function mainServerCreate(\EasySwoole\EasySwoole\Swoole\EventRegister $register)
{
$register->add($register::onConnect, function (\Swoole\Server $server, int $fd, int $reactor_id) {
echo "fd {$fd} connected";
});
$register->add($register::onReceive, function (\Swoole\Server $server, int $fd, int $reactor_id, string $data) {
echo "fd:{$fd} send:{$data}\n";
});
$register->add($register::onClose, function (\Swoole\Server $server, int $fd, int $reactor_id) {
echo "fd {$fd} closed";
});
}
子服务
顾名思义:另开一个端口进行tcp
监听。
EasySwooleEvent
中mainServerCreate事件,进行子服务监听:
public static function mainServerCreate(\EasySwoole\EasySwoole\Swoole\EventRegister $register)
{
$server = \EasySwoole\EasySwoole\ServerManager::getInstance()->getSwooleServer();
$subPort = $server->addlistener('0.0.0.0', 9502, SWOOLE_TCP);
$subPort->set([
// swoole 相关配置
'open_length_check' => false,
]);
$subPort->on($register::onConnect, function (\Swoole\Server $server, int $fd, int $reactor_id) {
echo "fd {$fd} connected";
});
$subPort->on($register::onReceive, function (\Swoole\Server $server, int $fd, int $reactor_id, string $data) {
echo "fd:{$fd} send:{$data}\n";
});
$subPort->on($register::onClose, function (\Swoole\Server $server, int $fd, int $reactor_id) {
echo "fd {$fd} closed";
});
}
当前内容版权归 EasySwoole 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 EasySwoole .