请教::我想写一个方法在onWebSocketConnect中被调用,我应该怎么写?放在哪里

z325988

我想请教各位大侠一个问题,我在onWebSocketConnect中写了一段语法,但是需要两次被调用,因此,我感觉代码重复了,我想吧这段代码写成一个方法,然后在onWebSocketConnect中进行调用,我应该怎么写?
或者写好后放在哪里?
我用的是thinkphp3.2.3框架

public static function onWebSocketConnect($client_id,$data){
       if ($data != 'http://local.gendan5.com'){
           Gateway::closeClient($data);
       }else{
//           $xmlInfo = brokerAccount_data('xm-zero',2,'./cache/xm-zero');
//           Gateway::sendToClient($client_id,json_encode($data));
           $o_cid = $data;
           if ($o_cid != "null"){
               Gateway::closeClient($o_cid);
           }
           $BName = $data;
           $url = "http://local.gendan5.com/tp/index.php/?s=Home/test/test_file_get_contents/BName/$BName";
           $curl = curl_init($url);
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
           $xmlInfo = curl_exec($curl);
           curl_close($curl);
           Gateway::sendToClient($client_id,$xmlInfo);
           \Workerman\Lib\Timer::add(5,function ($client_id,$data){
               $BName = $data;
               $url = "http://local.gendan5.com/tp/index.php/?s=Home/test/test_file_get_contents/BName/$BName";
               $curl = curl_init($url);
               curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
               $xmlInfo = curl_exec($curl);
               curl_close($curl);
               Gateway::sendToClient($client_id,$xmlInfo);
           },array($client_id,$data));
       }
   }
阅读 1197
1个回答

blogdaren

可以写在Events或者自定义的业务类, 然后把“长得很像的代码”抽象成一个通用的方法即可。