當前位置: 首頁>>代碼示例>>PHP>>正文


PHP swoole_client類代碼示例

本文整理匯總了PHP中swoole_client的典型用法代碼示例。如果您正苦於以下問題:PHP swoole_client類的具體用法?PHP swoole_client怎麽用?PHP swoole_client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了swoole_client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

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

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

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

示例4: getClient

function getClient()
{
    $client = new swoole_client(SWOOLE_SOCK_TCP);
    if (!$client->connect('127.0.0.1', 9501, -1)) {
        exit("connect failed. Error: {$client->errCode}\n");
    }
    $res = $client->getSocket();
    return $client;
}
開發者ID:noikiy,項目名稱:swoole-src,代碼行數:9,代碼來源:get_socket.php

示例5: 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;
 }
開發者ID:zhangjingpu,項目名稱:yaf-lib,代碼行數:9,代碼來源:Swoole.php

示例6: __construct

 public function __construct()
 {
     $client = new swoole_client(SWOOLE_SOCK_UDP);
     //默認是同步,第二個參數可以選填異步
     //發起網絡連接
     $client->connect('0.0.0.0', 9504, 0.5);
     $client->send('demo');
     echo $client->recv();
 }
開發者ID:jhomephper,項目名稱:phalcon_swoole,代碼行數:9,代碼來源:UdpSocketClient.php

示例7: send

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";
}
開發者ID:pangudashu,項目名稱:swoole-src,代碼行數:9,代碼來源:async_client.php

示例8: onReceive

 function onReceive($serv, $fd, $from_id, $data)
 {
     $socket = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
     if ($socket->connect('127.0.0.1', 8002, 0.5)) {
         $socket->send($data);
         $serv->send($fd, $socket->recv(8192, 0));
     }
     //unset($socket);
     $serv->close($fd);
 }
開發者ID:jinguanio,項目名稱:david,代碼行數:10,代碼來源:proxy_sync.php

示例9: 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");
}
開發者ID:liangkwok,項目名稱:Swoole,代碼行數:12,代碼來源:coroutine.php

示例10: sendToServer

function sendToServer($str)
{
    global $server;
    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
    if (!$client->connect($server['ip'], $server['port'], -1)) {
        exit("connect failed. Error: {$client->errCode}\n");
    }
    $client->send($str);
    $str = $client->recv();
    $client->close();
    return $str;
}
開發者ID:dormscript,項目名稱:dataTransfer,代碼行數:12,代碼來源:handleQueue.php

示例11: multiRequest

/**
 * 批量請求
 * @param array $request_buffer_array ['ip:port'=>req_buf, 'ip:port'=>req_buf, ...]
 * @return multitype:unknown string
 */
function multiRequest($request_buffer_array)
{
    \Statistics\Lib\Cache::$lastSuccessIpArray = array();
    $client_array = $sock_to_ip = $ip_list = array();
    foreach ($request_buffer_array as $address => $buffer) {
        list($ip, $port) = explode(':', $address);
        $ip_list[$ip] = $ip;
        $client = new swoole_client(SWOOLE_TCP | SWOOLE_KEEP, SWOOLE_SOCK_SYNC);
        $client->connect($ip, $port);
        if (!$client) {
            continue;
        }
        $client_array[$address] = $client;
        $client_array[$address]->send(encode($buffer));
        $sock_to_address[(int) $client->sock] = $address;
    }
    $read = $client_array;
    $write = $except = $read_buffer = array();
    $time_start = microtime(true);
    $timeout = 0.99;
    // 輪詢處理數據
    while (count($read) > 0) {
        foreach ($read as $client) {
            $address = $sock_to_address[(int) $client->sock];
            $buf = $client->recv();
            if (!$buf) {
                unset($client_array[$address]);
                continue;
            }
            if (!isset($read_buffer[$address])) {
                $read_buffer[$address] = $buf;
            } else {
                $read_buffer[$address] .= $buf;
            }
            // 數據接收完畢
            if (($len = strlen($read_buffer[$address])) && $read_buffer[$address][$len - 1] === "\n") {
                unset($client_array[$address]);
            }
        }
        // 超時了
        if (microtime(true) - $time_start > $timeout) {
            break;
        }
        $read = $client_array;
    }
    foreach ($read_buffer as $address => $buf) {
        list($ip, $port) = explode(':', $address);
        \Statistics\Lib\Cache::$lastSuccessIpArray[$ip] = $ip;
    }
    \Statistics\Lib\Cache::$lastFailedIpArray = array_diff($ip_list, \Statistics\Lib\Cache::$lastSuccessIpArray);
    ksort($read_buffer);
    return $read_buffer;
}
開發者ID:smalleyes,項目名稱:statistics,代碼行數:58,代碼來源:functions.php

示例12: send_chunk

/**
 * 分段發送數據
 *
 * @param swoole_client $client
 * @param string        $data
 * @param int           $chunk_size
 */
function send_chunk(swoole_client $client, $data, $chunk_size = 1024)
{
    $len = strlen($data);
    $chunk_num = intval($len / $chunk_size) + 1;
    for ($i = 0; $i < $chunk_num; $i++) {
        if ($len < ($i + 1) * $chunk_size) {
            $sendn = $len - $i * $chunk_size;
        } else {
            $sendn = $chunk_size;
        }
        $client->send(substr($data, $i * $chunk_size, $sendn));
    }
}
開發者ID:sophia2152,項目名稱:swoole-src,代碼行數:20,代碼來源:client.php

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

示例14: dbcp_query

function dbcp_query($sql)
{
    $link = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
    //TCP方式、同步
    $link->connect('127.0.0.1', 55151);
    //連接
    $link->send($sql);
    //執行查詢
    die(var_dump($link->recv()));
    return unserialize($link->recv());
    //這行會報錯,注釋掉了:PHP Notice:  unserialize(): Error at offset 0 of 292 bytes in /data/htdocs/mysql.swoole.com/mysqlSwooleCli.php on line 6
    //swoole_client類析構時會自動關閉連接
}
開發者ID:suhanyujie,項目名稱:digitalOceanVps,代碼行數:13,代碼來源:mysqlClient.php

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


注:本文中的swoole_client類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。