Example #1
0
 /**
  * 有消息时触发该方法
  * @param int $client_id 发消息的client_id
  * @param string $message 消息
  * @return void
  */
 public static function onMessage($client_id, $message)
 {
     $db = \Lib\Db::instance('tc504');
     $nowtime = idate("U");
     $ok = "right";
     $error = "error";
     $sqlok = "sql right!";
     $sqlerror = "sql error!";
     if ($message[0] == '{') {
         $message = JsonProtocol::decode($message);
         $sqldidian = $message['ID'];
         $sqlshidu = $message['humidity'];
         $sqlwendu = $message['temperature'];
         $insert_id = $db->insert('wenshidu')->cols(array('didian' => $sqldidian, 'shidu' => $sqlshidu, 'wendu' => $sqlwendu, 'time' => $nowtime))->query();
         GateWay::sendToClient($client_id, TextProtocol::encode($insert_id));
     } else {
         $message = TextProtocol::decode($message);
         $commend = trim($message);
         $ret = $db->select('ui_cardid')->from('tbl_user')->where("ui_cardid = '{$commend}' ")->single();
         if ($commend === $ret) {
             $insert_id = $db->insert('tbl_log')->cols(array('log_card' => $commend, 'log_time' => $nowtime))->query();
             if ($insert_id === 1) {
                 GateWay::sendToClient($client_id, TextProtocol::encode($sqlok));
             } else {
                 GateWay::sendToClient($client_id, TextProtocol::encode($sqlerror));
             }
         } else {
             GateWay::sendToClient($client_id, TextProtocol::encode($error));
         }
     }
 }
 /**
  * 数据接收完整后处理业务逻辑
  * @see Man\Core.SocketWorker::dealProcess()
  */
 public function dealProcess($recv_str)
 {
     /**
      * data的数据格式为
      * ['class'=>xx, 'method'=>xx, 'param_array'=>array(xx)]
      * @var array
      */
     $data = JsonProtocol::decode($recv_str);
     // 判断数据是否正确
     if (empty($data['class']) || empty($data['method']) || !isset($data['param_array'])) {
         // 发送数据给客户端,请求包错误
         return $this->sendToClient(JsonProtocol::encode(array('code' => 400, 'msg' => 'bad request', 'data' => null)));
     }
     // 获得要调用的类、方法、及参数
     $class = $data['class'];
     $method = $data['method'];
     $param_array = $data['param_array'];
     StatisticClient::tick($class, $method);
     $success = false;
     // 判断类对应文件是否载入
     if (!class_exists($class)) {
         $include_file = ROOT_DIR . "/Services/{$class}.php";
         if (is_file($include_file)) {
             require_once $include_file;
         }
         if (!class_exists($class)) {
             $code = 404;
             $msg = "class {$class} not found";
             StatisticClient::report($class, $method, $success, $code, $msg);
             // 发送数据给客户端 类不存在
             return $this->sendToClient(JsonProtocol::encode(array('code' => $code, 'msg' => $msg, 'data' => null)));
         }
     }
     // 调用类的方法
     try {
         $ret = call_user_func_array(array($class, $method), $param_array);
         StatisticClient::report($class, $method, 1, 0, '');
         // 发送数据给客户端,调用成功,data下标对应的元素即为调用结果
         return $this->sendToClient(JsonProtocol::encode(array('code' => 0, 'msg' => 'ok', 'data' => $ret)));
     } catch (Exception $e) {
         // 发送数据给客户端,发生异常,调用失败
         $code = $e->getCode() ? $e->getCode() : 500;
         StatisticClient::report($class, $method, $success, $code, $e);
         return $this->sendToClient(JsonProtocol::encode(array('code' => $code, 'msg' => $e->getMessage(), 'data' => $e)));
     }
 }
Example #3
0
 /**
  * 服务端接收数据
  *
  * @param $serv      swoole_server对象
  * @param $fd        连接的描述符
  * @param $from_id   reactor的id,无用
  * @param $data      接收数据
  */
 public function onReceive(swoole_server $serv, $fd, $from_id, $data)
 {
     //检测数据完整性
     if (JsonProtocol::check($data) != 0) {
         return;
     }
     $data = JsonProtocol::decode($data);
     //接收参数
     $class = $data['class'];
     $method = $data['method'];
     $params = $data['params'];
     $startTime = $this->microtimeFloat();
     // 判断类对应文件是否载入
     if (!class_exists($class)) {
         $include_file = ROOT_DIR . "Server/{$class}.php";
         if (is_file($include_file)) {
             require_once $include_file;
         }
         if (!class_exists($class)) {
             $code = 404;
             $msg = "class {$class} not found";
             $result = array('code' => $code, 'msg' => $msg, 'data' => null);
             $serv->send($fd, JsonProtocol::encode($result));
         }
     }
     // 调用类的方法
     try {
         $ret = call_user_func_array(array(new $class(), $method), $params);
         $code = $ret['code'];
         $msg = $ret['msg'];
         // 发送数据给客户端,调用成功,data下标对应的元素即为调用结果
         $serv->send($fd, JsonProtocol::encode($ret));
     } catch (Exception $e) {
         // 发送数据给客户端,发生异常,调用失败
         $code = $e->getCode() ? $e->getCode() : 500;
         $msg = $e->getMessage();
         $result = array('code' => $code, 'msg' => $msg, 'data' => $e);
         $serv->send($fd, JsonProtocol::encode($result));
     }
     //请求数据统计,放在task执行
     $executionTime = $this->microtimeFloat() - $startTime;
     $report = array('class' => $class, 'method' => $method, 'params' => json_encode($params), 'code' => $code, 'msg' => $msg, 'execution' => $executionTime, 'time' => time());
     $serv->task(json_encode($report));
 }