当前位置: 首页>>代码示例>>PHP>>正文


PHP swoole_client::on方法代码示例

本文整理汇总了PHP中swoole_client::on方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_client::on方法的具体用法?PHP swoole_client::on怎么用?PHP swoole_client::on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在swoole_client的用法示例。


在下文中一共展示了swoole_client::on方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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'));
                 }
             });
         }
     }
 }
开发者ID:learsu,项目名称:php-swoole-framework,代码行数:27,代码来源:TcpTestClient.php

示例2: send

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);
}
开发者ID:jinguanio,项目名称:david,代码行数:26,代码来源:multi_cli.php

示例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);
     }
 }
开发者ID:xxoxx,项目名称:gitswoolestudy,代码行数:26,代码来源:proxy.php

示例4: 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'));
             });
         }
     }
 }
开发者ID:RamboLau,项目名称:tsf,代码行数:29,代码来源:TCP.php

示例5: sendData

 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');
         //         }
         //     });
         // }
     }
 }
开发者ID:learsu,项目名称:php-swoole-framework,代码行数:32,代码来源:testTcpServ.php

示例6: 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;
 }
开发者ID:lanlin,项目名称:codeigniter-swoole,代码行数:34,代码来源:Client.php

示例7: createBackend

 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;
 }
开发者ID:hackers365,项目名称:swoole_proxy,代码行数:25,代码来源:proxy.php

示例8: 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'));
                 }
             });
         }
     }
 }
开发者ID:learsu,项目名称:php-swoole-framework,代码行数:32,代码来源:UdpTestClient.php

示例9: 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']);
             }
         });
     }
 }
开发者ID:zhanglei,项目名称:swPromise,代码行数:58,代码来源:HttpClientFuture.class.php

示例10: connect

 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;
 }
开发者ID:jinchunguang,项目名称:swoole-doc,代码行数:12,代码来源:WebSocketClient.php

示例11: create

 /**
  * 创建一个新的客户端
  */
 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 . "\$");
 }
开发者ID:swoole,项目名称:tests,代码行数:16,代码来源:async_client.php

示例12: send

 /**
  * 数据发送
  *
  * @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);
 }
开发者ID:kerisy,项目名称:framework,代码行数:20,代码来源:Client.php

示例13: addServerClient

 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;
 }
开发者ID:qieangel2013,项目名称:zys,代码行数:15,代码来源:DistributedClient.php

示例14: connect

 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;
 }
开发者ID:jinguanio,项目名称:swoolecrawler,代码行数:18,代码来源:reconnect_client.php

示例15: request

 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);
     });
 }
开发者ID:phpdn,项目名称:zphp,代码行数:44,代码来源:AsyncHttpClient.php


注:本文中的swoole_client::on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。