设置定时器,用于用户重新连接,避免频繁广播,但是运行后时好时坏

muyu
<?php
namespace Core;
use Core\Site;
use \GatewayWorker\Lib\Gateway;
use Core\RCache;
use Workerman\Lib\Timer;

class Toos extends Site{

    //当前连接的所有用户 封装  后续并发多了采用nosql存储  例如memcache redis

    private static $_redis = array(
        "host"  =>  "127.0.0.1",
        "port"  =>  6379,
    );

    //实例对象
    private static $Instance    = null;
    //私有  禁止外部实例化 防止数据分散
    protected function __construct(){}
    protected function __clone(){}
    /**
     * 取得类实例
     * @static
     * @access public
     * @return object
     */
    public static function getInstance() {
        if(!(self::$Instance instanceof  self)){
            self::$Instance = new self;
        }
        return self::$Instance;
    }
    /**
     * 获取一个用户
     * @static
     * @access public
     * @param  Int      $id
     * @return array || null
     */
    public static function getUserOne($uid = 0){
        $Redis = Redis::getInstance(self::$_redis);
        return @unserialize($Redis->get($uid));
    }
    /**
     * 设置一个用户
     * @static
     * @access public
     * @param Int       $id
     * @return void
     */
    public static function setUserOne($uid = 0 , $user = array()){
        $Redis = Redis::getInstance(self::$_redis);
        $Redis->set($uid,serialize($user));
    }
    /**
     * 清除一个用户
     * @static
     * @access public
     * @param Int       $id
     * @return void
     */
    public static function delUserOne($uid = 0){
        $Redis = Redis::getInstance(self::$_redis);
        $Redis->del($uid);
    }
    /**
     * 用户登陆,初始化信息.
     *
     * @static
     * @access public
     * @param  Int      $id
     * @param  String   $client_id
     * @return aray
     */
    public static function login($id = 0 , $client_id = ''){
        //检测是否是重新连接  包括刷新
        $user = self::clientIsReconnect($id);
        //设置session
        empty($user) && $user = time();
        $user = $id;
        Gateway::setSession($client_id, $user);
        self::setUserOne($id , array('client'=>$client_id));
        Gateway::bindUid($client_id,$id);//绑定此用户与当前客户端的id
        return self::createResponse("user login successed!" , "login" , 200);
    }
    /**
     *  当前用户是否是重新链接的
     *
     * @static
     * @access public
     * @param  Int      $id
     * @param  String   $client_id
     * @return bool
     */
    public static function clientIsReconnect($id){
        $user = self::getUserOne($id);
        if($user){
            //存在 正在重新链接 删除定时器
            if(isset($user) && !is_null($user)){
                echo "Timerdel \n";
                Timer::del($user);
                unset($user);
            }
        }
        return $user;
    }
    /**
     *  退出当前登陆
     *
     * @static
     * @access public
     * @param  String   $client_id
     * @param  Array    $session
     * @return void
     */
    public static function logout($client_id , $session = array()){
        $uid = isset($session) ? (int) $session : 0;
        if($uid <= 0)return;
        $user = self::getUserOne($uid);
        //设置定时器  1分钟内不连接上来则认为下线
        $user   =   Timer::add(7, array('Core\Toos', 'clearUser'), array($uid), false);
        echo "set timer_id\n";
        //var_dump($user);
        self::setUserOne($uid ,$user);
    }
    /**
     *  用户彻底下线  清除用户
     *
     * @static
     * @access public
     * @param  Int  $uid
     * @return void
     */
    public static function clearUser($uid = 0){
        echo "del {$uid}\n";
    }
    /**
     *  心跳包
     *
     * @static
     * @access public
     * @return void
     */
    public static function heartBeat(){
        //echo "heartBeat。。。。\n";
        return;
    }

}
3542 0 0
0个回答

年代过于久远,无法发表回答
×
🔝