Gateway::updateSession session添加丢失 获取不到

luciferid
<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \Workerman\Autoloader;
use Workerman\Lib\Timer;
use Channel\Client;
use GatewayWorker\Lib\Gateway;
use Shared\Config\nswc_timer;
use Shared\Model\Game\NSWC_Model;
use GlobalData\GClient;
use Shared\Config\nswc;

 // (我一共起了 四个进程处理,发现当客户端1和客户端2 都连接到相同的进程号的时候,出现后边登录客户端 //session  没有跟新成功 )

// 自动加载类
require_once __DIR__ . '/../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);

$task = new Worker();
// 开启多少个进程运行定时任务,注意多进程并发问题
$task->count = 4;
$task->onWorkerStart = function($task)
{
    global $global;//定义business中 共享数据单例对象
    $global = new GClient('127.0.0.1:2207');

    Channel\Client::connect('127.0.0.1', 2206);

    $name = $task->id .'_' . nswc_timer::leave_nswc;
    //添加一个离开房间的定时器
    Channel\Client::on($name, function($data){

        NSWC_Model::test('添加一个离开房间的定时器');

        $timer_id = Timer::add('12', function ()use(&$timer_id,$data){
            NSWC_Model::test('离开定时器触发');

           NSWC_Model::GetInstance()->leave_nswc($data,$data,$data);

        },array(),false);

        $session = array('timer_leave_nswc'=>$timer_id);

        Gateway::updateSession($data , $session);
    });

};

// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}
4317 2 0
2个回答

walkor

这个属于在GatewayWorker外部调用Gateway接口,类似使用GatewayClient,要在onWorkerStart里面设置下 Gateway::$registerAddress = 'register服务ip:register端口';

  • luciferid 2016-11-08

    @1 使用了你提供的方法在onWorkerStart 中 还是丢失 我感觉是Gateway::updateSession 的问题

  • luciferid 2016-11-08

    我找到解决方案了, walkor
    session确实丢失了,
    解决方法就是,要在business 业务逻辑中 保存下session : gateway::updateSession($client_id, $_SESSION);

    这样在其他的外部进程中获取到的就是全部的session信息,要不然会丢失

  • walkor 2016-11-08

    怎么保存的帖下吧。
    另外帖的代码都是Worker的代码,并不是businessWorker。
    从你的代码中并没有看出来哪里用了GatewayWorker。
    而且我也不清楚你有哪些进程,进程间关系,业务逻辑也没有,比如哪里读的session

luciferid
<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \Workerman\Autoloader;
use Workerman\Lib\Timer;
use Channel\Client;
use GatewayWorker\Lib\Gateway;
use Shared\Config\nswc_timer;
use Shared\Model\Game\NSWC_Model;
use GlobalData\GClient;
use Shared\Config\nswc;

// (我一共起了 四个进程处理,发现当客户端1和客户端2 都连接到相同的进程号的时候,出现后边登录客户端 //session 没有跟新成功 )

// 自动加载类
require_once __DIR__ . '/../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);

$task = new Worker();
// 开启多少个进程运行定时任务,注意多进程并发问题
$task->count = 4;
$task->onWorkerStart = function($task)
{

    Gateway::$registerAddress = '127.0.0.1:1236';

    global $global;//定义business中 共享数据单例对象
    $global = new GClient('127.0.0.1:2207');

    Channel\Client::connect('127.0.0.1', 2206);

    $name = $task->id .'_' . nswc_timer::leave_nswc;
//添加一个离开房间的定时器
    Channel\Client::on($name, function($data){

        NSWC_Model::test('添加一个离开房间的定时器');

        $timer_id = Timer::add('12', function ()use(&$timer_id,$data){
            NSWC_Model::test('离开定时器触发');

            NSWC_Model::GetInstance()->leave_nswc($data,$data,$data);

        },array(),false);

        $session = array('timer_leave_nswc'=>$timer_id);

        Gateway::updateSession($data , $session);
    });

};

// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}
  • luciferid 2016-11-08

    依然无法解决.............照样丢失.................问题就在这Gateway::updateSession 方法

  • walkor 2016-11-08

    打下日志吧,看看updateSession的client_id和获取的是不是一个。

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