phpsocketio 过一夜 可以连接成功但是emit不执行

HL

phpsocketio 过一夜 可以连接成功但是emit不执行

3232 10 0
10个回答

walkor 打赏

emit不执行的时候执行 php start.php status,截图下。注意是在emit不执行的时候截图

a1071121140

这是emit 不执行的截图

  • walkor 2020-04-08

    status 看没有问题。
    如果要解决,需要你提供更详细的信息。
    包括代码,测试方法,测试截图

  • a1071121140 2020-04-08

    @1:这个就是两段代码 一段是 js 第一段是 php

  • a1071121140 2020-04-08

    @1:就是 每个页面 都引入了 这段js代码,主要就是用来记录 当前用户在这个页面浏览了多长时间

  • walkor 2020-04-08

    不要在评论里放代码,没办法格式化。你在回复里放,还有测试步骤,测试结果截图

a1071121140
<?php
use Workerman\Worker;
use PHPSocketIO\SocketIO;

include __DIR__ . '/vendor/autoload.php';

//全局数组,保存正在监听的页面的信息
$routeInfo = array();
// 传入ssl选项,包含证书的路径
$context = array(
    'ssl' => array(
        'local_cert'  => '/etc/letsencrypt/live/xcx.ldcl3.cc/fullchain.pem',
        'local_pk'    => '/etc/letsencrypt/live/xcx.ldcl3.cc/privkey.pem',
        'verify_peer' => false,
    )
);
include __DIR__.'/helper.php';
$Helper = new Helper();
// PHPSocketIO服务
$sender_io = new SocketIO(2120,$context);
// 客户端发起连接事件时,设置连接socket的各种事件回调
$sender_io->on('connection', function($socket){
    // 当客户端发来登录事件时触发

    pdo_insert('logs',array('msg'=>'connection=>'));

    $socket->on('login', function ($data)use($socket){

         pdo_insert('logs',array('msg'=>'login=>'.$socket->uid));
        // 已经登录过了
        if(isset($socket->uid)){
            return;
        }
        global $Helper,$routeInfo;

        //这是浏览记录的参数
        $uid  = (string)$data['mid'];

        pdo_insert('logs',array('msg'=>'login=>'.$uid));

        $uniacid = $data['uniacid'];
        $route_title = $data['route_title'];
        $route = $data['route'];
        $parem = $data['parem'];
        //额外参数
        $courseid = $data['courseid'];
        $chapterid = $data['chapterid'];

        if(isset($routeInfo[$uid])){
            //如果存在 则先释放掉之前的页面监听
            //结束浏览
            if($routeInfo[$uid]['routeid']>0){
                $Helper->closeView($routeInfo[$uid]['routeid'],$routeInfo[$uid]['jointime']);
            }
            //结束浏览学习
            if($routeInfo[$uid]['view_recordid']>0){
                $Helper->closeStudyView($routeInfo[$uid]['view_recordid'],$routeInfo[$uid]['jointime']);
            }
            //结束学习
            if($routeInfo[$uid]['play_recordid']>0){
                $Helper->endStudy($routeInfo[$uid]['play_recordid'],$routeInfo[$uid]['play_starttime']);
            }

        }

        $socket->uid = $uid;

        if($chapterid>0){
             $view_recordid = $Helper->intoStudyView($uid,$uniacid,$courseid,$chapterid);   
        }

        $routeInfo[$uid] = array(
            'jointime'=>time(),
            'routeid'=>$Helper->intoView($uid,$uniacid,$route_title,$route,$parem),
            'view_recordid'=>$view_recordid,

        );

    });
     $socket->on('study', function ($data)use($socket){

        if(!isset($socket->uid))
        {
             return;
        }
        global $Helper,$routeInfo;

        $type = $data['type'];
        $uid = $data['mid'];
        $uniacid = $data['uniacid'];
        $courseid = $data['courseid'];
        $chapterid = $data['chapterid'];
        //开始计数之前先结束掉正在计数的内容
        if($routeInfo[$uid]['play_recordid']>0){
            $Helper->endStudy($routeInfo[$uid]['play_recordid'],$routeInfo[$uid]['play_starttime']);

        }
        if($type=='start'){

            $routeInfo[$uid]['play_recordid'] = $Helper->startStudy($uid,$uniacid,$courseid,$chapterid);

            $routeInfo[$uid]['play_starttime'] = time();
        }

    });
    // 当客户端断开连接是触发(一般是关闭网页或者跳转刷新导致)
    $socket->on('disconnect', function () use($socket) {

        if(!isset($socket->uid))
        {
             return;
        }
        global $Helper,$routeInfo;

        //结束浏览
        if($routeInfo[$socket->uid]['routeid']>0){
            $Helper->closeView($routeInfo[$socket->uid]['routeid'],$routeInfo[$socket->uid]['jointime']);
        }
        //结束浏览学习
        if($routeInfo[$socket->uid]['view_recordid']>0){
            $Helper->closeStudyView($routeInfo[$socket->uid]['view_recordid'],$routeInfo[$socket->uid]['jointime']);
        }
        //结束学习
        if($routeInfo[$socket->uid]['play_recordid']>0){
            $Helper->endStudy($routeInfo[$socket->uid]['play_recordid'],$routeInfo[$socket->uid]['play_starttime']);
        }

        unset($routeInfo[$socket->uid]);
    });
});

