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')); } }); } } }
function send() { $pid = posix_getpid(); $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->on("connect", function (swoole_client $cli) use($pid) { $data = microtime(true); lg("[{$pid}] Send: {$data}", __LINE__); $cli->send($data); }); $client->on("receive", function (swoole_client $cli, $data) use($pid) { lg("[{$pid}] Received: {$data}", __LINE__); $cli->close(); }); $client->on("error", function (swoole_client $cli) use($pid) { $cli->close(); //lg("[$pid] error then conn close", __LINE__); exit(0); }); $client->on("close", function (swoole_client $cli) use($pid) { //lg("[$pid] conn close", __LINE__); }); $client->connect('127.0.0.1', 8001, 0.5); //lg("[$pid] create conn succ", __LINE__); exit(0); }
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); } }
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')); }); } } }
public function sendData($tc, $c) { $client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC); $client->on("connect", function ($cli) use($tc) { $cli->send($tc->data); }); $client->on('close', function ($cli) { }); $client->on('error', function ($cli) { }); $client->on("receive", function ($cli, $data) use($c) { $cli->close(); $tc = $c->send($data); if ($tc instanceof TestClient) { $this->sendData($tc, $c); } else { unset($c); } }); if ($client->connect($tc->ip, $tc->port, $tc->timeout)) { // if (intval($tc ->timeout) >0) { // swoole_timer_after(intval($tc ->timeout) * 1000, function() use($client){ // if ($client ->isConnected()) { // $log = "timeout \n"; // error_log($log,3,'/tmp/timeout.log'); // $client ->close(); // $this ->callback('timeout'); // } // }); // } } }
/** * 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; }
public function createBackend() { $backend = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $backend->on('connect', function ($cli) { echo "backend connect.\n"; }); $backend->on('receive', function ($cli, $data) { echo "backend receive.\n"; $fd = (int) $cli->sock; if (!empty($this->b2c[$fd]['fd'])) { $this->server->send($this->b2c[$fd]['fd'], $data); } }); $backend->on('close', function ($cli) { if (!empty($cli)) { $this->backendReconnect($cli); //$this->clearClient((int)$cli->sock, $from_client=false); } }); $backend->on('error', function ($cli) { echo "error\n"; }); $backend->connect($this->backend_info['ip'], $this->backend_info['port']); return $backend; }
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')); } }); } } }
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']); } }); } }
public function connect($client) { $this->socket = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $this->socket->on('Connect', array($client, 'onConnect')); $this->socket->on('Receive', array($client, 'onReceive')); $this->socket->on('Close', array($client, 'onClose')); $this->socket->on('Error', array($client, 'onError')); if (!$this->socket->connect($this->host, $this->port)) { return false; } return true; }
/** * 创建一个新的客户端 */ static function create() { $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", 'G::onConnect'); $client->on("receive", 'G::onReceive'); $client->on("error", function (swoole_client $cli) { echo "error\n"; }); $client->on("close", 'G::onClose'); $client->connect('127.0.0.1', 9502); self::$count++; self::putLog("CREATE#" . $client->sock . "\$"); }
/** * 数据发送 * * @param $msg * @return mixed */ public function send($msg) { $client = new \swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC); $client->on('connect', function ($cli) use($msg) { try { $cli->send($msg); } catch (\Exception $e) { } }); $client->on("receive", [$this, "onReceive"]); $client->on('close', [$this, 'onClose']); $client->on('error', [$this, 'onError']); return $client->connect($this->host, $this->port, $this->timeOut, true); }
public function addServerClient($address) { $client = new swoole_client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC); $client->on('Connect', array(&$this, 'onConnect')); $client->on('Receive', array(&$this, 'onReceive')); $client->on('Close', array(&$this, 'onClose')); $client->on('Error', array(&$this, 'onError')); $config_obj = Yaf_Registry::get("config"); $distributed_config = $config_obj->distributed->toArray(); $client->connect($address, $distributed_config['port']); $this->cur_address = $address; $this->table->set(ip2long($address), array('clientfd' => ip2long($address))); $this->b_client_pool[ip2long($address)] = $client; return $client; }
function connect($host, $port) { if (empty($this->host)) { $this->host = $host; $this->port = $port; } $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", [$this, 'onConnect']); $client->on("receive", function (swoole_client $cli, $data) { $cli->send("HELLO"); echo "recv from server: {$data}\n"; usleep(100000); }); $client->on("error", [$this, 'onError']); $client->on("close", [$this, 'onClose']); $client->connect($host, $port); $this->swoole_client = $client; }
public static function request(callable $callback, $url, $method = 'GET', array $headers = [], array $params = []) { $parsed_url = parse_url($url); \swoole_async_dns_lookup($parsed_url['host'], function ($host, $ip) use($parsed_url, $callback, $url, $method, $headers, $params) { $port = isset($parsed_url['port']) ? $parsed_url['port'] : 'https' == $parsed_url['scheme'] ? 443 : 80; $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $method = strtoupper($method); $client->on("connect", function ($cli) use($url, $method, $parsed_url, $headers, $params) { \ZPHP\Common\AsyncHttpClient::clear($cli); $path = isset($parsed_url['path']) ? $parsed_url['path'] : '/'; if (!empty($params)) { $query = http_build_query($params); if ('GET' == $method) { $path .= "?" . $query; } } $sendData = $method . " {$path} HTTP/1.1\r\n"; $headers = array('Host' => $parsed_url['host'], 'Connection' => 'keep-alive', 'Pragma' => 'no-cache', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', 'Referer' => $url, 'Accept-Encoding' => 'gzip, deflate, sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8') + $headers; foreach ($headers as $key => $val) { $sendData .= "{$key}: {$val}\r\n"; } if ('POST' === $method) { $sendData .= "Content-Length: " . strlen($query) . "\r\n"; $sendData .= "\r\n" . $query; } else { $sendData .= "\r\n"; } $cli->send($sendData); }); $client->on("receive", function ($cli, $data) use($callback) { $ret = self::parseBody($cli, $data, $callback); if (is_array($ret)) { call_user_func_array($callback, array($cli, $ret)); } }); $client->on("error", function ($cli) { \ZPHP\Common\AsyncHttpClient::clear($cli); }); $client->on("close", function ($cli) { \ZPHP\Common\AsyncHttpClient::clear($cli); }); $client->connect($ip, $port); }); }
public function __construct() { $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //设置事件回调函数 $client->on("connect", function ($cli) { //$cli->send("hello world swoole !\n"); $cli->send("demo"); }); $client->on("receive", function ($cli, $data) { echo "Received: " . $data . "\n"; }); $client->on("error", function ($cli) { echo "Connect failed\n"; }); $client->on("close", function ($cli) { echo "Connection close\n"; }); //发起网络连接 $client->connect('0.0.0.0', 9500, 1); }
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); }
function getResult2($response) { $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on('connect', function ($cli) { echo "cli1 connect\n"; $cli->send("hello world\n"); }); $client->on('Receive', function ($cli, $data) use($response) { echo "cli1 receive\n"; $response->end($data); $cli->close(); }); $client->on("error", function ($cli) use($response) { echo "cli1 error\n"; $response->end("empty\n"); }); $client->on("close", function ($cli) { echo "cli1 close\n"; }); $client->connect('127.0.0.1', 9501); }
public static function send($ip, $port, $data, $callback, $timeout = 0.2) { $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //设置事件回调函数 $client->on("connect", function ($cli) { $cli->send($cli->data); }); $client->on("receive", function ($cli, $data) { //unset(self::$timerSockets[$cli->sock]); call_user_func_array($cli->callback, array('r' => 0, 'data' => $data)); }); $client->on("error", function ($cli) { call_user_func_array($cli->callback, array('r' => -1)); }); $client->on("close", function ($cli) { }); $client->data = $data; $client->callback = $callback; $client->timeout = $timeout; $client->createTime = microtime(true); //发起网络连接 $client->connect($ip, $port, $timeout); self::addTimeOut($client); }
<?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->finish = false; $client->_count = 0; $client->on("connect", function (swoole_client $cli) { $cli->send("GET / HTTP/1.1\r\n\r\n"); //$cli->sendfile(__DIR__.'/test.txt'); //$cli->_count = 0; }); $client->on("receive", function (swoole_client $cli, $data) { echo "Receive: {$data}"; $cli->_count++; if ($cli->_count > 10) { $cli->close(); return; } $cli->send(str_repeat('A', 100) . "\n"); $cli->finish = true; }); $client->on("error", function (swoole_client $cli) { echo "error\n"; }); $client->on("close", function (swoole_client $cli) { echo "Connection close\n"; }); $client->connect('127.0.0.1', 9502); swoole_timer_after(1000, function () use($client) { if ($client->finish) { return;
public function send(callable $callback) { $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function ($cli) { $cli->send($this->request); }); $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) { call_user_func_array(array($this, 'packRsp'), array('r' => 0, 'key' => $cli, 'data' => $data)); }); $this->callback = $callback; if ($client->connect($this->host, $this->port, $this->timeout)) { //同步调用 优化下? flag = 1 $this->calltime = microtime(true); } }
<?php function send(swoole_client $cli) { $data = array('str1' => str_repeat('A', rand(1000, 9000)), 'str2' => str_repeat('B', rand(1000, 9000)), 'str3' => str_repeat('C', rand(1000, 9000))); $data['int1'] = rand(100000, 999999); $sendStr = serialize($data); $sendData = pack('N', strlen($sendStr)) . $sendStr; $cli->send($sendData); echo "send length=" . strlen($sendData) . ", SerId={$data['int1']}\n"; } $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->set(array('open_length_check' => 1, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 2000000)); $client->on("connect", function (swoole_client $cli) { send($cli); }); $client->on("receive", function (swoole_client $cli, $data) { $resp = unserialize(substr($data, 4)); echo "recv length=" . strlen($data) . ", SerId={$resp['int1']}\n" . str_repeat('-', 60) . "\n"; // sleep(1); //usleep(200000); //send($cli); }); $client->on("error", function (swoole_client $cli) { echo "error\n"; }); $client->on("close", function (swoole_client $cli) { echo "Connection close\n"; }); $client->connect('127.0.0.1', 9501);
/** * [send 异步IO,定时器设置,异常回调] * @param callable $callback [description] * @return [type] [description] */ public function send(callable $callback) { $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function ($cli) { $cli->send($this->request); }); $client->on('close', function ($cli) { }); $client->on('error', function ($cli) use($callback) { $cli->close(); $this->calltime = microtime(true) - $this->calltime; 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) { /* 这里的on receivce会被触发多次,耗时和取消定时器都不在这里处理,在packRsp函数里 */ call_user_func_array(array($this, 'packRsp'), array('key' => $cli, 'data' => $data)); }); $this->callback = $callback; if ($client->connect($this->host, $this->port, $this->timeout)) { $this->calltime = microtime(true); if (floatval($this->timeout) > 0) { Timer::add($this->key, $this->timeout, $client, $callback, array('r' => 2, 'key' => $this->key, 'calltime' => $this->calltime, 'error_msg' => $this->host . ':' . $this->port . ' timeout')); } } }
protected function asyncSendAndReceive($request, $use) { $self = $this; $client = new \swoole_client($this->type, SWOOLE_SOCK_ASYNC); $buffer = ""; $len = ""; $client->on("connect", function ($cli) use($self, $request, $use) { if (!$self->send($cli, $request)) { $self->sendAndReceiveCallback('', new \Exception(socket_strerror($cli->errCode)), $use); } }); $client->on("error", function ($cli) use($self, $use) { $self->sendAndReceiveCallback('', new \Exception(socket_strerror($cli->errCode)), $use); }); $client->on("receive", function ($cli, $data) use($self, &$buffer, &$len, $use) { do { if (count($buffer) == 0 || is_string($len)) { $left = 4 - strlen($len); if (strlen($data) < $left) { $len .= $data; return; } $len .= substr($data, 0, $left); $len = unpack("N", $len); $len = $len[1]; $n = strlen($data) - $left; } else { $left = 0; $n = strlen($data); } if ($n == 0) { $buffer = ""; return; } if ($len == $n) { $response = $buffer . substr($data, $left); $buffer = ""; $len = ""; try { $self->sendAndReceiveCallback($response, null, $use); } catch (\Exception $e) { } swoole_timer_clear($cli->timer); $cli->close(); return; } if ($len > $n) { $buffer .= substr($data, $left); $len -= $n; return; } $response = $buffer . substr($data, $left, $len); $buffer = ""; $data = substr($data, $left + $len); $len = ""; try { $self->sendAndReceiveCallback($response, null, $use); } catch (\Exception $e) { } } while (true); }); $client->on("close", function ($cli) { }); $client->connect($this->host, $this->port); $client->timer = swoole_timer_after($this->timeout, function () use($client) { $client->close(); }); }
<?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->on("connect", function ($cli) { echo "connected\n"; $cli->send("hello world\n"); }); $client->on("receive", function ($cli, $data) { if (empty($data)) { $cli->close(); echo "closed\n"; } else { echo "received: {$data}\n"; sleep(1); $cli->send("hello\n"); } }); $client->on("error", function ($cli) { exit("error\n"); }); $client->on("close", function ($cli) { echo "connection is closed\n"; }); $client->connect('127.0.0.1', 9528, 0.5);
function execute() { if (empty($this->onReadyCallback)) { throw new \Exception(__CLASS__ . " require onReadyCallback"); } $cli = new \swoole_client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC); $this->cli = $cli; $cli->on('connect', array($this, 'onConnect')); $cli->on('error', array($this, 'onError')); $cli->on('Receive', array($this, 'onReceive')); $cli->on('close', array($this, 'onClose')); $cli->connect($this->uri['host'], $this->uri['port'], $this->timeout); }
<?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //TCP方式、同步 $client->on("receive", function ($cli, $data = "") { //$data = $cli->recv(); //1.6.10+ 不需要 if (empty($data)) { $cli->close(); echo "closed\n"; } else { var_dump($data); //echo "received: $data\n"; //sleep(1); //$cli->send("hello\n"); } //var_dump($cli); $cli->close(); }); $client->on("close", function ($cli) { //$cli->close(); // 1.6.10+ 不需要 echo "close\n"; }); $client->on("error", function ($cli) { exit("error\n"); }); $client->on("connect", function ($cli) { echo "connected"; $cli->send('show tables'); //执行查询 }); $client->connect('127.0.0.1', 9509);
| Aha | +----------------------------------------------------------------------+ | This source file is subject to version 2.0 of the Apache license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.apache.org/licenses/LICENSE-2.0.html | | If you did not receive a copy of the Apache2.0 license and are unable| | to obtain it through the world-wide-web, please send a note to | | yiming_6weijun@163.com so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Weijun Lu <*****@*****.**> | +----------------------------------------------------------------------+ */ $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //设置事件回调函数 $client->on("connect", function ($cli) { $arrData = array('cmd' => 'demo-server-tcp', 'body' => 'from http request'); $cli->send(json_encode($arrData) . "\r\n\r\n"); }); $client->on("receive", function ($cli, $data) { echo "Received: " . $data . "\n"; $cli->close(); }); $client->on("error", function ($cli) { echo "Connect failed\n"; }); $client->on("close", function ($cli) { echo "Connection close\n"; }); //发起网络连接 $client->connect('127.0.0.1', 9602, 0.5);
function __construct(RedisClient $redis) { $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on('connect', array($this, 'onConnect')); $client->on('error', array($this, 'onError')); $client->on('receive', array($this, 'onReceive')); $client->on('close', array($this, 'onClose')); $client->connect($redis->host, $redis->port); $this->client = $client; $redis->pool[$client->sock] = $this; $this->redis = $redis; }
global $i, $jjj, $lll; $i = 0; $jjj = 0; $lll = 0; $t0 = microtime(true); file_put_contents("log.txt", microtime(true) . ":" . $i . "\n"); function dump() { global $i, $jjj, $lll; echo "i={$i},j={$jjj},l={$lll}\n"; } do { $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function (swoole_client $cli) { $cli->send("GET / HTTP/1.1\r\n\r\n"); }); $client->on("receive", function (swoole_client $cli, $data) { $cli->close(); global $i, $jjj, $lll; //echo "Receive: $data"; $lll += strlen($data); $jjj++; file_put_contents("log.txt", "{$i},{$jjj},{$lll}\n", FILE_APPEND); //$cli->send(str_repeat('A', 100)."\n"); //sleep(1); }); $client->on("error", function (swoole_client $cli) { global $i, $jjj, $lll; file_put_contents("log.txt", "ERROR: {$i},{$jjj},{$lll}\n", FILE_APPEND); #echo "error\n";