RCTPackagerClient.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTPackagerClient.h>
  8. #import <React/RCTLog.h>
  9. #import <React/RCTReconnectingWebSocket.h>
  10. #import <React/RCTUtils.h>
  11. #if RCT_DEV // Only supported in dev mode
  12. const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION = 2;
  13. @implementation RCTPackagerClientResponder {
  14. id _msgId;
  15. __weak RCTReconnectingWebSocket *_socket;
  16. }
  17. - (instancetype)initWithId:(id)msgId socket:(RCTReconnectingWebSocket *)socket
  18. {
  19. if (self = [super init]) {
  20. _msgId = msgId;
  21. _socket = socket;
  22. }
  23. return self;
  24. }
  25. - (void)respondWithResult:(id)result
  26. {
  27. NSDictionary<NSString *, id> *msg = @{
  28. @"version" : @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
  29. @"id" : _msgId,
  30. @"result" : result,
  31. };
  32. NSError *jsError = nil;
  33. NSString *message = RCTJSONStringify(msg, &jsError);
  34. if (jsError) {
  35. RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
  36. } else {
  37. [_socket send:message];
  38. }
  39. }
  40. - (void)respondWithError:(id)error
  41. {
  42. NSDictionary<NSString *, id> *msg = @{
  43. @"version" : @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
  44. @"id" : _msgId,
  45. @"error" : error,
  46. };
  47. NSError *jsError = nil;
  48. NSString *message = RCTJSONStringify(msg, &jsError);
  49. if (jsError) {
  50. RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
  51. } else {
  52. [_socket send:message];
  53. }
  54. }
  55. @end
  56. #endif