Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiRTCBridgeWrapper.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "JitsiRCTBridgeWrapper.h"
  17. /*
  18. * Wrapper around RCTBridge which also implements the RCTBridgeDelegate methods,
  19. * allowing us to specify where the bundles are loaded from.
  20. */
  21. @implementation JitsiRCTBridgeWrapper
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
  27. }
  28. return self;
  29. }
  30. #pragma mark helper methods for getting the packager URL
  31. #if DEBUG
  32. static NSURL *serverRootWithHost(NSString *host)
  33. {
  34. return [NSURL URLWithString:
  35. [NSString stringWithFormat:@"http://%@:8081/", host]];
  36. }
  37. - (BOOL)isPackagerRunning:(NSString *)host
  38. {
  39. NSURL *url = [serverRootWithHost(host)
  40. URLByAppendingPathComponent:@"status"];
  41. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  42. NSURLResponse *response;
  43. NSData *data = [NSURLConnection sendSynchronousRequest:request
  44. returningResponse:&response
  45. error:NULL];
  46. NSString *status = [[NSString alloc] initWithData:data
  47. encoding:NSUTF8StringEncoding];
  48. return [status isEqualToString:@"packager-status:running"];
  49. }
  50. - (NSString *)guessPackagerHost
  51. {
  52. static NSString *ipGuess;
  53. static dispatch_once_t onceToken;
  54. dispatch_once(&onceToken, ^{
  55. NSString *ipPath = [[NSBundle bundleForClass:self.class]
  56. pathForResource:@"ip" ofType:@"txt"];
  57. ipGuess = [[NSString stringWithContentsOfFile:ipPath
  58. encoding:NSUTF8StringEncoding
  59. error:nil]
  60. stringByTrimmingCharactersInSet:
  61. [NSCharacterSet newlineCharacterSet]];
  62. });
  63. NSString *host = ipGuess ?: @"localhost";
  64. if ([self isPackagerRunning:host]) {
  65. return host;
  66. }
  67. return nil;
  68. }
  69. #endif
  70. #pragma mark RCTBridgeDelegate methods
  71. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  72. {
  73. #if DEBUG
  74. /*
  75. * In debug mode, try to fetch the bundle from the packager, or fallback to
  76. * the one inside the framework. The IP address for the packager host is
  77. * fetched from the ip.txt file inside the framework.
  78. *
  79. * This duplicates some functionality present in RCTBundleURLProvider, but
  80. * that mode is not designed to work inside a framework, because all
  81. * resources are loaded from the main bundle.
  82. */
  83. NSString *host = [self guessPackagerHost];
  84. if (host != nil) {
  85. NSString *path = @"/index.ios.bundle";
  86. NSString *query = @"platform=ios&dev=true&minify=false";
  87. NSURLComponents *components
  88. = [NSURLComponents componentsWithURL:serverRootWithHost(host)
  89. resolvingAgainstBaseURL:NO];
  90. components.path = path;
  91. components.query = query;
  92. return components.URL;
  93. }
  94. #endif
  95. return [self fallbackSourceURLForBridge:bridge];
  96. }
  97. - (NSURL *)fallbackSourceURLForBridge:(RCTBridge *)bridge
  98. {
  99. return [[NSBundle bundleForClass:self.class] URLForResource:@"main"
  100. withExtension:@"jsbundle"];
  101. }
  102. @end