function onPacket($sock) { $data = $this->centerSocket->recv(); $req = unserialize($data); if (empty($req['cmd'])) { $this->log("error packet"); return; } if ($req['cmd'] == 'getInfo') { $this->centerSocket->send(serialize(['cmd' => 'putInfo', 'info' => ['hostname' => gethostname(), 'ipList' => swoole_get_local_ip(), 'uname' => php_uname(), 'version' => self::VERSION, 'deviceInfo' => ['cpu' => self::getCpuInfo(), 'mem' => self::getMemInfo(), 'disk' => self::getDiskInfo()]]])); } elseif ($req['cmd'] == 'upgrade') { if (empty($req['url']) or empty($req['hash'])) { $this->log("缺少URL和hash"); } $file = self::downloadPackage($req['url']); if ($file) { $hash = md5($file); //hash对比一致,可以更新 if ($hash == $req['hash']) { //更新phar包 file_put_contents($this->pharFile, $file); $this->log("upgrade to " . $req['version']); //退出进程,等待重新拉起 exit; } } else { $this->log("upgrade failed. Cannot fetch url [{$req['url']}]"); } } }
public function onConnect($serv, $fd) { $this->connectioninfo = $serv->connection_info($fd); $localinfo = swoole_get_local_ip(); $this->localip = current($localinfo); if ($this->localip != $this->connectioninfo['remote_ip']) { $this->client_pool[ip2long($this->connectioninfo['remote_ip'])] = array('fd' => $fd, 'remote_ip' => $this->connectioninfo['remote_ip']); } }
public function __construct() { $this->client = new swoole_client(SWOOLE_SOCK_TCP); $config_obj = Yaf_Registry::get("config"); $distributed_config = $config_obj->distributed->toArray(); $localinfo = swoole_get_local_ip(); if (!$this->client->connect(current($localinfo), $distributed_config['port'], -1)) { exit("connect failed. Error: {$client->errCode}\n"); } }
<?php var_dump(swoole_get_local_ip());
protected function setHost() { $ipList = swoole_get_local_ip(); if (isset($ipList['eth1'])) { $this->host = $ipList['eth1']; } elseif (isset($ipList['eth0'])) { $this->host = $ipList['eth0']; } else { $this->host = '0.0.0.0'; } }
public function onConnect($serv) { $localinfo = swoole_get_local_ip(); $serv->send(json_encode(array('type' => 'system', 'data' => array('code' => 10001, 'status' => 1, 'fd' => current($localinfo))))); }
<?php $ifs = swoole_get_local_ip(); if (isset($ifs['eth0'])) { $server_ip = $ifs['eth0']; } else { die("Step-0 failed. Error: swoole_get_local_ip() failed."); } echo "-------------------------------------------------------------\n"; echo "Swoole Unit Tests. ServerIP: {$server_ip}\n"; echo "-------------------------------------------------------------\n"; /** * UnitTests for swoole server. * This is just a client. Server see examples/server.php */ $i = 0; $client = new swoole_client(SWOOLE_TCP); if (!$client->connect($server_ip, 9501)) { echo "Step-" . $i++ . ": failed. Error: connect to server failed.\n"; } else { if (!$client->send("hello")) { echo "Step-" . $i++ . ": failed. Error: send to server failed.\n"; } $data = $client->recv(); if (!$data) { echo "Step-" . $i++ . ": failed. Error: send to server failed.\n"; } else { if ($data != "Swoole: hello") { echo "Step-" . $i++ . ": failed. Error: recv error data.\n"; } else { echo "TCP-Test-OK\n";
public function excute() { $arrData = array('local_ip' => swoole_get_local_ip(), 'version' => swoole_version(), 'setting' => $this->_objDispatcher->getBootstrap()->getServer()->setting, 'stats' => $this->_objDispatcher->getBootstrap()->getServer()->stats()); $response = $this->_objDispatcher->getResponse(); $response->end(json_encode($arrData)); }
public static function getLocalip() { if (function_exists('swoole_get_local_ip')) { $ips = swoole_get_local_ip(); if (isset($ips['eth1'])) { $ip = $ips['eth1']; } elseif (isset($ips['eth0'])) { $ip = $ips['eth0']; } else { $ip = '127.0.0.1'; } } else { $di = DI::getDefault(); $ip = $di['request']->getServerAddress(); } return $ip; }
$color = $failed ? self::RED : self::GREEN; $status = $failed ? "Failed" : "Success"; $space = str_repeat(' ', self::LINE_N - strlen($msg)); echo "{$msg}{$space}[{$color}m [{$status}][0m\n"; if ($failed and $die) { die; } } } echo "------------------------------------------------------------------\n"; echo "Swoole Unit Tests. Version 1.0.0\n"; echo "Author: Han Tianfeng.\n"; echo "CopyRight: Swoole-Team.\n"; echo "------------------------------------------------------------------\n"; Console::put("extension_loaded('swoole')", !extension_loaded('swoole'), true); Console::put("swoole_get_local_ip()", !is_array(swoole_get_local_ip())); Console::put("swoole_version()", !is_string(swoole_version())); if (!class_exists('swoole_process')) { Console::put("no have swoole_process", true, true); } $server = new swoole_process(function ($process) { $php = $_SERVER['_']; return $process->exec($php, array(__DIR__ . "/../examples/server.php", 'daemon')); }, false, false); if (!$server->start()) { Console::put("Server start failed.", true, true); } usleep(200000); register_shutdown_function(function () { global $server; if (swoole_process::kill($server->pid, SIGTERM)) {
/** * 获取当前服务器ip,用于服务发现上报IP * * @return string */ protected function getLocalIp() { if ($this->serverIP == '0.0.0.0' || $this->serverIP == '127.0.0.1') { $serverIps = swoole_get_local_ip(); $patternArray = array('10\\.', '172\\.1[6-9]\\.', '172\\.2[0-9]\\.', '172\\.31\\.', '192\\.168\\.'); foreach ($serverIps as $serverIp) { // 匹配内网IP if (preg_match('#^' . implode('|', $patternArray) . '#', $serverIp)) { return $serverIp; } } } return $this->serverIP; }