public function __construct($uris = null) { parent::__construct($uris); swoole_async_set(array("socket_buffer_size" => 2 * 1024 * 1024 * 1024 - 1, "socket_dontwait" => false)); }
function run($host = "0.0.0.0", $port = 9507) { swoole_async_set(['enable_signalfd' => false]); $serv = new \swoole_server($host, $port, SWOOLE_BASE); $this->serv = $serv; $runtime_config = array('worker_num' => 1, 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 2000000); global $argv; if (!empty($argv[1]) and $argv[1] == 'daemon') { $runtime_config['daemonize'] = true; } $serv->set($runtime_config); $this->init(); $serv->on('connect', array($this, 'onConnect')); $serv->on('receive', array($this, 'onReceive')); $serv->on('close', array($this, 'onClose')); $serv->start(); }
protected function init() { # 设置参数 if (isset(self::$config['php']['error_reporting'])) { error_reporting(self::$config['php']['error_reporting']); } if (isset(self::$config['php']['timezone'])) { date_default_timezone_set(self::$config['php']['timezone']); } if (isset($config['server']['unixsock_buffer_size']) && $config['server']['unixsock_buffer_size'] > 1000) { # 修改进程间通信的UnixSocket缓存区尺寸 ini_set('swoole.unixsock_buffer_size', $config['server']['unixsock_buffer_size']); } if (self::$config['clusters']['mode'] !== 'none') { if (!function_exists('\\msgpack_pack')) { self::warn('开启集群模式必须安装 msgpack 插件'); exit; } } if (!self::$config['server']['socket_block']) { # 设置不阻塞 swoole_async_set(['socket_dontwait' => 1]); } $this->info("======= Swoole Config ========\n" . json_encode(self::$config['swoole'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); if (self::$clustersType > 0) { # 集群模式下初始化 Host 设置 Clusters\Host::init(self::$config['clusters']['register']['is_register']); } }
<?php swoole_async_set(array('dns_lookup_random' => true)); swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip) { echo "{$host} reslove to {$ip}\n"; swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip) { echo "{$host} reslove to {$ip}\n"; }); });