本文整理汇总了PHP中swoole_server::close方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::close方法的具体用法?PHP swoole_server::close怎么用?PHP swoole_server::close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: unpack
function onReceive_unpack_php($serv, $fd, $from_id, $data)
{
if (empty($this->buffer[$fd])) {
$this->buffer[$fd] = '';
$this->length[$fd] = 0;
}
$this->buffer[$fd] .= $data;
$buffer =& $this->buffer[$fd];
do {
if ($this->length[$fd] === 0) {
$n = unpack('Nlen', substr($buffer, 0, 4));
$this->length[$fd] = $n['len'];
if ($n['len'] > self::MAX_PACKAGE_LEN) {
$this->serv->close($fd);
return;
}
}
if (strlen($buffer) >= $this->length[$fd]) {
$this->onPackage($fd, substr($buffer, 0, $this->length[$fd]));
$buffer = substr($buffer, $this->length[$fd]);
$this->length[$fd] = 0;
} else {
break;
}
} while (strlen($buffer) > 0);
}
示例2: message
function message($fd, $code, $msg)
{
$this->serv->send($fd, json_encode(array('code' => $code, 'msg' => $msg)));
echo "[-->{$fd}]\t{$code}\t{$msg}\n";
if ($code != 0) {
$this->serv->close($fd);
}
return true;
}
示例3: sendResult
/**
* 发送回应
* @param $fd
* @param $code
* @param $msg
* @return bool
*/
protected function sendResult($fd, $code, $msg)
{
$this->serv->send($fd, $this->pack(array('code' => $code, 'msg' => $msg)));
//打印日志
if (is_string($msg) and strlen($msg) < 128) {
echo "[-->{$fd}]\t{$code}\t{$msg}\n";
}
//错误时自动关闭连接
if ($code != 0) {
$this->serv->close($fd);
}
return true;
}
示例4: onReceive
function onReceive($serv, $fd, $from_id, $data)
{
if (!isset($this->clients[$fd])) {
$this->serv->send($fd, "backend server not connected. please try reconnect.");
$this->serv->close($fd);
} else {
echo microtime() . ": client receive\n";
$backend_socket = $this->clients[$fd]['socket'];
$backend_socket->send($data);
echo microtime() . ": send to backend\n";
echo str_repeat('-', 100) . "\n";
}
}
示例5: on_receive
/**
* listen on receive data
*
* @param \swoole_server $serv
* @param $fd
* @param $from_id
* @param $data
*/
public function on_receive(\swoole_server $serv, $fd, $from_id, $data)
{
// format passed
$data = str_replace(self::EOFF, '', $data);
$data = unserialize($data);
// check is signal for server shutdown
if (!empty($data['shutdown']) || !empty($data['reload'])) {
// try to clear timers
$this->__clear_timer();
$send = !empty($data['shutdown']) ? $serv->shutdown() : $serv->reload();
$send = serialize($send);
$send .= self::EOFF;
$ends = $serv->send($fd, $send);
if ($ends) {
$serv->close($fd);
}
return;
}
// dont send back
if (!empty($data['return']) && $data['return'] === FALSE) {
$back = serialize(TRUE);
$back .= self::EOFF;
$serv->send($fd, $back);
$serv->close($fd);
}
// now post data to a task
$param = ['fd' => $fd, 'data' => $data];
$param = serialize($param);
$param .= self::EOFF;
// worker direct
$this->on_task($serv, NULL, NULL, $param);
return;
}
示例6: onReceive
/**
* @param \swoole_server $serv
* @param $fd
* @param $from_id
* @param $data
*/
function onReceive($serv, $fd, $from_id, $data)
{
$parser = new \HttpParser();
if (empty($this->buffers[$fd])) {
$this->clearBuffer($fd);
}
$this->buffers[$fd] .= $data;
$buffer =& $this->buffers[$fd];
$nparsed =& $this->nparsed[$fd];
$nparsed = $parser->execute($buffer, $nparsed);
if ($parser->hasError()) {
$serv->close($fd, $from_id);
} else {
if ($parser->isFinished()) {
$this->clearBuffer($fd);
$_SERVER = $parser->getEnvironment();
$_GET = array();
if (!empty($_SERVER['QUERY_STRING'])) {
parse_str($_SERVER['QUERY_STRING'], $_GET);
}
if (!empty($_SERVER['REQUEST_BODY'])) {
parse_str($_SERVER['REQUEST_BODY'], $_POST);
}
if (!empty($_SERVER['HTTP_COOKIE'])) {
$_COOKIE = self::parseCookie($_SERVER['HTTP_COOKIE']);
}
$this->currentFd = $fd;
call_user_func($this->_onRequest, $this);
}
}
}
示例7: onReceive
function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$this->count[$fd]++;
echo "Client#{$fd} recv: {$data}\n";
if ($this->count[$fd] > 10) {
$serv->close($fd);
} else {
$serv->send($fd, "hello client");
}
}
示例8: __construct
public function __construct()
{
$serv = new swoole_server("127.0.0.1", 9502);
$serv->set(array('worker_num' => 16, 'daemonize' => true));
$serv->on('connect', function ($serv, $fd) {
echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, $data);
$serv->close($fd);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();
}
示例9: serverAction
public function serverAction()
{
$server = new swoole_server("192.168.80.140", 9021, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$setting = array('daemonize' => 0, 'reactor_num' => 2, 'worker_num' => 4, 'max_request' => 2000, 'backlog' => 128, 'open_cpu_affinity' => 1, 'open_tcp_nodelay' => 1, 'log_file' => '../log/swoole_test.log', 'heartbeat_check_interval' => 30, 'heartbeat_idle_time' => 60, 'dispatch_mode' => 2);
$server->set($setting);
var_dump($server->setting);
$server->on("connect", function ($server, $fd) {
#echo "connect\n";
});
$server->on("receive", function ($server, $fd, $from_id, $data) {
$server->send($fd, 'Swoole: ' . $data);
$server->close($fd);
});
$server->on("close", function ($server, $fd) {
#echo "close\n";
#$server->shutdown();
});
$server->start();
return false;
}
示例10: function
<?php
$serv = new swoole_server("0.0.0.0", 9509);
$serv->set(array('worker_num' => 1));
$serv->on('workerStart', function ($serv, $worker_id) {
//if($worker_id == 0) $serv->addtimer(500);
});
$serv->on('connect', function ($serv, $fd, $from_id) {
$serv->array['fd'] =& strval($fd);
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
//echo "[#".posix_getpid()."]\tClient[$fd]: $data\n";
$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
$data = '';
$n_bytes = 0;
for ($i = 0; $i < 10; $i++) {
$_str = str_repeat($array[$i % 7], 4030) . "\n";
//$serv->send($fd, $_str);
$n_bytes += strlen($_str);
$data .= $_str;
}
echo "send " . $n_bytes . " bytes\n";
$serv->send($serv->array['fd'], $data);
$serv->close($fd);
});
$serv->on('close', function ($serv, $fd, $from_id) {
echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Close.\n";
});
$serv->start();
示例11: onError
public function onError(swoole_client $cli)
{
Trace::debug("*********cli {$this->fd} error");
$this->serv->close($this->fd);
$cli->close();
}
示例12: onReceive
/**
* 接收到数据时回调此函数,发生在worker进程中
*
* @param $serv
* @param $fd
* @param $from_id
* @param $data
* @author yangyifan <yangyifanphp@gmail.com>
*/
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$data = json_decode($data, true);
if ($data['step'] == 'task') {
//开始 task
$params = ['fd' => $fd, 'targer' => $data['targer'], 'params' => $data['params'], 'callback' => $data['callback']];
$serv->task($params);
} else {
if ($data['step'] == 'default') {
$send_data = '来至' . $from_id . ':' . $data['params']['data'] . $this->swoole_config['package_eof'];
echo $send_data;
//$this->swoole_server->send($fd, $send_data);
}
}
$serv->close($fd);
}
示例13: onReceive
public function onReceive(\swoole_server $server, $fd, $from_id, $data)
{
$data = trim($data);
if ($data == 'Stop') {
//停止
$server->shutdown();
$server->send($fd, 'success', $from_id);
} else {
if ($data == 'Stats') {
//状态
$data = $server->stats();
$server->send($fd, json_encode($data), $from_id);
} else {
if ($data == 'Reload') {
//重启
$server->reload();
$server->send($fd, 'success', $from_id);
} else {
if ($data == 'ResetTelnet') {
//重置所有telnet链接
} else {
$data = json_decode($data, true);
if ($data['act'] == 'Telnet' && isset($data['ip']) && isset($data['cmd'])) {
//执行命令
$service = TelnetEvent::getService($data['ip']);
if ($service == null) {
$result['code'] = 2;
} else {
if ($data['cmd'] > 0) {
if (isset($data['arg'])) {
$result['data'] = $service->exec($data['cmd'], $data['arg']);
} else {
$result['data'] = $service->exec($data['cmd']);
}
if ($result == null || $result['data'] == null) {
$result = ['code' => 3];
} else {
$result['code'] = 1;
}
} else {
$service->getSwitch()->connect();
if ($service->getSwitch()->isConnect()) {
$result['code'] = 1;
} else {
$result['code'] = 3;
}
}
}
$server->send($fd, json_encode($result), $from_id);
} else {
$server->send($fd, 'unknown command!', $from_id);
}
}
}
}
}
$server->close($fd);
}
示例14: array
<?php
/**
* Created by PhpStorm.
* User: zhaoquan
* Date: 15-5-29
* Time: 下午2:59
*/
$server = new swoole_server("127.0.0.1", 9021, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$setting = array('daemonize' => 0, 'reactor_num' => 2, 'worker_num' => 4, 'max_request' => 2000, 'backlog' => 128, 'open_cpu_affinity' => 1, 'open_tcp_nodelay' => 1, 'log_file' => '../log/swoole_test.log', 'heartbeat_check_interval' => 30, 'heartbeat_idle_time' => 60, 'dispatch_mode' => 2);
$server->set($setting);
var_dump($server->setting);
$server->on("connect", function ($server, $fd) {
echo "connect\n";
});
$server->on("receive", function ($server, $fd, $from_id, $data) {
$server->send($fd, 'Swoole: ' . $data);
$server->close($fd);
});
$server->on("close", function ($server, $fd) {
echo "close\n";
$server->shutdown();
});
$server->start();
示例15: close
/**
* @param $client_id
* @return bool
*/
public function close($client_id)
{
return $this->server->close($client_id);
}