Ejemplo n.º 1
0
 function __construct($host, $port, $timeout = 0)
 {
     $this->sw = swoole_server_create($host, $port, self::$sw_mode, SWOOLE_SOCK_TCP);
     $this->host = $host;
     $this->port = $port;
     \Swoole\Error::$stop = false;
     \Swoole_js::$return = true;
     $this->swooleSetting = array('timeout' => 2.5, 'backlog' => 128, 'log_file' => '/tmp/swoole.log');
 }
Ejemplo n.º 2
0
 function detail()
 {
     session();
     if (empty($_GET['aid'])) {
         exit;
     }
     $_user = createModel('UserInfo');
     $_reply = createModel('AskReply');
     $aid = (int) $_GET['aid'];
     $ask = createModel('AskSubject')->get($aid);
     if (empty($ask->_data)) {
         Swoole\Http::header("HTTP/1.1 404 Not Found");
         return Swoole\Error::info("Page not found", "");
     }
     $ask->lcount++;
     $ask->save();
     $timeout['day'] = intval(($ask['expire'] - time()) / 86400);
     $timeout['hour'] = intval(($ask['expire'] - time() - $timeout['day'] * 86400) / 3600);
     $user = $_user->get($ask['uid'])->get();
     $content = createModel('AskContent')->get($aid)->get();
     $gets['aid'] = $aid;
     $gets['select'] = $_reply->table . '.id as id,uid,sex,best,content,nickname,avatar,addtime';
     $gets['order'] = 'best desc,' . $_reply->table . '.id asc';
     $gets['leftjoin'] = array($_user->table, $_user->table . '.id=' . $_reply->table . '.uid');
     $gets['pagesize'] = 10;
     $gets['page'] = empty($_GET['page']) ? 1 : (int) $_GET['page'];
     $replys = $_reply->gets($gets, $pager);
     $if_vote = true;
     if ($_SESSION['isLogin']) {
         $vote = $this->swoole->db->query("select count(*) as c from ask_vote where aid={$aid} and uid={$_SESSION['user_id']} limit 1")->fetch();
         if ($vote['c'] > 0) {
             $if_vote = false;
         }
     }
     $this->swoole->tpl->assign('if_vote', $if_vote);
     $this->swoole->tpl->assign('if_vote', $if_vote);
     $this->swoole->tpl->assign('expire', $timeout);
     $this->swoole->tpl->ref('user', $user);
     $this->swoole->tpl->ref('ask', $ask->get());
     $this->swoole->tpl->ref('content', $content);
     $this->swoole->tpl->ref('replys', $replys);
     if ($pager->totalpage > 1) {
         $this->swoole->tpl->ref('pager', $pager->render());
     }
     $this->swoole->tpl->display();
 }
Ejemplo n.º 3
0
/**
 * 错误信息输出处理
 */
function swoole_error_handler($errno, $errstr, $errfile, $errline)
{
    $info = '';
    switch ($errno) {
        case E_USER_ERROR:
            $level = 'User Error';
            break;
        case E_USER_WARNING:
            $level = 'Warnning';
            break;
        case E_USER_NOTICE:
            $level = 'Notice';
            break;
        default:
            $level = 'Unknow';
            break;
    }
    $title = 'Swoole ' . $level;
    $info .= '<b>File:</b> ' . $errfile . "<br />\n";
    $info .= '<b>Line:</b> ' . $errline . "<br />\n";
    $info .= '<b>Info:</b> ' . $errstr . "<br />\n";
    $info .= '<b>Code:</b> ' . $errno . "<br />\n";
    echo Swoole\Error::info($title, $info);
}
Ejemplo n.º 4
0
<?php

if (!defined('EVENT_MODE') or EVENT_MODE == 'sync') {
    $event = new SwooleEvent('sync');
} else {
    $queue_url = '';
    $queue_type = 'CacheQueue';
    if (defined('EVENT_QUEUE')) {
        $queue_url = EVENT_QUEUE;
    }
    if (defined('EVENT_QUEUE_TYPE')) {
        $queue_type = EVENT_QUEUE_TYPE;
    }
    $event = new SwooleEvent('async', $queue_url, $queue_type);
}
if (defined('EVENT_HANDLE')) {
    require EVENT_HANDLE;
    if (empty($handle)) {
        Swoole\Error::info('SwooleEvent Error', 'Event handles not be empty!');
    }
    $event->set_listens($handle);
}
return $event;
Ejemplo n.º 5
0
        $this->log("onMessage: " . $client_id . ' = ' . $ws['message']);
        $this->send($client_id, 'Server: ' . $ws['message']);
        //$this->broadcast($client_id, $ws['message']);
    }
    function broadcast($client_id, $msg)
    {
        foreach ($this->connections as $clid => $info) {
            if ($client_id != $clid) {
                $this->send($clid, $msg);
            }
        }
    }
}
//require __DIR__'/phar://swoole.phar';
Swoole\Config::$debug = true;
Swoole\Error::$echo_html = false;
$AppSvr = new WebSocket();
$AppSvr->loadSetting(__DIR__ . "/swoole.ini");
//加载配置文件
$AppSvr->setLogger(new \Swoole\Log\EchoLog(true));
//Logger
/**
 * 如果你没有安装swoole扩展,这里还可选择
 * BlockTCP 阻塞的TCP,支持windows平台
 * SelectTCP 使用select做事件循环,支持windows平台
 * EventTCP 使用libevent,需要安装libevent扩展
 */
