RCTSRWebSocket.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright 2012 Square Inc.
  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. #import <Foundation/Foundation.h>
  17. #import <Security/SecCertificate.h>
  18. typedef NS_ENUM(unsigned int, RCTSRReadyState) {
  19. RCTSR_CONNECTING = 0,
  20. RCTSR_OPEN = 1,
  21. RCTSR_CLOSING = 2,
  22. RCTSR_CLOSED = 3,
  23. };
  24. typedef NS_ENUM(NSInteger, RCTSRStatusCode) {
  25. RCTSRStatusCodeNormal = 1000,
  26. RCTSRStatusCodeGoingAway = 1001,
  27. RCTSRStatusCodeProtocolError = 1002,
  28. RCTSRStatusCodeUnhandledType = 1003,
  29. // 1004 reserved.
  30. RCTSRStatusNoStatusReceived = 1005,
  31. // 1004-1006 reserved.
  32. RCTSRStatusCodeInvalidUTF8 = 1007,
  33. RCTSRStatusCodePolicyViolated = 1008,
  34. RCTSRStatusCodeMessageTooBig = 1009,
  35. };
  36. @class RCTSRWebSocket;
  37. extern NSString *const RCTSRWebSocketErrorDomain;
  38. extern NSString *const RCTSRHTTPResponseErrorKey;
  39. #pragma mark - RCTSRWebSocketDelegate
  40. @protocol RCTSRWebSocketDelegate;
  41. #pragma mark - RCTSRWebSocket
  42. @interface RCTSRWebSocket : NSObject <NSStreamDelegate>
  43. @property (nonatomic, weak) id<RCTSRWebSocketDelegate> delegate;
  44. @property (nonatomic, readonly) RCTSRReadyState readyState;
  45. @property (nonatomic, readonly, strong) NSURL *url;
  46. // This returns the negotiated protocol.
  47. // It will be nil until after the handshake completes.
  48. @property (nonatomic, readonly, copy) NSString *protocol;
  49. // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol.
  50. - (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray<NSString *> *)protocols NS_DESIGNATED_INITIALIZER;
  51. - (instancetype)initWithURLRequest:(NSURLRequest *)request;
  52. // Some helper constructors.
  53. - (instancetype)initWithURL:(NSURL *)url protocols:(NSArray<NSString *> *)protocols;
  54. - (instancetype)initWithURL:(NSURL *)url;
  55. // Delegate queue will be dispatch_main_queue by default.
  56. // You cannot set both OperationQueue and dispatch_queue.
  57. - (void)setDelegateOperationQueue:(NSOperationQueue *)queue;
  58. - (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;
  59. // By default, it will schedule itself on +[NSRunLoop RCTSR_networkRunLoop] using defaultModes.
  60. - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  61. - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  62. // RCTSRWebSockets are intended for one-time-use only. Open should be called once and only once.
  63. - (void)open;
  64. - (void)close;
  65. - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
  66. // Send a UTF8 String or Data.
  67. - (void)send:(id)data;
  68. // Send Data (can be nil) in a ping message.
  69. - (void)sendPing:(NSData *)data;
  70. @end
  71. #pragma mark - RCTSRWebSocketDelegate
  72. @protocol RCTSRWebSocketDelegate <NSObject>
  73. // message will either be an NSString if the server is using text
  74. // or NSData if the server is using binary.
  75. - (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message;
  76. @optional
  77. - (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket;
  78. - (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error;
  79. - (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  80. - (void)webSocket:(RCTSRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
  81. @end
  82. #pragma mark - NSURLRequest (CertificateAdditions)
  83. @interface NSURLRequest (CertificateAdditions)
  84. @property (nonatomic, readonly, copy) NSArray *RCTSR_SSLPinnedCertificates;
  85. @end
  86. #pragma mark - NSMutableURLRequest (CertificateAdditions)
  87. @interface NSMutableURLRequest (CertificateAdditions)
  88. @property (nonatomic, copy) NSArray *RCTSR_SSLPinnedCertificates;
  89. @end
  90. #pragma mark - NSRunLoop (RCTSRWebSocket)
  91. @interface NSRunLoop (RCTSRWebSocket)
  92. + (NSRunLoop *)RCTSR_networkRunLoop;
  93. @end