本文整理汇总了PHP中swoole_client::close方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_client::close方法的具体用法?PHP swoole_client::close怎么用?PHP swoole_client::close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_client
的用法示例。
在下文中一共展示了swoole_client::close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: close
public function close()
{
if (!$this->connected) {
return true;
}
return $this->client->close();
}
示例2: run
public function run(Promise &$promise)
{
$cli = new \swoole_client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC);
$urlInfo = parse_url($this->url);
$timeout = $this->timeout;
if (!isset($urlInfo['port'])) {
$urlInfo['port'] = 80;
}
$httpParser = new \HttpParser();
$cli->on("connect", function ($cli) use($urlInfo, &$timeout, &$promise) {
$cli->isConnected = true;
$host = $urlInfo['host'];
if ($urlInfo['port']) {
$host .= ':' . $urlInfo['port'];
}
$req = array();
$req[] = "GET {$this->url} HTTP/1.1\r\n";
$req[] = "User-Agent: PHP swAsync\r\n";
$req[] = "Host:{$host}\r\n";
$req[] = "Connection:close\r\n";
$req[] = "\r\n";
$req = implode('', $req);
$cli->send($req);
});
$cli->on("receive", function ($cli, $data = "") use(&$httpParser, &$promise) {
$ret = $httpParser->execute($data);
if ($ret !== false) {
Timer::del($cli->sock);
$cli->isDone = true;
if ($cli->isConnected()) {
$cli->close();
}
$promise->accept(['http_data' => $ret]);
}
});
$cli->on("error", function ($cli) use(&$promise) {
Timer::del($cli->sock);
$promise->accept(['http_data' => null, 'http_error' => 'Connect error']);
});
$cli->on("close", function ($cli) use(&$promise) {
});
if ($this->proxy) {
$cli->connect($this->proxy['host'], $this->proxy['port'], 0.05);
} else {
$cli->connect($urlInfo['host'], $urlInfo['port'], 0.05);
}
$cli->isConnected = false;
if (!$cli->errCode) {
Timer::add($cli->sock, $this->timeout, function () use($cli, &$promise) {
@$cli->close();
if ($cli->isConnected) {
$promise->accept(['http_data' => null, 'http_error' => 'Http client read timeout']);
} else {
$promise->accept(['http_data' => null, 'http_error' => 'Http client connect timeout']);
}
});
}
}
示例3: onConnect
function onConnect($serv, $fd, $from_id)
{
$socket = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
echo microtime() . ": Client[{$fd}] backend-sock[{$socket->sock}]: Connect.\n";
$socket->on('connect', function (swoole_client $socket) {
echo "connect to backend server success\n";
});
$socket->on('error', function (swoole_client $socket) {
echo "connect to backend server fail\n";
$this->serv->send($this->backends[$socket->sock]['client_fd'], "backend server not connected. please try reconnect.");
$this->serv->close($this->backends[$socket->sock]['client_fd']);
$socket->close();
});
$socket->on('close', function (swoole_client $socket) {
echo "backend connection close\n";
});
$socket->on('receive', function (swoole_client $socket, $data) {
//PHP-5.4以下版本可能不支持此写法,匿名函数不能调用$this
//可以修改为类静态变量
$servinst->send($this->backends[$socket->sock]['client_fd'], $data);
});
if ($socket->connect('61.135.169.125', 80, 1)) {
$this->backends[$socket->sock] = array('client_fd' => $fd, 'socket' => $socket);
$this->clients[$fd] = array('socket' => $socket);
}
}
示例4: test_client
function test_client()
{
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
//同步阻塞
if (!$client->connect('127.0.0.1', 10000)) {
exit("connect fail\n");
}
if (empty($argv[1])) {
$loop = 1;
} else {
$loop = intval($argv[1]);
}
for ($i = 0; $i < $loop; $i++) {
$client->send(str_repeat("A", 600) . $i);
$data = $client->recv(7000, 0);
if ($data === false) {
echo "recv fail\n";
break;
}
//echo "recv[$i]",$data,"\n";
}
//echo "len=".strlen($data)."\n";
// $client->send("HELLO\0\nWORLD");
// $data = $client->recv(9000, 0);
$client->close();
var_dump($data);
unset($client);
}
示例5: client
/**
* connect to swoole server then send data
*
* @return string
*/
public static function client()
{
$return = FALSE;
$client = new \swoole_client(SWOOLE_SOCK_TCP);
// set eof charactor
$client->set(['open_eof_split' => TRUE, 'package_eof' => self::EOFF]);
// listen on
$client->on('connect', '\\CI_Swoole\\Client::on_connect');
$client->on('receive', '\\CI_Swoole\\Client::on_receive');
$client->on('error', '\\CI_Swoole\\Client::on_error');
$client->on('close', '\\CI_Swoole\\Client::on_close');
// connect
$client->connect(self::HOST, self::PORT, 10);
// send data
if ($client->isConnected()) {
$post = serialize(static::$post);
$post .= self::EOFF;
$issend = $client->send($post);
}
// receiv data
if (isset($issend) && $issend) {
$return = @$client->recv();
$return = str_replace(self::EOFF, '', $return);
$return = unserialize($return);
}
$client->close();
unset($client);
return $return;
}
示例6: onReceive
static function onReceive(swoole_client $cli, $data)
{
self::putLog("RECV#" . $cli->sock . "\$");
$r = rand(0, 100);
//20%几率关闭连接
if ($r > 80) {
self::putLog("CLOSE#" . $cli->sock . "\$");
$cli->close();
} elseif ($r > 60) {
$index = array_rand(self::$clients);
$_cli = self::$clients[$index];
self::putLog("CLOSE#" . $_cli->sock . "\$");
$_cli->close();
} elseif ($r > 40) {
$index = array_rand(self::$clients);
$_cli = self::$clients[$index];
self::send($_cli);
} elseif ($r > 20) {
//不要超过最大客户端数量
if (count(self::$clients) < self::MAX_CLIENTS) {
self::create();
}
} else {
self::send($cli);
}
}
示例7: send
public function send(callable $callback)
{
$client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function ($cli) {
$cli->send($this->data);
});
$client->on('close', function ($cli) {
});
$client->on('error', function ($cli) use($callback) {
$cli->close();
call_user_func_array($callback, array('r' => 1, 'key' => $this->key, 'calltime' => $this->calltime, 'error_msg' => 'conncet error'));
});
$client->on("receive", function ($cli, $data) use($callback) {
$this->calltime = microtime(true) - $this->calltime;
$cli->close();
call_user_func_array($callback, array('r' => 0, 'key' => $this->key, 'calltime' => $this->calltime, 'data' => $data));
});
if ($client->connect($this->ip, $this->port, $this->timeout, 1)) {
$this->calltime = microtime(true);
if (floatval($this->timeout) > 0) {
$this->timer = swoole_timer_after(floatval($this->timeout) * 1000, function () use($client, $callback) {
$client->close();
\SysLog::error(__METHOD__ . " TIMEOUT ", __CLASS__);
$this->calltime = microtime(true) - $this->calltime;
call_user_func_array($callback, array('r' => 2, 'key' => '', 'calltime' => $this->calltime, 'error_msg' => 'timeout'));
});
}
}
}
示例8: sendtagbyswoole
/**
* send message by swoole
* @param string $content the command
* return boolean true if shut down the swoole_client successfully
*/
private function sendtagbyswoole($content)
{
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 8888, 0.5, 0);
$client->send($content);
return $client->close();
}
示例9: sendData
public function sendData(callable $callback)
{
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function ($cli) {
$cli->send($this->data);
});
$client->on('close', function ($cli) {
});
$client->on('error', function ($cli) use($callback) {
$cli->close();
call_user_func_array($callback, array('r' => 1, 'key' => $this->key, 'error_msg' => 'conncet error'));
});
$client->on("receive", function ($cli, $data) use($callback) {
$cli->close();
call_user_func_array($callback, array('r' => 0, 'key' => $this->key, 'data' => $data));
});
if ($client->connect($this->ip, $this->port, $this->timeout)) {
if (intval($this->timeout) > 0) {
swoole_timer_after(intval($this->timeout) * 1000, function () use($client, $callback) {
if ($client->isConnected()) {
$client->close();
call_user_func_array($callback, array('r' => 2, 'key' => '', 'error_msg' => 'timeout'));
}
});
}
}
}
示例10: sendData
public function sendData(callable $callback)
{
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function ($cli) {
$this->isConnect = true;
$cli->send($this->data);
});
$client->on('close', function ($cli) {
$this->isConnect = false;
});
$client->on('error', function ($cli) use($callback) {
$this->isConnect = false;
$cli->close();
call_user_func_array($callback, array('r' => 1, 'key' => $this->key, 'error_msg' => 'conncet error'));
});
$client->on("receive", function ($cli, $data) use($callback) {
$this->isConnect = false;
$cli->close();
call_user_func_array($callback, array('r' => 0, 'key' => $this->key, 'data' => $data));
});
if ($client->connect($this->ip, $this->port, $this->timeout)) {
if (intval($this->timeout) > 0) {
swoole_timer_after(intval($this->timeout) * 1000, function () use($client, $callback) {
if ($this->isConnect) {
//error_log(__METHOD__." client ===== ".print_r($client,true),3,'/tmp/client.log');
$client->close();
call_user_func_array($callback, array('r' => 2, 'key' => '', 'error_msg' => 'timeout'));
}
});
}
}
}
示例11: __destruct
public function __destruct()
{
Trace::debug("*********cli {$this->fd} __destruct");
if (isset($this->cli)) {
$this->cli->isConnected() && $this->cli->close();
unset($this->cli);
}
unset($this->queue);
}
示例12: clientAction
public function clientAction()
{
$client = new swoole_client(SWOOLE_SOCK_TCP);
$client->connect('192.168.80.140', 9021, 0.5);
$client->send('hello world!');
echo $client->recv();
$client->close();
return false;
}
示例13: onReceive
function onReceive($serv, $fd, $from_id, $data)
{
$socket = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$socket->on('connect', function (swoole_client $socket) use($data) {
$socket->send($data);
});
$socket->on('error', function (swoole_client $socket) use($serv, $fd) {
_e("connect to backend server fail", __LINE__);
$serv->send($fd, "backend server not connected. please try reconnect.");
$socket->close();
});
$socket->on('close', function (swoole_client $socket) use($serv, $fd) {
$serv->close($fd);
});
$socket->on('receive', function (swoole_client $socket, $data) use($serv, $from_id, $fd) {
$serv->send($fd, $data, $from_id);
$socket->close();
});
$socket->connect('127.0.0.1', 8002, 0.2);
}
示例14: thread_start
function thread_start(swoole_thread $coroutine)
{
$serv = $coroutine->serv;
$data = $serv->recv($fd);
$socket = new swoole_client(SWOOLE_SOCK_TCP);
if ($socket->connect('127.0.0.1', 9502, 0.5)) {
$socket->send("request\n");
$response = $socket->recv();
}
$socket->close();
$serv->send($fd, "Server: {$response}\n");
}
示例15: swooleCall
public function swooleCall($data, $remoteUri = '', $return = 0)
{
$address = '127.0.0.1:9588';
$data2 = json_encode($data);
if (!empty($remoteUri)) {
$address = $remoteUri;
}
$client = new \swoole_client(SWOOLE_SOCK_TCP);
$address = explode(':', $address);
if (!@$client->connect($address[0], $address[1], 20)) {
return $this->call($data);
}
$client->send($data2);
if ($return) {
$res = $client->recv();
$client->close();
return json_decode($res);
}
$client->close();
return array('code' => 200, 'data' => 0);
}