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


PHP swoole_client::connect方法代码示例

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


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

示例1: setCenterSocket

 function setCenterSocket($ip, $port)
 {
     $this->centerSocket = new \swoole_client(SWOOLE_SOCK_UDP);
     $this->centerSocket->connect($ip, $port);
     $this->centerHost = $ip;
     $this->centerPort = $port;
 }
开发者ID:FrankWebDev,项目名称:node-agent,代码行数:7,代码来源:Node.php

示例2: connect

 public function connect($config)
 {
     $connected = $this->client->connect($config['host'], $config['port'], $config['timeout']);
     if (false == $connected) {
         throw new ConnectionException(socket_strerror($this->client->errCode), $this->client->errCode);
     }
     $this->setConnected();
 }
开发者ID:vimac,项目名称:zan,代码行数:8,代码来源:Client.php

示例3: 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

示例4: connect

 /**
  * Connect client to server
  *
  * @return $this
  */
 public function connect()
 {
     $this->socket = new \swoole_client(SWOOLE_SOCK_TCP);
     if (!$this->socket->connect($this->host, $this->port)) {
         return false;
     }
     $this->socket->send($this->createHeader());
     return $this->recv();
 }
开发者ID:viile,项目名称:swoole-src,代码行数:14,代码来源:WebSocketClient.php

示例5: connect

 function connect($host, $port, $timeout = 30)
 {
     $this->sock = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
     $this->sock->set(['open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 2000000]);
     if ($this->sock->connect($host, $port, $timeout) === false) {
         $this->errCode = $this->sock->errCode;
         return false;
     } else {
         return true;
     }
 }
开发者ID:FrankWebDev,项目名称:node-agent,代码行数:11,代码来源:Client.php

示例6: 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

示例7: 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

示例8: query

 public static function query($sql)
 {
     $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
     $client->connect('127.0.0.1', 9509, 0.5, 0);
     $client->send($sql);
     return $client->recv();
 }
开发者ID:kcloze,项目名称:ycf,代码行数:7,代码来源:ModelProxyDb.php

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

示例10: 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

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

示例12: getClientObj

 private function getClientObj()
 {
     $key = $this->getConfigObjKey();
     $clientKey = $this->serverConfig[$key]["ip"] . "_" . $this->serverConfig[$key]["port"];
     //set the current client key
     $this->currentClientKey = $clientKey;
     if (!isset(self::$client[$clientKey])) {
         $client = new \swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
         $client->set(array('open_length_check' => 1, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 1024 * 1024 * 2, 'open_tcp_nodelay' => 1));
         if (!$client->connect($this->serverConfig[$key]["ip"], $this->serverConfig[$key]["port"], self::SW_RECIVE_TIMEOUT)) {
             //connect fail
             $errorCode = $client->errCode;
             if ($errorCode == 0) {
                 $msg = "connect fail.check host dns.";
                 $errorCode = -1;
             } else {
                 $msg = socket_strerror($errorCode);
             }
             //put the fail connect config to block list
             $this->serverConfigBlock[$key] = 1;
             throw new \Exception($msg, $errorCode);
         }
         self::$client[$clientKey] = $client;
     }
     //success
     return self::$client[$clientKey];
 }
开发者ID:jeftom,项目名称:Dora-RPC,代码行数:27,代码来源:client.php

示例13: sendData

 /**
  * 发送数据
  * @param string $data
  * @return unknown
  */
 public function sendData($data)
 {
     if (empty($this->client)) {
         $this->client = new \swoole_client(SWOOLE_SOCK_TCP);
     }
     if (!$this->client->connect($this->ip, $this->port, -1)) {
         exit("connect failed. Error: {$this->client->errCode}\n");
     }
     if (\is_array($data) || \is_object($data)) {
         $data = \json_encode($data);
     }
     $data = StringUtil::encryStr($data, ApiConfig::ENCRYTP_DECRYPT_SALT);
     $this->client->send($data);
     $result = $this->client->recv();
     return StringUtil::decryStr($result, ApiConfig::ENCRYTP_DECRYPT_SALT);
 }
开发者ID:zgoubaophp,项目名称:boytemptapi,代码行数:21,代码来源:ApiClient.php

示例14: 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();
 }
开发者ID:CptSteven,项目名称:faster-rcnn-mod,代码行数:12,代码来源:train.php

示例15: 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


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