Giter Club home page Giter Club logo

Comments (3)

walkor avatar walkor commented on May 20, 2024

session的存取是异步的,所以有一点点延迟(大概小于1毫秒左右)。如果请求过快,可能会造成A请求改变了session,但是B请求读到的仍然是旧的session。为了解决这个问题,可以用Gateway::getSession 和 Gateway::setSession 接口来存取session。

但是onClose里无法调用Gateway::getSession 和 Gateway::setSession接口,因为此时链接已经关闭了。
我想这里的解决办法是在内存中添加一个全局数组,里面记录所有client_id的session快照,如果快照本存在,就用最新的快照。

class Events
{
    // ['client_id1'=>session_array, 'client_id2'=>session_array, ...]
    public static $sessions = array();
    public static function onMessage($client_id)
    {
        // 如果内存中有对应client_id的session快照数据则使用内存中最新的数据
        $_SESSION = isset(self::$sessions[$client_id]) ? self::$sessions[$client_id] : $_SESSION;
        // 各种操作$_SESSION
        $_SESSION['xxx'] = xx;
        unset($_SESSION['rooms']['A']);
      
       // 操作完session记得同步一份给self::$sessions作为快照,保证self::$sessions里的数据是最新的
        self::$sessions[$client_id] = $_SESSION;
    }

    public function onClose($client_id) 
    {
         // 如果内存中有对应client_id的session数据则使用内存中最新的数据
        $_SESSION = isset(self::$sessions[$client_id]) ? self::$sessions[$client_id] : $_SESSION;
        // 业务逻辑
        // ....
        // 链接断开了,最后记得删除快照,避免内存泄漏
        unset(self::$sessions[$client_id]);      
    }
}

注意:这个快照要求gateway路由规则是将某个client_id的请求路由给固定worker进程(默认路由就是这样)。
如果你自定义了路由规则需要小心,某个client_id的session数据可能分散在各个worker进程的self::$sessions里,那么这个方法就不会奏效。

from gatewayworker.

nameldk avatar nameldk commented on May 20, 2024

了解了,感谢!

  1. 如果用第三方组件比如redis来存用户的session数据,就不存在延迟问题了吧?
  2. 如果用redis来存储session,性能是不是没有用直接用$_SESSION的好?
  3. client_id 是全局唯一的吧?

from gatewayworker.

walkor avatar walkor commented on May 20, 2024

1、对
2、性能上一般感觉不到差别
3、对

from gatewayworker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.