walkor 11 anni fa
parent
commit
b46d7986c0
2 ha cambiato i file con 17 aggiunte e 15 eliminazioni
  1. 12 14
      applications/Demo/Event.php
  2. 5 1
      applications/Demo/Lib/Gateway.php

+ 12 - 14
applications/Demo/Event.php

@@ -34,18 +34,6 @@ class Event
         return TextProtocol::check($buffer);
     }
     
-   
-   /**
-    * 当用户断开连接时触发的方法
-    * @param integer $client_id 断开连接的用户id 
-    * @return void
-    */
-   public static function onClose($client_id)
-   {
-       // 广播 xxx 退出了
-       GateWay::sendToAll(TextProtocol::encode("{$_SESSION['name']}[$client_id] logout"));
-   }
-   
    /**
     * 有消息时触发该方法
     * @param int $client_id 发消息的client_id
@@ -67,9 +55,9 @@ class Event
         }
         
         // ********* 进入聊天逻辑 ****************
-        // 判断是否是私聊,私聊数据格式 client_id:xxxxx
+        // 判断是否是私聊
         $explode_array = explode(':', $message, 2);
-        // 私聊
+        // 私聊数据格式 client_id:xxxxx
         if(count($explode_array) > 1)
         {
             $to_client_id = (int)$explode_array[0];
@@ -80,4 +68,14 @@ class Event
         return GateWay::sendToAll(TextProtocol::encode($_SESSION['name'] . "[$client_id] said :" . $message));
    }
    
+   /**
+    * 当用户断开连接时触发的方法
+    * @param integer $client_id 断开连接的用户id
+    * @return void
+    */
+   public static function onClose($client_id)
+   {
+       // 广播 xxx 退出了
+       GateWay::sendToAll(TextProtocol::encode("{$_SESSION['name']}[$client_id] logout"));
+   }
 }

+ 5 - 1
applications/Demo/Lib/Gateway.php

@@ -24,7 +24,7 @@ class Gateway
     * @param string $message 向客户端发送的消息(可以是二进制数据)
     * @param array $client_id_array 客户端id数组
     */
-   public static function sendToAll($message, $client_id_array = array())
+   public static function sendToAll($message, $client_id_array = null)
    {
        $pack = new GatewayProtocol();
        $pack->header['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
@@ -41,6 +41,10 @@ class Gateway
            $params = array_merge(array('N*'), $client_id_array);
            $pack->ext_data = call_user_func_array('pack', $params);
        }
+       elseif(empty($client_id_array) && is_array($client_id_array))
+       {
+           return;
+       }
        
        $buffer = $pack->getBuffer();
        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据