if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

-------------------------------
  • 暂无评论
a1071121140
define(['core', 'tpl'], function (core, tpl) {
    var modal = {};
    var params = {};
    modal.init = function (param) {
        params=param;
        modal.socket();   
    };
    modal.socket = function () {
        // 连接服务端
        var socket = io('https://xcx.ldcl3.cc:2120');
        var data = {
            mid:params.mid,
            uniacid:params.uniacid,
            route_title:document.title ? document.title:'',
            route:document.URL,
            courseid:params.courseid,
            chapterid:params.chapterid,
            parem:params.parem,
        };

        // 连接后登录
        socket.on('connect', function($aa){

            console.log($aa);

            socket.emit('login', data);
        });

        //点击 发送 数据
        $("#send_aa").click(function(){

            socket.emit('study', data);

        })

    };

    return modal
});
  • 暂无评论
walkor 打赏

$Helper = new Helper();
这里是初始化了数据库么?

a1071121140
<?php
require dirname(__FILE__) . '/../../../../framework/bootstrap.inc.php';

class Helper{

     private $route_view = 'ldy_shop_route_view';

     private $play_record = 'ldy_shop_knowledgepay_play_record';

     private $view_record = 'ldy_shop_knowledgepay_view_record';
    /*
     * 记录浏览页面
     * (int)mid 用户id,
     * (string)route 访问路径
     * (string)route_title 路径对应的标题
     */

    public function intoView($mid,$uniacid,$route_title,$route,$parem){
        //往记录表中增加记录
        if(empty($parem)){
            $parem = 'index';
        }

        $mid = explode("_",$mid)[1];

        //查询今天有没有浏览过这个页面
        $view_log = pdo_fetch("select id,view_cnt from " .tablename($this->route_view). " where mid=:mid and parem=:parem and create_time between unix_timestamp(curdate()) and unix_timestamp(now()) order by id desc limit 1",array(':mid'=>$mid,':parem'=>$parem));

        pdo_insert('logs',array('msg'=>serialize($view_log)));

        if(empty($view_log)){
            //添加一条记录
            $data = array(
                'uniacid'=>$uniacid,
                'mid'=>$mid,
                'route'=>$route,
                'route_title'=>$route_title,
                'create_time'=>time(),
                'view_cnt'=>1,
                'view_time'=>1,
                'parem'=>$parem,
            );
            pdo_insert($this->route_view,$data);
            $id = pdo_insertid();
        }else{
            //更新一条记录
            pdo_update($this->route_view,array('view_cnt'=>$view_log['view_cnt']+1),array('id'=>$view_log['id']));
            $id = $view_log['id'];
        }

        return $id;

    }
    /*
     * 关闭连接(结束浏览)
     * 浏览记录ID $routeid
     * 
     */
    public function closeView($routeid,$jointime){

        $view_log = pdo_fetch("select id,view_time from " .tablename($this->route_view). " where id=:id",array(':id'=>$routeid));

        if($view_log){
            //计算浏览时间
            $view_time = time()-$jointime;
            //叠加时间

            pdo_update($this->route_view,array('view_time'=>$view_log['view_time']+$view_time,'end_time'=>time()),array('id'=>$view_log['id']));

        }
        return true;
    }

