public function onReceive($cli, $data) { $rs = JsonProtocol::decode($data); echo '<pre>'; print_r($rs); echo '</pre>'; }
/** * 服务端接收数据 * * @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']; // 判断类对应文件是否载入 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); // 发送数据给客户端,调用成功,data下标对应的元素即为调用结果 $serv->send($fd, JsonProtocol::encode($ret)); } catch (Exception $e) { // 发送数据给客户端,发生异常,调用失败 $code = $e->getCode() ? $e->getCode() : 500; $result = array('code' => $code, 'msg' => $e->getMessage(), 'data' => $e); $serv->send($fd, JsonProtocol::encode($result)); } }
/** * 从服务端接收数据 * @throws Exception */ public function recvData() { $ret = fgets($this->connection); $this->closeConnection(); if (!$ret) { throw new Exception("recvData empty"); } return JsonProtocol::decode($ret); }
/** * 从服务端接收数据 * @throws Exception */ public function recvData() { if (self::$useSwoole) { $res = $this->swooleClient->recv(); $this->swooleClient->close(); } else { $res = fgets($this->connection); $this->closeConnection(); } if (!$res) { throw new Exception("recvData empty"); } return JsonProtocol::decode($res); }
/** * 数据接收完整后处理业务逻辑 * @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, $this->statisticAddress); // 发送数据给客户端 类不存在 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, '', $this->statisticAddress); // 发送数据给客户端,调用成功,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, $this->statisticAddress); return $this->sendToClient(JsonProtocol::encode(array('code' => $code, 'msg' => $e->getMessage(), 'data' => $e))); } }