本文整理汇总了PHP中swoole_timer_after函数的典型用法代码示例。如果您正苦于以下问题:PHP swoole_timer_after函数的具体用法?PHP swoole_timer_after怎么用?PHP swoole_timer_after使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了swoole_timer_after函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: send
public function send($request, $future, $context, $conn)
{
$self = $this;
$timeout = $context->timeout;
if ($timeout > 0) {
$conn->timeoutId = swoole_timer_after($timeout, function () use($self, $future, $conn) {
$future->reject(new TimeoutException('timeout'));
if ($conn->isConnected()) {
$conn->close();
}
});
}
$conn->onreceive = function ($conn, $data) use($self, $future) {
$self->clean($conn);
$self->sendNext($conn);
$future->resolve($data);
};
$conn->onclose = function ($conn) use($self, $future) {
$self->clean($conn);
if ($conn->errCode !== 0) {
$future->reject(new Exception(socket_strerror($conn->errCode)));
} else {
$future->reject(new Exception('The server is closed.'));
}
$self->size--;
};
$header = pack('N', strlen($request));
$conn->send($header);
$conn->send($request);
}
示例2: timeout
function timeout($tm)
{
echo time() . ": Timeout #{$tm}\n";
if ($tm == 5) {
swoole_timer_after(3000, 'timeout', 7);
}
}
示例3: 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'));
});
}
}
}
示例4: 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'));
}
});
}
}
}
示例5: 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'));
}
});
}
}
}
示例6: init
/**
* [init 启动定时器]
* @return [type] [description]
*/
public static function init()
{
if (!self::$isOnTimer) {
swoole_timer_after(1000 * self::LOOPTIME, function () {
//循环数组,踢出超时情况
self::loop();
self::$isOnTimer = false;
});
self::$isOnTimer = true;
}
}
示例7: __construct
public function __construct()
{
$fp = stream_socket_client("tcp://127.0.0.1:9504", $code, $msg, 3);
$http_request = "GET /index.html HTTP/1.1\r\n\r\n";
fwrite($fp, $http_request);
swoole_event_add($fp, function ($fp) {
echo fread($fp, 8192);
swoole_event_del($fp);
fclose($fp);
});
swoole_timer_after(2000, function () {
echo "2000ms timeout\n";
});
swoole_timer_tick(1000, function () {
echo "1000ms interval\n";
});
}
示例8: 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->timer = swoole_timer_after($this->timeout, [$this, 'onConnectTimeout']);
$this->swoole_client = $client;
}
示例9: __construct
/**
* @param $serverPid
* @throws NotFound
*/
function __construct($serverPid)
{
$this->pid = $serverPid;
if (posix_kill($serverPid, 0) === false) {
throw new NotFound("Process#{$serverPid} not found.");
}
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
swoole_event_add($this->inotify, function ($ifd) {
$events = inotify_read($this->inotify);
if (!$events) {
return;
}
var_dump($events);
foreach ($events as $ev) {
if ($ev['mask'] == IN_IGNORED) {
continue;
} else {
if ($ev['mask'] == IN_CREATE or $ev['mask'] == IN_DELETE or $ev['mask'] == IN_MODIFY or $ev['mask'] == IN_MOVED_TO or $ev['mask'] == IN_MOVED_FROM) {
$fileType = strstr($ev['name'], '.');
//非重启类型
if (!isset($this->reloadFileTypes[$fileType])) {
continue;
}
}
}
//正在reload,不再接受任何事件,冻结10秒
if (!$this->reloading) {
$this->putLog("after 10 seconds reload the server");
//有事件发生了,进行重启
swoole_timer_after($this->afterNSeconds * 1000, array($this, 'reload'));
$this->reloading = true;
}
}
});
}
示例10: wait
protected function wait($interval, $callback)
{
$future = new Future();
swoole_timer_after($interval * 1000, function () use($future, $callback) {
Future\sync($callback)->fill($future);
});
return $future;
}
示例11: swoole_client
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
//异步非阻塞
$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
$client->_count = 0;
$client->on("connect", function (swoole_client $cli) {
swoole_timer_clear($cli->timer);
$cli->send("GET / HTTP/1.1\r\n\r\n");
//$cli->sendfile(__DIR__.'/test.txt');
//$cli->_count = 0;
});
$client->on("receive", function (swoole_client $cli, $data) {
echo "Receive: {$data}";
$cli->_count++;
if ($cli->_count > 10) {
$cli->close();
return;
}
$cli->send(str_repeat('A', 100) . "\n");
});
$client->on("error", function (swoole_client $cli) {
echo "error\n";
});
$client->on("close", function (swoole_client $cli) {
echo "Connection close\n";
});
$client->connect('127.0.0.1', 9501);
$client->timer = swoole_timer_after(1000, function () use($client) {
echo "socket timeout\n";
$client->close();
});
echo "connect to 127.0.0.1:9501\n";
示例12: Server
<?php
require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
use Hprose\Future;
$server = new Server("http://0.0.0.0:1315");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->addFunction(function ($a, $b) use($server) {
$promise = new Future();
swoole_timer_after(1000, function () use($a, $b, $promise) {
$promise->resolve($a + $b);
});
return $promise;
}, "sum");
$server->start();
示例13: connect
/**
* 连接一个RPC服务器
*
* @param $ip
* @param $port
* @return bool
*/
public function connect($ip, $port)
{
if ($this->__client) {
@$this->__client->close();
$this->__client = null;
}
/**
* @var RPC $rpc
*/
$this->__ip = $ip;
$this->__port = $port;
$this->__closeByServer = false;
$rpc = $this->__rpc;
$key = $rpc::_getRpcKey();
$client = new \Swoole\Client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC);
$client->on('receive', function ($client, $data) use($key) {
$arr = explode(Server::$EOF, $data);
foreach ($arr as $item) {
if ($item === '') {
continue;
}
$tmp = @msgpack_unpack($item);
if ($key) {
$tmp = Server::decryption($tmp, $key);
if (!$tmp) {
\MyQEE\Server\Server::$instance->warn('rpc decryption data fail. data: ' . $item);
continue;
}
}
switch ($tmp->type) {
case 'on':
$event = $tmp->event;
if (isset($this->__events[$event])) {
# 回调执行
call_user_func_array($this->__events[$event], $tmp->args);
} else {
\MyQEE\Server\Server::$instance->warn("unknown rpc {$this->__rpc} event: {$event}");
}
break;
case 'close':
$this->__closeByServer = true;
$this->__client = null;
break;
default:
\MyQEE\Server\Server::$instance->warn("unknown rpc type {$tmp->type}");
break;
}
}
});
$client->on('connect', function ($client) {
if (isset($this->__events['connect'])) {
# 回调自定义的事件
call_user_func($this->__events['connect'], $client);
}
});
$client->on('close', function ($client) {
$this->__client = null;
\MyQEE\Server\Server::$instance->warn("rpc connection closed, {$this->__ip}:{$this->__port}.");
if (!$this->isClosedByServer()) {
# 不是被服务器强制关闭的则自动重新连接
$this->reconnect();
}
if (isset($this->__events['close'])) {
# 回调自定义的事件
call_user_func($this->__events['close'], $client);
}
});
$client->on('error', function ($client) {
$this->__client = null;
\MyQEE\Server\Server::$instance->warn("rpc connection({$this->__ip}:{$this->__port}) error: " . socket_strerror($client->errCode));
# 遇到错误则自动重连
swoole_timer_after(3000, function () {
$this->reconnect();
});
if (isset($this->__events['error'])) {
# 回调自定义的事件
call_user_func($this->__events['error'], $client);
}
});
$this->__client = $client;
# 发心跳包
swoole_timer_tick(1000 * 60 * 5, function () {
if ($this->__client && $this->__client->isConnected()) {
$this->__client->send("" . Server::$EOF);
}
});
$this->__client->connect($ip, $port);
return true;
}
示例14: timeout
<?php
function timeout($tm)
{
echo time() . " Timeout #{$tm}\n";
if ($tm == 3) {
global $timer4;
swoole_timer_clear($timer4);
}
}
$timer1 = swoole_timer_after(1000, function ($id) {
echo "hello world";
global $timer1;
swoole_timer_clear($timer1);
}, 1);
$timer2 = swoole_timer_after(2000, 'timeout', 2);
$timer3 = swoole_timer_after(4000, 'timeout', 3);
$timer4 = swoole_timer_after(8000, 'timeout', 4);
$timer5 = swoole_timer_after(10000, 'timeout', 5);
swoole_process::signal(SIGTERM, function () {
swoole_event_exit();
});
var_dump($timer1, $timer2, $timer3, $timer4, $timer5);
示例15: asyncHello
function asyncHello($name, $callback)
{
swoole_timer_after(3000, function () use($name, $callback) {
$callback("Hello async {$name}!");
});
}