Variable defaultConst

default: {
    Client: (new () => {
        invoke(method: string, request: any, option: ClientOption): Response;
        load(importPaths: string[], ...filenames: string[]): void;
    });
    SerializationTypeJSON: 2;
    SerializationTypePB: 0;
    Stream: (new (method: string, option: ClientOption) => {
        closeSend(): any;
        recvMsg(): any;
        sendMsg(request: any): any;
    });
    load(importPaths: string[], ...filenames: string[]): void;
}

Type declaration

  • Client: (new () => {
        invoke(method: string, request: any, option: ClientOption): Response;
        load(importPaths: string[], ...filenames: string[]): void;
    })
      • new (): {
            invoke(method: string, request: any, option: ClientOption): Response;
            load(importPaths: string[], ...filenames: string[]): void;
        }
      • Returns {
            invoke(method: string, request: any, option: ClientOption): Response;
            load(importPaths: string[], ...filenames: string[]): void;
        }

        • invoke:function
          • 执行 method 方法。

            import trpc from 'pts/trpc';

            const client = new trpc.Client()

            // 加载协议文件根目录中的 test.proto
            client.load([], 'test.proto');

            // 加载中协议文件 dirName 目录中的 test.proto
            // client.load(['dirName'], 'test.proto');

            export default function () {
            const res = client.invoke('/trpc.test.helloworld.Greeter/SayHello', {'msg':'abc'}, {'serviceName':'trpc.weishi.test.trpc'})
            console.log(res)
            };

            Parameters

            • method: string

              完整 path 路径 /a.b.c.d/e

            • request: any

              业务请求内容

            • option: ClientOption

              ClientOption 对象

            Returns Response

            响应对象

        • load:function
          • 加载 pb 文件。

            import trpc from 'pts/trpc';

            const client = new trpc.Client()

            // 加载协议文件根目录中的 test.proto
            client.load([], 'test.proto');

            // 加载中协议文件 dirName 目录中的 test.proto
            client.load(['dirName'], 'test.proto');

            Parameters

            • importPaths: string[]

              用于搜索在 proto 源文件的 import 语句中引用的依赖项的路径。如果没有提供导入路径,则当前目录被假定为唯一的导入路径。

            • Rest...filenames: string[]

              pb 文件名列表, 支持单个文件名调用

            Returns void

  • SerializationTypeJSON: 2

    JSON 序列化

  • SerializationTypePB: 0

    PB 序列化

  • Stream: (new (method: string, option: ClientOption) => {
        closeSend(): any;
        recvMsg(): any;
        sendMsg(request: any): any;
    })

    Stream 新建trpc流。

    import trpc from 'pts/trpc';

    // 加载协议文件根目录中的 test.proto
    trpc.load([], 'test.proto');

    // 加载中协议文件 dirName 目录中的 test.proto
    // trpc.load(['dirName'], 'test.proto');

    export default function () {
    const stream = new trpc.Stream('/trpc.test.test.TestStream/ClientStream', {
    namespace: 'Development',
    // 123 环境名
    env: 'test',
    // 123 环境名
    serviceName: 'trpc.test.test.TestStream',
    // 被调北极星服务名
    timeoutMs: 3500,
    // 超时时间
    serializationType: trpc.SerializationTypePB,
    // 序列化方式,SerializationTypePB需要上传pb协议文件,SerializationTypeJSON 可以不上传 PB 协议文件
    metaData: {
    // 通过 trpc 协议头部中的 transinfo 字段来透传数据
    person_id: '123456',
    },
    });
      • new (method, option): {
            closeSend(): any;
            recvMsg(): any;
            sendMsg(request: any): any;
        }
      • Parameters

        • method: string

          完整 path 路径 /a.b.c.d/e

        • option: ClientOption

          ClientOption 对象

        Returns {
            closeSend(): any;
            recvMsg(): any;
            sendMsg(request: any): any;
        }

        返回Stream对象

        • closeSend:function
          • closeSend 结束发送。

              stream.closeSend()
            

            Returns any

        • recvMsg:function
          • recvMsg 接收服务端数据。

                const rsp = stream.recvMsg()
            console.log("recvMsg rsp: "+JSON.stringify(rsp));
            // 如果返回结果为"EOF" 字符串,表明服务端发送结束
            if (rsp === "EOF") {
            console.log("stream end")
            }
            // 以map的形式返回结果,比如message格式如下
            //message HelloRsp {
            // string msg = 1;
            //}
            let msg = rsp['msg'];
            console.log('msg: ' + msg);

            Returns any

            回包内容,如果为"EOF"字符串,则服务端发送数据结束

        • sendMsg:function
          • sendMsg 发送业务请求数据。


            stream.sendMsg({
            'msg': 'ping 1111',
            })

            Parameters

            • request: any

              业务请求内容

            Returns any

  • load:function
    • 加载 pb 文件。

      import trpc from 'pts/trpc';

      // 加载协议文件根目录中的 test.proto
      trpc.load([], 'test.proto');

      // 加载中协议文件 dirName 目录中的 test.proto
      trpc.load(['dirName'], 'test.proto');

      Parameters

      • importPaths: string[]

        用于搜索在 proto 源文件的 import 语句中引用的依赖项的路径。如果没有提供导入路径,则当前目录被假定为唯一的导入路径。

      • Rest...filenames: string[]

        pb 文件名列表, 支持单个文件名调用

      Returns void