美文网首页
GRPC iOS 使用

GRPC iOS 使用

作者: Bool吖 | 来源:发表于2019-04-03 13:15 被阅读0次

    GRPC是一个高性能、通用的开源RPC框架,基于HTTP/2协议标准和Protobuf序列化协议开发,支持众多的开发语言。。。更多介绍

    编写Proto(asset.proto)(这里使用server写的没有自己写)

    syntax = "proto3";
    
    option java_multiple_files = true;
    option java_package = "io.grpc.examples.proto.asset";
    option java_outer_classname = "AssetProto";
    
    package nameservers;
    
    // 定义服务
    service AssetService {
      // 服务中的方法,以流的方式返回
      rpc getAsset (User) returns (stream Asset) {
      }
    }
    
    message User {
      string account = 1;
    }
    
    message Asset {
      string account=1;
      int64 cash = 2;
      int64 timestamp = 3;
    }
    

    编写Podspec:

    Pod::Spec.new do |s|
      s.name     = "GRPCDemo"
      s.version  = "0.0.1"
      s.license  = "Apache License, Version 2.0"
      s.authors  = { 'gRPC contributors' => 'grpc-io@googlegroups.com' }
      s.homepage = "https://grpc.io/"
      s.summary = "GRPCDemo example"
      s.source = { :git => 'https://github.com/grpc/grpc.git' }
    
      s.ios.deployment_target = "7.1"
      s.osx.deployment_target = "10.9"
    
      # Base directory where the .proto files are.
      src = "./protos/"
    
      # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients.
      s.dependency "!ProtoCompiler-gRPCPlugin", "~> 1.0"
    
      # Pods directory corresponding to this app's Podfile, relative to the location of this podspec.
      pods_root = 'Pods'
    
      # Path where Cocoapods downloads protoc and the gRPC plugin.
      protoc_dir = "#{pods_root}/!ProtoCompiler"
      protoc = "#{protoc_dir}/protoc"
      plugin = "#{pods_root}/!ProtoCompiler-gRPCPlugin/grpc_objective_c_plugin"
    
      # Directory where the generated files will be placed.
      dir = "#{pods_root}/#{s.name}"
    
      s.prepare_command = <<-CMD
        mkdir -p #{dir}
        #{protoc} \
            --plugin=protoc-gen-grpc=#{plugin} \
            --objc_out=#{dir} \
            --grpc_out=#{dir} \
            -I #{src} \
            -I #{protoc_dir} \
            #{src}/*.proto
      CMD
    
      # Files generated by protoc
      s.subspec "asset" do |ms|
        ms.source_files = "#{dir}/*.pbobjc.{h,m}", "#{dir}/**/*.pbobjc.{h,m}"
        ms.header_mappings_dir = dir
        ms.requires_arc = false
        # The generated files depend on the protobuf runtime.
        ms.dependency "Protobuf"
      end
    
      # Files generated by the gRPC plugin
      s.subspec "Services" do |ss|
        ss.source_files = "#{dir}/*.pbrpc.{h,m}", "#{dir}/**/*.pbrpc.{h,m}"
        ss.header_mappings_dir = dir
        ss.requires_arc = true
        # The generated files depend on the gRPC runtime, and on the files generated by protoc.
        ss.dependency "gRPC-ProtoRPC"
        ss.dependency "#{s.name}/Messages"
      end
    
      s.pod_target_xcconfig = {
        # This is needed by all pods that depend on Protobuf:
        'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1',
        # This is needed by all pods that depend on gRPC-RxLibrary:
        'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES',
      }
    end
    
    

    Podfile:

    platform :ios, '8.0'
    
    target 'GRPCDemo' do
    
    inhibit_all_warnings! #屏蔽所有warning
    
    
    
    pod 'gRPC'
    pod 'GRPCDemo', :path => '.'
    
    
    use_frameworks!
    end
    
    

    pod intall

    引入头文件*.pbobjc.h *.pbrpc.h

    Asset demo 使用:

    
     User *user = [[User alloc] init];
     user.account = @"";
    
     // 服务中的方法,以流的方式返回
     [service getAssetWithRequest:user eventHandler:^(BOOL done, Asset * _Nullable response, NSError * _Nullable error) {
        if (error) {
             NSLog(@"%@", error);
        }
            
        if (response) {
            NSLog(@"%lld", response.cash);
        }
     }];
    
    

    点击直达demo

    server demo

    扩展:

    swift GRPC

    OAuth2 on gRPC - Objective-C

    相关文章

      网友评论

          本文标题:GRPC iOS 使用

          本文链接:https://www.haomeiwen.com/subject/xxnabqtx.html