    /*
     * 开始学习
     *  
     */
    public function startStudy($mid,$uniacid,$courseid,$chapterid){
        //查询今天有没有学习过这个课程中的章节
        $mid = explode("_",$mid)[1];
        $play_record = pdo_fetch("select id from " .tablename($this->play_record). " where mid=:mid and courseid=:courseid and chapterid=:chapterid and create_time between unix_timestamp(curdate()) and unix_timestamp(now()) order by id desc limit 1",array(':mid'=>$mid,':courseid'=>$courseid,':chapterid'=>$chapterid));
        if(empty($play_record)){
            //添加一条学习记录
            $data = array(
                'uniacid'=>$uniacid,
                'courseid'=>$courseid,
                'chapterid'=>$chapterid,
                'mid'=>$mid,
                'create_time'=>time(),
                'study_duration'=>1
            );
            pdo_insert($this->play_record,$data);

            $play_recordid = pdo_insertid();
        }else{

            $play_recordid = $play_record['id'];
            pdo_update($this->play_record,array('study_duration'=>$play_record['study_duration']+1),array('id'=>$play_record['id']));

        }

        return $play_recordid;

    }
    /*
     * 结束学习
     */
    public function endStudy($play_recordid,$starttime){

        $end_time = time();
        $study_duration = $end_time - $starttime;
        //
        $play_record = pdo_fetch("select id,study_duration from " .tablename($this->play_record). " where id=:id",array(':id'=>$play_recordid));
        if($play_record){

            pdo_update($this->play_record,array('study_duration'=>$play_record['study_duration']+$study_duration),array('id'=>$play_recordid));

        }

        return true;

    }
    /*
     * 开始浏览学习
     * 
     */
    public function intoStudyView($mid,$uniacid,$courseid,$chapterid){
        //往记录表中增加记录
        $mid = explode("_",$mid)[1];
        //查询今天有没有浏览过这个页面
        $view_log = pdo_fetch("select id,view_cnt from " .tablename($this->view_record). " where mid=:mid and courseid=:courseid and chapterid=:chapterid and create_time between unix_timestamp(curdate()) and unix_timestamp(now()) order by id desc limit 1",array(':mid'=>$mid,':courseid'=>$courseid,':chapterid'=>$chapterid));

        if(empty($view_log)){
            //添加一条记录
            $data = array(
                'uniacid'=>$uniacid,
                'mid'=>$mid,
                'courseid'=>$courseid,
                'chapterid'=>$chapterid,
                'create_time'=>time(),
                'view_cnt'=>1,
                'view_time'=>1
            );
            pdo_insert($this->view_record,$data);
            $id = pdo_insertid();
        }else{
            //更新一条记录
            pdo_update($this->view_record,array('view_cnt'=>$view_log['view_cnt']+1),array('id'=>$view_log['id']));
            $id = $view_log['id'];
        }

        return $id;

    }
    /*
     * 结束浏览学习
     * 
     */
    public function closeStudyView($view_recordid,$jointime){

        $view_log = pdo_fetch("select id,view_time from " .tablename($this->view_record). " where id=:id",array(':id'=>$view_recordid));

        if($view_log){
            //计算浏览时间
            $view_time = time()-$jointime;
            //叠加时间

            pdo_update($this->view_record,array('view_time'=>$view_log['view_time']+$view_time,'end_time'=>time()),array('id'=>$view_log['id']));

        }
        return true;
    }

}

?>
  • 暂无评论
a1071121140

![](//www.workerman.net/upload/20200408/085e8d4925e75a.png)

bootstrap.inc.php 里面引入了 pdo方法,然后pdo 再去初始化数据库

  • 暂无评论
walkor 打赏

判断无法触发emit是根据什么判断的,数据库没有写入数据了?

  • a1071121140 2020-04-08

    是根据数据库没有写入数据判断的,然后我也在想 是不是数据库一直连着,导致是数据库断开了

  • a1071121140 2020-04-08

    数据库初始化的时候 我是不是应该放在 connection的回调里面,这样每次用户连接的时候 就初始化

walkor 打赏

应该是数据库连接长时间不使用被mysql服务端断开了,然后你的数据库类没有做重连,导致数据无法写入数据库。
解决办法有2种
第一种、数据库连接使用完就关闭,下次使用时再重连数据库
第二种、把初始化数据库连接放到onworkerStart 里。并且做好数据库断开重连的操作,重连参考 https://github.com/walkor/mysql/blob/master/src/Connection.php#L1748 ,后者直接使用http://doc.workerman.net/components/workerman-mysql.html 这个数据库类,它自带重连功能。
$sender_io->on('workerStart', function()use($io) {
// 初始化数据库连接
});

  • a1071121140 2020-04-08

    我用的是 phpsocketio 请问应该放哪个位置

  • walkor 2020-04-08

    初始化连接放到workerStart里
    $sender_io->on('workerStart', function()use($io) {
    // 初始化数据库连接
    });

  • a1071121140 2020-04-08

    谢谢

  • a1071121140 2020-04-08

    @1:已经把数据库连接 改成了workerman 自带的类,就看明天会不会断开连接了

a1071121140

测试就是 每次进入页面 都会去 连接 并且登录

  • 暂无评论
年代过于久远,无法发表回答
×
🔝