Exemple #1
0
 /**
  * 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 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'));
                 }
             });
         }
     }
 }
 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 __destruct()
 {
     Trace::debug("*********cli {$this->fd} __destruct");
     if (isset($this->cli)) {
         $this->cli->isConnected() && $this->cli->close();
         unset($this->cli);
     }
     unset($this->queue);
 }
Exemple #5
0
 public static function init()
 {
     if (!self::$client) {
         $client = new swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
         if (!$client->isConnected()) {
             $client->set(array('open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 2000000));
             if (!$client->connect('127.0.0.1', 8996, 1)) {
                 //                    throw new MyException('client connect timeout', ERROR::CONNECTION_TIMEOUT);
                 return false;
             }
         }
         self::$client = $client;
     }
     return self::$client;
 }
 /**
  * 执行redis指令
  * @param $cmd
  * @param $callback
  */
 function command($cmd, $callback)
 {
     /**
      * 如果已经连接,直接发送数据
      */
     if ($this->client->isConnected()) {
         $this->client->send($cmd);
     } else {
         $this->wait_send = $cmd;
     }
     $this->callback = $callback;
     //从空闲连接池中移除,避免被其他任务使用
     $this->redis->lockConnection($this->client->sock);
 }
Exemple #7
0
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
//同步阻塞
if (!$client->connect('127.0.0.1', 9501, -1)) {
    exit("connect failed. Error: {$client->errCode}\n");
}
for ($i = 0; $i < 100; $i++) {
    //if ($client->sendfile(__DIR__.'/test.txt') === false)
    if ($client->send(str_repeat("A", 8000)) === false) {
        echo "send failed. Error: {$client->errCode}\n";
        break;
    }
    usleep(20000);
}
sleep(10000);
//if ($client->sendfile(__DIR__.'/test.txt') === false)
if ($client->send(str_repeat("A", 600)) === false) {
    echo "send failed. Error: {$client->errCode}\n";
    break;
}
$data = $client->recv(7000);
if ($data === false) {
    echo "recv failed. Error: {$client->errCode}\n";
    break;
}
var_dump($client->isConnected());
//var_dump($data);
//$data = $client->recv(7000);
var_dump($data);
$client->close();
var_dump($client->isConnected());
Exemple #8
0
 public function isConnected()
 {
     if (!$this->client->isConnected()) {
         $this->setConnected(false);
     }
 }
Exemple #9
0
 /**
  * @brief 发生错误时的回调
  * @param \swoole_client $client
  */
 public function onError(\swoole_client $client)
 {
     $error = array('errno' => \Aha\Network\Client::ERR_UNEXPECT, 'errmsg' => array('errCode' => $client->errCode, 'error' => socket_strerror($client->errCode)), 'package' => $this->_package);
     echo "Redis onError![error]" . serialize($error) . PHP_EOL;
     $callback = $this->_callback;
     $arguments = $this->_arguments;
     $this->_free();
     if ($client->isConnected()) {
         $client->close();
     }
     try {
         call_user_func($callback, false, $arguments['callback'], $this, 'Redis onError');
     } catch (\Exception $ex) {
         echo "Redis onError callback![exception]" . $ex->getMessage() . PHP_EOL;
     }
 }
Exemple #10
0
 /**
  * @brief 发生错误时的回调
  * @param \swoole_client $client
  */
 public function onError(\swoole_client $client)
 {
     $response = array('errno' => \Aha\Network\Client::ERR_UNEXPECT, 'errmsg' => array('errCode' => $client->errCode, 'error' => socket_strerror($client->errCode)), 'requestId' => $this->_requestId, 'const' => microtime(true) - $this->_const, 'data' => array());
     if (is_callable($this->_callback)) {
         try {
             call_user_func($this->_callback, $response);
         } catch (\Exception $ex) {
             echo "Client onConnect send callback failed![exception]" . $ex->getMessage() . PHP_EOL;
         }
     }
     if ($client->sock && $client->isConnected()) {
         $client->close();
     } else {
         $this->_free();
     }
 }