本文整理汇总了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];
}
示例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();
}
示例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();
}
示例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;
}
示例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;
}
示例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();
}
示例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";
}
示例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);
}
示例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");
}
示例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;
}
示例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;
}
示例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));
}
}
示例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 . "\$");
}
示例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类析构时会自动关闭连接
}
示例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'));
}
});
}
}
}