Bladeren bron

add ping.data

walkor 11 jaren geleden
bovenliggende
commit
a2b70d1574

BIN
applications/ChatDemo/Bootstrap/ping.data


+ 3 - 3
applications/ChatDemo/Protocols/JsonProtocol.php

@@ -5,9 +5,9 @@ class JsonProtocol
     public static function check($buffer)
     {
         // 读取首部四个字节
-        $buffer_data = unpack(‘Ntotal_length’, $buffer);
+        $buffer_data = unpack('Ntotal_length', $buffer);
         // 得到这次数据的整体长度(字节)
-        $total_length = $buffer_data['total_len'];
+        $total_length = $buffer_data['total_length'];
         // 已经收到的长度(字节)
         $recv_length = strlen($buffer);
         if($total_length>$recv_length)
@@ -34,7 +34,7 @@ class JsonProtocol
     {
         $buffer_data = unpack('Ntotal_length', $buffer);
         // 得到这次数据的整体长度(字节)
-        $total_length = $buffer_data['total_len'];
+        $total_length = $buffer_data['total_length'];
         // json的数据
         $json_string = substr($buffer, 4);
         return json_decode($json_string, true);

+ 19 - 5
applications/ChatDemo/Tests/Chat.php

@@ -10,8 +10,9 @@ if(!$sock)exit("can not create sock\n");
 
 
 fwrite($sock, JsonProtocol::encode('connect'));
-$rsp_string = fgets($sock, 1024);
-$ret = JsonProtocol::decode($rsp_string);
+
+
+$ret = read_data($sock);
 if(isset($ret['uid']))
 {
     echo "chart room login success , your uid is [{$ret['uid']}]\n";
@@ -41,7 +42,7 @@ while(1)
           // 接收消息
           if((int)$fd === (int)$sock)
           {
-              $ret = fgets($fd, 102400);
+              $ret = read_data($fd);
               if(!$ret){continue;exit("connection closed\n ");}
               
               // 是服务端发来的心跳,只是检测联通性,不用回复
@@ -50,8 +51,6 @@ while(1)
                   continue;
               }
               
-              $ret = JsonProtocol::decode(trim($ret));
-              
               if($ret['to_uid'] == $MYUID)
               {
                   echo $ret['from_uid'] , ' say to YOU:', $ret['message'], "\n";
@@ -80,3 +79,18 @@ while(1)
        }
     }
 }
+
+function read_data($socket)
+{
+   $buf = stream_socket_recvfrom($socket, 4);
+   if($buf == '')
+   {
+       return;
+   }
+   $remain_len = JsonProtoCol::check($buf);
+   //echo '$remain_len:'.$buf.':'.bin2hex($buf).":".$remain_len."\n";
+   $buf .= stream_socket_recvfrom($socket, $remain_len);
+   stream_set_blocking($socket, 0);
+   //echo '$buf:'.$buf."\n";
+   return JsonProtocol::decode($buf);
+}

+ 1 - 1
workerman/conf/conf.d/Gateway.conf

@@ -38,4 +38,4 @@ lan_port_start = 40000
 ping_interval = 10
 
 ;发送的心跳数据,可以是字符串或者二进制数据,二进制数据需要配置成 文件路径 如ping_data=/yourpath/ping.bin
-ping_data = #ping#
+ping_data = ../applications/ChatDemo/Bootstrap/ping.data