在iOS React Native应用中实现gRPC [英] Implement gRPC in an iOS React Native app

查看:339
本文介绍了在iOS React Native应用中实现gRPC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题中所述,我们无法使用以下方法实现gRPC客户端因为" RN不是纯Node ",所以执行Node.

As described in this issue, we can't implement a gRPC client using the Node implementation because "RN is not pure Node".

所以我开始使用本机模块.

[service postWithRequest:request handler:^(RequestConfirmation * _Nullable response, NSError * _Nullable error) {
    if (response) {
        // This prints correctly in my JS console
        RCTLogInfo(@"%@", response.message);

        // This generates an error, see below
        resolve(response);

        // This works
        NSDictionary *formattedResponse = @{
            @"id": response.id_p,
            @"message": response.message
        };
        resolve(formattedResponse);
    } else {
        reject(@"error", @"An error occurred while saving", error);
    }
}];

错误:

RCTJSONStringify() encountered the following error: Invalid type in JSON write (RequestConfirmation)

如您所见,问题出在resolve方法.我想React无法找到将原始消息转换为JSON的任何方法.

As you can see the problem is with the resolve method. I suppose React does not find any way to convert my proto message to JSON.

如何将响应保持原样并将其传递给resolve方法? 然后,可以在我的JS代码中对其进行解码.

How can I keep the response as is and pass it to the resolve method ? Then I can decode it in my JS code.

谢谢.

RequestConfirmation在我的proto文件中定义如下:

RequestConfirmation is defined in my proto file like this :

message RequestConfirmation {
    string id = 1;
    string message = 2;
}

然后在Objective-C中生成它:

And then it is generated in Objective-C :

@interface RequestConfirmation : GPBMessage

@property(nonatomic, readwrite, copy, null_resettable) NSString *id_p;

@property(nonatomic, readwrite, copy, null_resettable) NSString *message;

@end

推荐答案

以下是对此的潜在解决方案.

Maybe the following is a potential solution for this.

不可实现的工程已编码:

  • grpc-web -打字稿/Javascript,浏览器或NodeJS!
  • grpc-web - Typescript/Javascript, browser or NodeJS!

除了不幸的命名之外, 不是 似乎只是

Aside from the unfortunate naming, this appears not to be a mere fork of the official, C++-using, grpc-web project).

引用不可能的项目自己的 grpc-web-client 页面:

Quoting the Improbable project's own grpc-web-client page:

该库既可用于浏览器,也可用于Node.js的JavaScript和TypeScript.

This library is intended for both JavaScript and TypeScript usage from either a browser or Node.js

Improbable版本可能带来的好处似乎是: 1.它似乎没有使用本机代码(即 no C/C ++/Java) 2.它可以为Node.JS生成Typescript(因此是JavaScript)

The possible benefits of Improbable's version seem to be: 1. It doesn't appear to use native code (i.e. no C/C++/Java) 2. It can generate Typescript (therefore JavaScript) for Node.JS

因此,当2号点与NodeJS-on-RN项目(例如

So, maybe, we could get GRPC-on-RN working, when point No. 2 is coupled with a NodeJS-on-RN project such as rn-nodeify.

如果成功,请提供反馈.

Please provide feedback, if you have any success with this.

这篇关于在iOS React Native应用中实现gRPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