node.js - 在 iOS React Native 应用程序中实现 gRPC

标签 node.js react-native protocol-buffers grpc react-native-ios

this issue 中所述,我们无法使用 Node 实现来实现 gRPC 客户端,因为“RN 不是纯 Node”。

所以我开始使用 Native Modules 进行 Objective-C 实现.

[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 没有找到任何方法将我的原型(prototype)消息转换为 JSON。

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

谢谢。

编辑 1:

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

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

然后在 Objective-C 中生成:

@interface RequestConfirmation : GPBMessage

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

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

@end

最佳答案

也许以下是一个潜在的解决方案。

Improbable Engineering已编码:

  • grpc-web - Typescript/Javascript、浏览器或 NodeJS!

除了不幸的命名之外,这似乎不是仅仅是the official, C++-using, grpc-web project的一个分支。 ).

引用 Improbable 项目自己的 grpc-web-client页:

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

Improbable 版本的可能好处似乎是:

  1. 它似乎没有使用本地代码(即没有 C/C++/Java)
  2. 它可以为 Node.JS 生成 Typescript(因此是 JavaScript)

所以,也许,当第 2 点与 NodeJS-on-RN 项目(例如 rn-nodeify)相结合时,我们可以让 GRPC-on-RN 工作。 .

如果您对此有任何成功,请提供反馈。

关于node.js - 在 iOS React Native 应用程序中实现 gRPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962290/

相关文章:

javascript - 无法加载资源 : the server responded with a status of 404 (Not Found) https://websitename. herokuapp.com/js/bootstrap.js

node.js - node.js 的进程内持久键值存储

javascript - 在组件中读取文本输入引用时,React Native refs 未定义

ruby - 如何在 Ruby 中使用 Protocol Buffers 3?

go - 无法找出 udemy 类(class)中缺少的 protoc 命令

nginx - Beanstalk 中的负载均衡 go 服务器

node.js - 在 Node.js 中保存大型文档时,MongoDB-mongoose 高 CPU 使用率

javascript - 从 Sequelize.js "query"(MySQL) 获取最后插入的 id

reactjs - 如何解决 'this navigator has both navigation and container props'错误

javascript - react native 类型错误 : Cannot read property 'createClient' of undefined