瀏覽代碼

心跳 无报错了

49107199@qq.com 3 年之前
父節點
當前提交
76819e505b
共有 2 個文件被更改,包括 91 次插入46 次删除
  1. 90 45
      app/api/imsocket/imsocket.php
  2. 1 1
      config/worker_server.php

+ 90 - 45
app/api/imsocket/imsocket.php

@@ -1,10 +1,14 @@
 <?php
 namespace app\api\imsocket;
 use think\worker\Server;
+use Workerman\Lib\Timer;
+use workerman\workerman;
+define('HEARTBEAT_TIME', 45);
 class imsocket extends Server
 {
     protected $socket = 'websocket://0.0.0.0:6541';
     protected $uidConnnections = array();
+    protected $connectionUid;
     public function __construct()
     {
         parent::__construct();
@@ -16,9 +20,27 @@ class imsocket extends Server
 
         $connection->send(json_encode($msg));
     }
+    public function onWorkerStart($worker){
+        //设置一个每5秒运转一次的定时器
+        Timer::add(10, function()use($worker){
+            $time_now = time();
+            foreach($worker->connections as $connection) {
+              // connection还没收到过音讯,则lastMessageTime设置为当前时时间
+                if (empty($connection->lastConnTime)) {
+                    $connection->lastConnTime = $time_now;
+                    continue;
+                }
+                if ($time_now - $connection->lastConnTime > HEARTBEAT_TIME) {
+                    $connection->close();
+                }
+            }
+        });
+    }
 
     public function onMessage($connection,$data)
     {
+        //给connection暂时设置一个lastConnTime属性,用来纪录上次收到音讯的时间
+        $connection->lastConnTime = time();
         $arr = json_decode($data);
 
         $db = new \mysqli('47.112.222.163','nn2022060401','RL2rpdKdKicikFBx','nn2022060401','3306');
@@ -27,73 +49,91 @@ class imsocket extends Server
         //绑定用户
         if($arr->type == "bind" && !isset($connection->uid)){
             $connection->uid = $arr->uid;
+            $this->connectionUid = $arr->uid;
             //保存uid到connection的映射,实现针对特定uid的推送
            $this->worker->uidConnections[$connection->uid]=$connection;
+
+
            if($arr->school_id){
                //如果是学校的话,返回一个老师的id给他。
 
-            $tsql="SELECT u.user_id,u.user_type,u.real_name FROM yoshop_user_info i JOIN yoshop_user u ON  i.user_id = u.user_id WHERE u.user_type=3 AND u.is_delete=0 AND i.school_id=".$arr->school_id." AND i.status=1";
-            $resT=$db->query($tsql);
-            if($resT->num_rows > 0) {
-                $tempList=[];
-                while($row = $resT->fetch_assoc()) {
-
-                    $temp['uid']= $row['user_id'];
-                    $temp['uname']= $row['real_name'];
-                    $temp['utype']= $row['user_type'];
-                    if(isset($this->worker->uidConnections[$row['user_id']])){
-                        //是否在线,在线加权重
-                        $temp['w']=1;
-
-                    }else{
-                        $temp['w']=0;
+                $tsql="SELECT u.user_id,u.user_type,u.real_name FROM yoshop_user_info i JOIN yoshop_user u ON  i.user_id = u.user_id WHERE u.user_type=3 AND u.is_delete=0 AND i.school_id=".$arr->school_id." AND i.status=1";
+                $resT=$db->query($tsql);
+                if($resT->num_rows > 0) {
+                    $tempList=[];
+                    while($row = $resT->fetch_assoc()) {
+
+                        $temp['uid']= $row['user_id'];
+                        $temp['uname']= $row['real_name'];
+                        $temp['utype']= $row['user_type'];
+                        if(isset($this->worker->uidConnections[$row['user_id']])){
+                            //是否在线,在线加权重
+                            $temp['w']=1;
+
+                        }else{
+                            $temp['w']=0;
+                        }
+                        $tempList[]=$temp;
                     }
-                    $tempList[]=$temp;
-                }
-                //按权重随机返回
-                $weight = 0;
-                $tempdata = array ();
-                foreach ($tempList as $one) {
-                    $weight += $one['w'];
-                    for ($i = 0; $i < $one['w']; $i++) {
-                        $tempdata[] = $one;
+                    //按权重随机返回
+                    $weight = 0;
+                    $tempdata = array ();
+                    foreach ($tempList as $one) {
+                        $weight += $one['w'];
+                        for ($i = 0; $i < $one['w']; $i++) {
+                            $tempdata[] = $one;
+                        }
                     }
-                }
-                $use = rand(0, $weight -1);
-                $one = $tempdata[$use];
+                    $use = rand(0, $weight -1);
+                    $one = $tempdata[$use];
 
 
-                $msg=['type'=>'bind','fansinfo'=>['uid'=>$one['uid'],'uname'=>$one['uname']]];
+                    $msg=['type'=>'bind','fansinfo'=>['uid'=>$one['uid'],'uname'=>$one['uname']]];
 
-            }else{
-                $msg=['type'=>'bind','fansinfo'=>''];
-            }
+                }else{
+                    $msg=['type'=>'bind','fansinfo'=>''];
+                }
 
-               $connection->send(json_encode($msg));
+           }else{
+               $msg=['type'=>'bind','fansinfo'=>''];
            }
+           $connection->send(json_encode($msg));
 
         }
 
         //聊天
         if($arr->type=="text"){
 
+            //先进库
+            $insetSql="INSERT INTO yoshop_imchat (from_user_id,to_user_id,message,school_id,speciality_id,sendtime,chat_key,is_push)
+            VALUES ('".$arr->from_user_id."','".$arr->to_user_id."','".$arr->msg."','".$arr->school_id."','".$arr->speciality_id."','".$arr->sendtime."','".$arr->chat_key."','1')";
+            //保存数据
+            $rs=$db->query($insetSql);
+            $id=mysqli_insert_id($db);
+            $temML['id']= $id;
+            $temML['from_user_id']= $arr->from_user_id;
+            $temML['to_user_id']= $arr->to_user_id;
+            $temML['msg']= $arr->msg;
+            $temML['school_id']= $arr->msg;
+            $temML['speciality_id']= $arr->msg;
+            $temML['chat_key']= $arr->chat_key;
+            $temML['sendtime']= $arr->sendtime;
+            $temML['type']= $arr->type;
             if(isset($this->worker->uidConnections[$arr->to_user_id])){
                 $conn= $this->worker->uidConnections[$arr->to_user_id];
-                $is_push=0;
-                $conn->send($data);
+                $conn->send(json_encode($temML));
             }else{
-//                //先返回给发送者错误信息
-//                $conn2= $this->worker->uidConnections[$arr->from_user_id];
-//                $msg=['type'=>'error','msg'=>'对方不在线'];
-//                $conn2->send(json_encode($msg));
-                $is_push=1;
+                $conn= $this->worker->uidConnections[$arr->from_user_id];
+                $conn->send(json_encode(['type'=>'down','msg'=>'对方不在线']));
             }
-            $insetSql="INSERT INTO yoshop_imchat (from_user_id,to_user_id,message,school_id,speciality_id,sendtime,chat_key,is_push)
-            VALUES ('".$arr->from_user_id."','".$arr->to_user_id."','".$arr->msg."','".$arr->school_id."','".$arr->speciality_id."','".$arr->sendtime."','".$arr->chat_key."','".$is_push."')";
-            //保存数据
-            $rs=$db->query($insetSql);
 
 
+
+        }
+        //在线更新已读消息
+        if($arr->type=='readly'){
+            $readSql="UPDATE yoshop_imchat SET is_push=0 WHERE id =".$arr->id;
+            $db->query($readSql);
         }
         //获取聊天记录
         if($arr->type=="loadHistory"){
@@ -136,7 +176,6 @@ class imsocket extends Server
 
             }
 
-
             $conn2->send(json_encode($msg));
 
         }
@@ -144,7 +183,13 @@ class imsocket extends Server
 
     }
     public function onClose($connection){
-        unset($this->worker->uidConnections[$connection->uid]);
+
+
+        unset($this->worker->uidConnnections[$this->connectionUid]);
+        $connection->send(json_encode(['type'=>'all','msg'=>'用户'.$this->connectionUid.'已经下线']));
+        $this->connectionUid='';
+
+
 
     }
 

+ 1 - 1
config/worker_server.php

@@ -24,7 +24,7 @@ return [
 
     // 支持workerman的所有配置参数
     'name'           => 'thinkphp',
-    'count'          => 4,
+    'count'          => 1,
     'daemonize'      => false,
     'pidFile'        => '',