Esempio n. 1
0
 public function onConnect($cli)
 {
     $class = 'Member';
     $method = 'userInfoByMid';
     $params = array('mid' => 13392);
     $data = array('class' => $class, 'method' => $method, 'params' => $params);
     $buffer = JsonProtocol::encode($data);
     $cli->send($buffer);
 }
Esempio n. 2
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'];
     // 判断类对应文件是否载入
     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));
     }
 }
Esempio n. 3
0
 /**
  * 发送数据给服务端
  * @param string $method
  * @param array $arguments
  */
 public function sendData($method, $arguments)
 {
     $this->openConnection();
     $bin_data = JsonProtocol::encode(array('class' => $this->serviceName, 'method' => $method, 'param_array' => $arguments));
     return fwrite($this->connection, $bin_data) == strlen($bin_data);
 }
 /**
  * 发送数据给服务端
  * @param string $method
  * @param array $arguments
  */
 public function sendData($method, $arguments)
 {
     $this->openConnection();
     $bin_data = JsonProtocol::encode(array('class' => $this->serviceName, 'method' => $method, 'param_array' => $arguments));
     if (fwrite($this->connection, $bin_data) !== strlen($bin_data)) {
         throw new \Exception('Can not send data');
     }
     return true;
 }
Esempio n. 5
0
 /**
  * 发送数据给服务端
  * 
  * @param string $method        	
  * @param array $arguments        	
  */
 public function sendData($method, $arguments)
 {
     $bin_data = JsonProtocol::encode(array('class' => $this->serviceName, 'method' => $method, 'param_array' => $arguments)) . "\r\n";
     $this->openConnection();
     if (self::$useSwoole) {
         return $this->swooleClient->send($bin_data);
     } else {
         return fwrite($this->connection, $bin_data) == strlen($bin_data);
     }
 }
Esempio n. 6
0
 /**
  * 数据接收完整后处理业务逻辑
  * @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)));
     }
 }