protected function request($request) { $runDir = $this->service->getRunDir(); $port = (include $runDir . 'service-port.php'); $sockFile = $runDir . 'service-' . $port . '.sock'; $client = new Client(SWOOLE_UNIX_STREAM); $client->set(['open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4]); if (!@$client->connect($sockFile, 0, 20)) { return false; } $request = serialize($request); $client->send(pack('N', $length = strlen($request))); if ($length > 2097152) { foreach (str_split($request, 1048576) as $chunk) { $client->send($chunk); } } else { $client->send($request); } $response = $client->recv(); $client->close(); if ($response === false) { return false; } $length = unpack('N', $response)[1]; return unserialize(substr($response, -$length)); }
/** * 关闭服务 */ public function close() { if ($this->__client) { @$this->__client->close(); $this->__client = null; } }
function post_topic() { if (empty($_POST['content']) or empty($_POST['title']) or empty($_POST['category_id'])) { $this->http->status(403); return "access deny\n"; } $this->session->start(); if (empty($_SESSION['user'])) { return $this->json('', 403, "需要登录"); } $user = $_SESSION['user']; $put['question_content'] = trim($_POST['title']); $put['question_detail'] = trim($_POST['content']); $put['published_uid'] = $user['uid']; $put['update_time'] = $put['add_time'] = time(); $put['ip'] = ip2long(Swoole\Client::getIP()); $put['category_id'] = intval($_POST['category_id']); $id = table('aws_question')->put($put); if ($id) { return $this->json(['topic_id' => $id]); } else { return $this->json('', 500, "操作失败,请稍后重试"); } }
function ip() { echo "My ip is " . Swoole\Client::getIP() . "\n"; }