问题
As described in this issue, we can't implement a gRPC client using the Node implementation because "RN is not pure Node".
So I started working on an Objective-C implementation using the Native Modules.
[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);
}
}];
Error :
RCTJSONStringify() encountered the following error: Invalid type in JSON write (RequestConfirmation)
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.
How can I keep the response as is and pass it to the resolve method ? Then I can decode it in my JS code.
Thanks.
EDIT 1 :
RequestConfirmation
is defined in my proto
file like this :
message RequestConfirmation {
string id = 1;
string message = 2;
}
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
回答1:
Maybe the following is a potential solution for this.
Improbable Engineering have coded:
- 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).
Quoting the Improbable project's own grpc-web-client page:
This library is intended for both JavaScript and TypeScript usage from either a browser or Node.js
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
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.
来源:https://stackoverflow.com/questions/44962290/implement-grpc-in-an-ios-react-native-app