$enable_ssl = false;
$server = Swoole\Network\Server::autoCreate('0.0.0.0', 9443, $enable_ssl);
$server->setProtocol($AppSvr);
//$server->daemonize(); //作为守护进程
Ejemplo n.º 6
0
 /**
  * 运行MVC处理模型
  * @param $url_processor
  * @return None
  */
 function runMVC()
 {
     $mvc = call_user_func($this->router_function);
     if ($mvc === false) {
         $this->http->status(404);
         return Swoole\Error::info('MVC Error', "url route fail!");
     }
     //check controller name
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['controller'])) {
         return Swoole\Error::info('MVC Error!', "controller[{$mvc['controller']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     //check view name
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['view'])) {
         return Swoole\Error::info('MVC Error!', "view[{$mvc['view']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     //check app name
     if (isset($mvc['app']) and !preg_match('/^[a-z0-9_]+$/i', $mvc['app'])) {
         return Swoole\Error::info('MVC Error!', "app[{$mvc['app']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     $this->env['mvc'] = $mvc;
     //使用命名空间,文件名必须大写
     $controller_class = '\\App\\Controller\\' . ucwords($mvc['controller']);
     $controller_path = self::$app_path . '/controllers/' . ucwords($mvc['controller']) . '.php';
     if (class_exists($controller_class, false)) {
         goto do_action;
     } else {
         if (is_file($controller_path)) {
             require_once $controller_path;
             goto do_action;
         }
     }
     //file not found
     $this->http->status(404);
     return Swoole\Error::info('MVC Error', "Controller <b>{$mvc['controller']}</b>[{$controller_path}] not exist!");
     do_action:
     //服务器模式下,尝试重载入代码
     if (defined('SWOOLE_SERVER')) {
         $this->reloadController($mvc, $controller_path);
     }
     $controller = new $controller_class($this);
     if (!is_callable(array($controller, $mvc['view']))) {
         $this->http->status(404);
         return Swoole\Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!");
     }
     if (empty($mvc['param'])) {
         $param = null;
     } else {
         $param = $mvc['param'];
     }
     $method = $mvc['view'];
     //doAction
     $return = $controller->{$method}($param);
     //保存Session
     if (defined('SWOOLE_SERVER') and $this->session->open and $this->session->readonly === false) {
         $this->session->save();
     }
     //响应请求
     if (!empty($controller->is_ajax)) {
         $this->http->header('Cache-Control', 'no-cache, must-revalidate');
         $this->http->header('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
         $this->http->header('Content-Type', 'application/json');
         $return = json_encode($return);
     }
     if (defined('SWOOLE_SERVER')) {
         return $return;
     } else {
         echo $return;
     }
 }
Ejemplo n.º 7
0
<?php

require __DIR__ . '/config.php';
Swoole\Error::$echo_html = true;
$php->runMVC();
Ejemplo n.º 8
0
 /**
  * 运行MVC处理模型
  * @param $url_processor
  * @return None
  */
 function runMVC()
 {
     $mvc = $this->urlRoute();
     if ($mvc === false) {
         \Swoole\Http::status(404);
         return Swoole\Error::info('MVC Error', "url route fail!");
     }
     //check
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['controller'])) {
         return Swoole\Error::info('MVC Error!', "controller[{$mvc['controller']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['view'])) {
         return Swoole\Error::info('MVC Error!', "view[{$mvc['view']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     if (isset($mvc['app']) and !preg_match('/^[a-z0-9_]+$/i', $mvc['app'])) {
         return Swoole\Error::info('MVC Error!', "app[{$mvc['app']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     $this->env['mvc'] = $mvc;
     $controller_path = self::$app_path . "/controllers/{$mvc['controller']}.php";
     if (!class_exists($mvc['controller'], false)) {
         if (!is_file($controller_path)) {
             \Swoole\Http::status(404);
             return Swoole\Error::info('MVC Error', "Controller <b>{$mvc['controller']}</b> not exist!");
         } else {
             require_once $controller_path;
         }
     }
     //服务器模式下,尝试重载入代码
     if (defined('SWOOLE_SERVER')) {
         $this->reloadController($mvc, $controller_path);
     }
     $class = $mvc['controller'];
     $controller = new $class($this);
     if (!is_callable(array($controller, $mvc['view']))) {
         \Swoole\Http::status(404);
         return Swoole\Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!");
     }
     if (empty($mvc['param'])) {
         $param = null;
     } else {
         $param = $mvc['param'];
     }
     $method = $mvc['view'];
     $return = $controller->{$method}($param);
     //保存Session
     if ($this->session->open and $this->session->readonly === false) {
         $this->session->save();
     }
     //响应请求
     if ($controller->is_ajax) {
         header('Cache-Control: no-cache, must-revalidate');
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         header('Content-type: application/json');
         $return = json_encode($return);
     }
     if (defined('SWOOLE_SERVER')) {
         return $return;
     } else {
         echo $return;
     }
 }
Ejemplo n.º 9
0
<?php

require __DIR__ . '/../config.php';
require 'check.php';
require_once 'func.php';
$apps = getApps();
$php->tpl->assign("apps", $apps);
if (isset($_GET['debug'])) {
    Swoole\Error::sessd();
}
if (isset($_GET['page'])) {
    $php->tpl->display("admin_index_{$_GET['page']}.html");
} else {
    $php->tpl->display('admin_index.html');
}