SRWebSocket.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 enum {
  19. SR_CONNECTING = 0,
  20. SR_OPEN = 1,
  21. SR_CLOSING = 2,
  22. SR_CLOSED = 3,
  23. } SRReadyState;
  24. @class SRWebSocket;
  25. extern NSString *const SRWebSocketErrorDomain;
  26. #pragma mark - SRWebSocketDelegate
  27. @protocol SRWebSocketDelegate;
  28. #pragma mark - SRWebSocket
  29. @interface SRWebSocket : NSObject <NSStreamDelegate>
  30. @property (nonatomic, assign) id <SRWebSocketDelegate> delegate;
  31. @property (nonatomic, readonly) SRReadyState readyState;
  32. @property (nonatomic, readonly, retain) NSURL *url;
  33. // This returns the negotiated protocol.
  34. // It will be nil until after the handshake completes.
  35. @property (nonatomic, readonly, copy) NSString *protocol;
  36. // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol.
  37. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
  38. - (id)initWithURLRequest:(NSURLRequest *)request;
  39. // Some helper constructors.
  40. - (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
  41. - (id)initWithURL:(NSURL *)url;
  42. // Delegate queue will be dispatch_main_queue by default.
  43. // You cannot set both OperationQueue and dispatch_queue.
  44. - (void)setDelegateOperationQueue:(NSOperationQueue*) queue;
  45. - (void)setDelegateDispatchQueue:(dispatch_queue_t) queue;
  46. // By default, it will schedule itself on +[NSRunLoop SR_networkRunLoop] using defaultModes.
  47. - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  48. - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  49. // SRWebSockets are intended for one-time-use only. Open should be called once and only once.
  50. - (void)open;
  51. - (void)close;
  52. - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
  53. // Send a UTF8 String or Data.
  54. - (void)send:(id)data;
  55. @end
  56. #pragma mark - SRWebSocketDelegate
  57. @protocol SRWebSocketDelegate <NSObject>
  58. // message will either be an NSString if the server is using text
  59. // or NSData if the server is using binary.
  60. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  61. @optional
  62. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  63. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  64. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  65. @end
  66. #pragma mark - NSURLRequest (CertificateAdditions)
  67. @interface NSURLRequest (CertificateAdditions)
  68. @property (nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;
  69. @end
  70. #pragma mark - NSMutableURLRequest (CertificateAdditions)
  71. @interface NSMutableURLRequest (CertificateAdditions)
  72. @property (nonatomic, retain) NSArray *SR_SSLPinnedCertificates;
  73. @end
  74. #pragma mark - NSRunLoop (SRWebSocket)
  75. @interface NSRunLoop (SRWebSocket)
  76. + (NSRunLoop *)SR_networkRunLoop;
  77. @end