参加比赛workerman框架程序

  1 <?php
  2 /*2015/5/7 23:32
  3 *liuyang 聊天主程序
  4 *
  5 */
  6 use \GatewayWorker\Lib\Gateway;
  7 use \GatewayWorker\Lib\Store;
  8 
  9 require ("./sql.php");
 10 
 11 class Event
 12 {
 13    /*
 14     * 有消息时
 15     */
 16    public static function onMessage($client_id, $message)
 17    {
 18         //日志写入_________________________
 19         self::log($message);
 20        $message_data = json_decode($message, true);//客户端传递的是json数据
 21       //检验信息接受
 22        if(0&&$message!=null)
 23         {
 24             if(empty($_SESSION['id'])){
 25             $_SESSION['id']=$message;
 26                 
 27             }  
 28             Gateway::sendToCurrentClient($message_data['type']);
 29             return ;
 30         }
 31         
 32         switch($message_data['type'])// 根据类型执行不同的业务
 33         {   
 34             // 客户端回应服务端的心跳
 35             case 'pong':
 36                 if(empty($_SESSION['cid'])){
 37                   $db1 = new DB(); 
 38                   $sql = "UPDATE `user` SET `cid`='".$client_id."' WHERE name = '".$message_data['name']."'  ";
 39                   $db1->query($sql);
 40                 }
 41                 return;
 42             case 'mid':
 43                 $mid=$message_data['mid'];
 44                 self::msgid($mid);
 45                return;
 46             //客户端注册
 47             case 'register':
 48                $db1 = new DB(); 
 49                $name=$message_data['username'];
 50                $password=$message_data['password'];
 51                $question=$message_data['question'];
 52                $answer=$message_data['answer'];
 53                //$phone=$message_data['phone'];
 54                
 55                $sql = " SELECT * FROM `user` WHERE name = '".$name."'";
 56                $array=$db1->get_one($sql);
 57                if(!is_array($array))
 58                {
 59                   $sql = "INSERT INTO `shine`.`user` ( `name`, `password`,`question`,`answer`) VALUES ( '".$name."', '".$password."', '".$question."', '".$answer."');";
 60                   $db1->query($sql);
 61                   //返回
 62                     $msg = array(
 63                     'type'=>'register',
 64                     'cid'=>$client_id,
 65                     'register'=>'true',                    
 66                     'time'=>date('Y-m-d H:i:s'),
 67                    );
 68                 // 向当前客户端发送已登陆数据
 69                 return Gateway::sendToCurrentClient( json_encode(  $msg ));
 70                }
 71                else {
 72                     $msg = array(
 73                     'type'=>'register',
 74                     'cid'=>$client_id, 
 75                     'register'=>'false',                    
 76                     'time'=>date('Y-m-d H:i:s'),
 77                 );
 78                 // 向当前客户端发送已登陆数据
 79                 return Gateway::sendToCurrentClient( json_encode(  $msg ));
 80                }
 81             //客户端登陆
 82             case 'login':
 83                $db1 = new DB(); 
 84                $name=$message_data['username'];
 85                $password=$message_data['password'];
 86                $sql = " SELECT * FROM `user` WHERE name = '".$name."' AND password = '".$password."'  ";
 87                $array=$db1->get_one($sql);
 88                if(is_array($array)&&$array['cid']==0)
 89                {
 90                   $sql = "UPDATE `user` SET `cid`='".$client_id."' WHERE name = '".$name."' AND password = '".$password."' ";
 91                   $db1->query($sql);
 92                   
 93                     $msg = array(
 94                     'type'=>'login', 
 95                     'online'=>'true',
 96                     'cid'=>$client_id,
 97                     'time'=>date('Y-m-d H:i:s'),
 98                 );
 99                 // 向当前客户端发送已登陆数据
100                 Gateway::sendToCurrentClient( json_encode(  $msg ));
101                 //查询服务器缓存的消息
102                 self::ask_msgc($name);
103                 return;
104                }
105                else{
106                    
107                     $msg = array(
108                     'type'=>'login', 
109                     'online'=>'false',
110                     'cid'=>$client_id,
111                     'time'=>date('Y-m-d H:i:s'),
112                 );
113                   // 向当前客户端发送登陆出错
114                 Gateway::sendToCurrentClient( json_encode(  $msg ));
115                 return;
116                }
117              
118                
119                 return;
120             //忘记密码
121             case 'forget':
122                
123                $db1 = new DB(); 
124                $name=$message_data['username'];
125                $password=$message_data['password'];
126                $question=$message_data['question'];
127                $answer=$message_data['answer'];
128                $sql = " SELECT * FROM `user` WHERE name = '".$name."' AND question = '".$question."' AND answer = '".$answer."'  ";
129                $array=$db1->get_one($sql);
130                if(is_array($array))
131                {  
132                   //$sql = "UPDATE `user` SET `name`='".$name."' , `password`='".$password."' WHERE name = '".$message_data['name']."' ";
133                   $sql = "UPDATE `user` SET  `password`='".$password."' WHERE name = '".$name."' ";
134                   $db1->query($sql);
135                    $msg = array(
136                     'type'=>'forget', 
137                     'forget'=>'true',
138                     'msg'=>"new secret is running",
139                     'cid'=>$client_id,
140                     'time'=>date('Y-m-d H:i:s'),
141                     );
142                    
143                }
144                else{
145                     $msg = array(
146                     'type'=>'forget', 
147                     'forget'=>'false',
148                     'msg'=>"the answer is wrong",
149                     'cid'=>$client_id,
150                     'time'=>date('Y-m-d H:i:s'),
151                     );
152                }
153                
154                      Gateway::sendToCurrentClient(json_encode( $msg));
155                return;
156             case 'join':
157                 /////////////////////
158                  $name=$message_data['name'];
159                  $joinname=$message_data['joinname'];
160                 $msg = array(
161                     'type'=>'join',
162                     'joinname'=>$name,    
163                     'name'=>$joinname,                           
164                     'time'=>date('Y-m-d H:i:s'),
165                     'mid'=>time().rand(100,999)%100000000
166                 );            
167                   self::send($joinname,$msg);
168                  //////////////////////////////
169             //同意加好友
170             case 'agreejoin':
171                  $db1 = new DB(); 
172                  $name=$message_data['name'];
173                  $joinname=$message_data['joinname'];
174                  $isagree=$message_data['isagree'];
175                  if($isagree){
176                  self::join($name, $joinname); 
177                  
178                  self::join($joinname, $name);
179                  }
180                 
181                   return;
182                   //点击录音限制群组录音
183             case 'luyin':    
184                 $toname=$message_data['toname'];
185             
186                  $msg = array(
187                     'type'=>'luyin',
188                     'fromname'=>$message_data['fromname'],    
189                     'time'=>date('Y-m-d H:i:s'),
190                     'mid'=>time().rand(100,999)%100000000
191                 );
192                     foreach($toname as $v){
193                      // 转发消息给对应的客户端
194                 self::send($v['name'],$message_data,0);
195                 }            
196                   return  ;        
197             case 'agreeluyin':    
198                 $toname=$message_data['toname'];
199                 
200                 foreach($toname as $v){
201                      // 转发消息给对应的客户端
202                 self::send($v['name'],$message_data,0);
203                 }        
204                 return  ;        
205                 //发消息给某个人    
206             case 'say':
207                  $toname=$message_data['toname'];
208             
209                  $msg = array(
210                     'type'=>'say',
211                     'fromname'=>$message_data['name'],    
212                     'msg'=>$message_data['msg'],                    
213                     'time'=>date('Y-m-d H:i:s'),
214                     'mid'=>time().rand(100,999)%100000000
215                 );                    
216                 self::send($toname,$msg);
217                 return;
218                 //发消息给N个人    
219             case 'group':    
220                 $toname=$message_data['toname'];
221             
222                  $msg = array(
223                     'type'=>'group',
224                     'fromname'=>$message_data['name'],    
225                     'msg'=>$message_data['msg'],                    
226                     'time'=>date('Y-m-d H:i:s'),
227                     'mid'=>time().rand(100,999)%100000000
228                 );
229                 foreach($toname as $v){
230                      // 转发消息给对应的客户端
231                 self::send($v['name'],$msg);
232                 }         
233                   return  ;    
234                                           
235         }//switch结束
236         
237    }
238    /**
239     * 当客户端断开连接时
240     * @param integer $client_id 客户端id
241     */
242    public static function onClose($client_id)
243    {   
244                   $db1 = new DB(); 
245                   $sql = "UPDATE `user` SET `cid`=0 WHERE cid = '".$client_id."'";
246                   $db1->query($sql);
247    } 
248    /**
249    *简单封装个send函数
250    *5/10
251    *param string name object msg
252    */
253     public static function send($name,$msg,$c=1)
254    {   
255                  $db1 = new DB(); 
256                  $sql = " SELECT * FROM `user` WHERE name = '".$name."' ";
257                  $array=$db1->get_one($sql);
258                   if($array['cid']==0){
259                  // Gateway::sendToCurrentClient( "the member is not online");
260                    return;                  
261                   }
262                  //Gateway::sendToCurrentClient( "success");
263                  if($c){
264                      self::msgc($name,$msg);//消息保存到数据库用来检测消息是否发送成功
265                  }
266                  $msg=json_encode($msg);
267                  Gateway::sendToClient($array['cid'],$msg);
268                  //日志写入_________________________
269                  self::log($msg);
270                  return;                
271    } 
272    //数据库缓存信息
273    public static function msgc($toname,$msg,$from_name=null){
274                  $db1 = new DB(); 
275                  $sql = "INSERT INTO `msgc` ( `toname`, `from_name`, `mid`, `msg`) VALUES ('".$toname."' ,'".$from_name."' , '".$msg['mid']."', '".json_encode($msg)."');";
276                  $db1->query($sql);
277                  
278                  return;            
279        
280    }
281    //查询数据库是否有自己的消息
282    public static function ask_msgc($toname){
283                  $db1 = new DB(); 
284                  $sql = " SELECT * FROM `msgc` WHERE toname = '".$toname."' AND issend = 0 ";
285                  $array=$db1->get_all($sql);
286                 foreach($array as $v){
287                     
288                // self::send($toname,$v['msg']);
289                Gateway::sendToCurrentClient( $v['msg']); 
290               
291                 }   
292                  return;            
293        
294    }
295    //返回msgid确认发送成功  
296    public static function msgid($mid){
297                   $db1 = new DB(); 
298                   $sql = "UPDATE `msgc` SET `issend`=1 WHERE mid = '".$mid."'";
299                   $db1->query($sql);
300                   return;
301    }
302    //加好友操作
303    public static function join($name,$joinname){
304                  $db1 = new DB(); 
305                  $sql = " SELECT * FROM `user` WHERE name = '".$name."' ";
306                  $array=$db1->get_one($sql);
307                  $list=json_decode($array['list']);
308                  foreach($list as $l){
309                      if($l->name===$joinname){
310                          // Gateway::sendToCurrentClient(  "the friend is exits in you friendlist");
311                           return;
312                      }
313                  }
314                  if(is_array($list)){
315                  $list=array_merge($list,array(array("name"=>$joinname)));     
316                  }
317                  else{
318                  $list=array_merge(array(array("name"=>$joinname)));          
319                  }
320                  $sql = "UPDATE `user` SET `list`='".json_encode($list)."' WHERE name = '".$name."'  ";
321                  //$_SESSION['list']=$list;
322                  $db1->query($sql);
323                 /*  $msg = array(
324                     'type'=>'join',
325                     'list'=>$list,                    
326                     'time'=>date('Y-m-d H:i:s'),
327                 );
328                 Gateway::sendToCurrentClient(  json_encode(  $msg )); */
329                 
330                  return;
331    }
332    
333    //写入日志文件
334     public static function log($msg=''){
335         $is_log = 1;
336         $path="./log/jsonlog.txt";
337         if($is_log){
338             $handle = fopen($path, "a+");
339             $text = date("Y-m-d H:i:s")." ".$msg."\r\n";
340             fwrite($handle,$text);
341             fclose($handle);
342         }
343     }
344 }

 

posted @ 2016-08-24 14:12  刘阳|lyang|yangakw  阅读(489)  评论(0编辑  收藏  举报