Client.asyncInvoke(url, request [,params])
异步调用给定方法的 unary RPC 请求。
要调用的给定方法必须事先通过 Client.load() 函数加载其 RPC schema,否则将抛出错误。
必须先调用 Client.connect(),然后才能发起请求,否则将抛出错误。
参数 | 类型 | 描述 |
---|---|---|
url | string | 要调用的 gRPC 方法 URL,格式为 /package.Service/Method ,例如 /google.cloud.language.v1.LanguageService/AnalyzeSentiment 。开头的斜杠 / 是可选的。 |
request | object | 规范请求对象,根据 Protobuf JSON 映射。 |
params (可选) | object | Params 对象,包含额外的请求参数。 |
返回值
类型 | 描述 |
---|---|
包含 Response 的 Promise | gRPC Response 对象。 |
示例
import grpc from 'k6/net/grpc';
import { check } from 'k6';
const client = new grpc.Client();
client.load([], 'routeguide.proto');
export default () => {
client.connect('localhost:10000', { plaintext: true });
client
.asyncInvoke('main.RouteGuide/GetFeature', {
latitude: 410248224,
longitude: -747127767,
})
.then((response) => {
check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK });
console.log(response.message.name);
// output: 3 Hasta Way, Newton, NJ 07860, USA
client.close();
});
};