@ad2302/rxjs-grpc 中文文档教程

发布于 3年前 浏览 12 项目主页 更新于 3年前

构建状态npm 版本

rxjs-grpc

Installation

$ npm install rxjs-grpc rxjs grpc

Quickstart

创建您的 protobuf 定义文件 sample.proto

syntax = "proto3";

package sample;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

生成您的 TypeScript 接口:

$ ./node_modules/.bin/rxjs-grpc -o grpc-namespaces.ts *.proto

实现您的类型安全服务器返回 Observable

import { of } from 'rxjs';
import { serverBuilder } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const server = serverBuilder<sample.ServerBuilder>('sample.proto', 'sample')
// Add implementation
server.addGreeter({
  sayHello(request: sample.HelloRequest) {
    return of({
      message: 'Hello ' + request.name
    });
  }
})
// Start the server to listen on port 50051
server.start('0.0.0.0:50051');

从客户端:

import { clientFactory } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const Services = clientFactory<sample.ClientFactory>('sample.proto', 'sample');
// Create a client connecting to the server
const services = new Services('localhost:50051');
// Get a client for the Greeter service
const greeter = services.getGreeter();

// Call the service by passing a sample.HelloRequest
greeter.sayHello({ name: 'world' }).forEach(response => {
  console.log(`Greeting: ${response.message}`);
});

Generated interfaces

import { Observable } from 'rxjs';

export namespace sample {

  export interface ClientFactory {
    getGreeter(): sample.Greeter;
  }

  export interface ServerBuilder {
    addGreeter(impl: sample.Greeter): sample.ServerBuilder;
  }

  export interface Greeter {
    sayHello(request: sample.HelloRequest): Observable<sample.HelloReply>;
  }

  export interface HelloRequest {
    name?: string;
  }

  export interface HelloReply {
    message?: string;
  }

}

Examples

您可以在examples 文件夹 中看到一个简单的示例项目。

Build Statusnpm version

rxjs-grpc

Installation

$ npm install rxjs-grpc rxjs grpc

Quickstart

Create your protobuf definition file sample.proto:

syntax = "proto3";

package sample;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

Generate your TypeScript interfaces:

$ ./node_modules/.bin/rxjs-grpc -o grpc-namespaces.ts *.proto

Implement your typesafe server returning Observable<sample.HelloReply>:

import { of } from 'rxjs';
import { serverBuilder } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const server = serverBuilder<sample.ServerBuilder>('sample.proto', 'sample')
// Add implementation
server.addGreeter({
  sayHello(request: sample.HelloRequest) {
    return of({
      message: 'Hello ' + request.name
    });
  }
})
// Start the server to listen on port 50051
server.start('0.0.0.0:50051');

Call it from a client:

import { clientFactory } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const Services = clientFactory<sample.ClientFactory>('sample.proto', 'sample');
// Create a client connecting to the server
const services = new Services('localhost:50051');
// Get a client for the Greeter service
const greeter = services.getGreeter();

// Call the service by passing a sample.HelloRequest
greeter.sayHello({ name: 'world' }).forEach(response => {
  console.log(`Greeting: ${response.message}`);
});

Generated interfaces

import { Observable } from 'rxjs';

export namespace sample {

  export interface ClientFactory {
    getGreeter(): sample.Greeter;
  }

  export interface ServerBuilder {
    addGreeter(impl: sample.Greeter): sample.ServerBuilder;
  }

  export interface Greeter {
    sayHello(request: sample.HelloRequest): Observable<sample.HelloReply>;
  }

  export interface HelloRequest {
    name?: string;
  }

  export interface HelloReply {
    message?: string;
  }

}

Examples

You can see a simple example project in the examples folder.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文