FSRWebSocket.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. } FSRReadyState;
  24. @class FSRWebSocket;
  25. extern NSString *const FSRWebSocketErrorDomain;
  26. @protocol FSRWebSocketDelegate;
  27. @interface FSRWebSocket : NSObject <NSStreamDelegate>
  28. @property (nonatomic, weak) id <FSRWebSocketDelegate> delegate;
  29. @property (nonatomic, readonly) FSRReadyState readyState;
  30. @property (nonatomic, readonly, retain) NSURL *url;
  31. // This returns the negotiated protocol.
  32. // It will be niluntil after the handshake completes.
  33. @property (nonatomic, readonly, copy) NSString *protocol;
  34. // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol
  35. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols queue:(dispatch_queue_t)queue googleAppID:(NSString*)googleAppID andUserAgent:(NSString *)userAgent;
  36. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
  37. - (id)initWithURLRequest:(NSURLRequest *)request queue:(dispatch_queue_t)queue googleAppID:(NSString*)googleAppID andUserAgent:(NSString *)userAgent;
  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 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. @protocol FSRWebSocketDelegate <NSObject>
  57. // message will either be an NSString if the server is using text
  58. // or NSData if the server is using binary
  59. - (void)webSocket:(FSRWebSocket *)webSocket didReceiveMessage:(id)message;
  60. @optional
  61. - (void)webSocketDidOpen:(FSRWebSocket *)webSocket;
  62. - (void)webSocket:(FSRWebSocket *)webSocket didFailWithError:(NSError *)error;
  63. - (void)webSocket:(FSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  64. @end
  65. @interface NSURLRequest (FCertificateAdditions)
  66. @property (nonatomic, retain, readonly) NSArray *FSR_SSLPinnedCertificates;
  67. @end
  68. @interface NSMutableURLRequest (FCertificateAdditions)
  69. @property (nonatomic, retain) NSArray *FSR_SSLPinnedCertificates;
  70. @end
  71. @interface NSRunLoop (FSRWebSocket)
  72. + (NSRunLoop *)FSR_networkRunLoop;
  73. @end