PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/iOS/UnittWebSocketClient/tags/0.9.2.0/UnittWebSocketClient/WebSocket00.h

http://unitt.googlecode.com/
C Header | 180 lines | 57 code | 32 blank | 91 comment | 0 complexity | 9613e56ef2d1340a32eff94f625968d3 MD5 | raw file
  1. //
  2. // WebSocket.h
  3. // UnittWebSocketClient
  4. //
  5. // Created by Josh Morris on 5/3/11.
  6. // Copyright 2011 UnitT Software. All rights reserved.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. // use this file except in compliance with the License. You may obtain a copy of
  10. // the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. // License for the specific language governing permissions and limitations under
  18. // the License.
  19. //
  20. #import <Foundation/Foundation.h>
  21. #import "AsyncSocket.h"
  22. #import <Security/Security.h>
  23. #import <CommonCrypto/CommonDigest.h>
  24. #import <CommonCrypto/CommonCryptor.h>
  25. #import "NSData+Base64.h"
  26. enum
  27. {
  28. WebSocketReadyStateConnecting = 0, //The connection has not yet been established.
  29. WebSocketReadyStateOpen = 1, //The WebSocket connection is established and communication is possible.
  30. WebSocketReadyStateClosing = 2, //The connection is going through the closing handshake.
  31. WebSocketReadyStateClosed = 3 //The connection has been closed or could not be opened
  32. };
  33. typedef NSUInteger WebSocketReadyState;
  34. @protocol WebSocketDelegate <NSObject>
  35. /**
  36. * Called when the web socket connects and is ready for reading and writing.
  37. **/
  38. - (void) didOpen;
  39. /**
  40. * Called when the web socket closes. aError will be nil if it closes cleanly.
  41. **/
  42. - (void) didClose: (NSError*) aError;
  43. /**
  44. * Called when the web socket receives an error. Such an error can result in the
  45. socket being closed.
  46. **/
  47. - (void) didReceiveError: (NSError*) aError;
  48. /**
  49. * Called when the web socket receives a message.
  50. **/
  51. - (void) didReceiveMessage: (NSString*) aMessage;
  52. @end
  53. @interface WebSocket : NSObject
  54. {
  55. @private
  56. id<WebSocketDelegate> delegate;
  57. NSURL* url;
  58. NSString* origin;
  59. AsyncSocket* socket;
  60. WebSocketReadyState readystate;
  61. NSError* closingError;
  62. BOOL isSecure;
  63. NSTimeInterval timeout;
  64. NSDictionary* tlsSettings;
  65. NSArray* protocols;
  66. NSString* serverProtocol;
  67. NSString* key1;
  68. NSString* key2;
  69. NSData* key3;
  70. NSData* serverHandshake;
  71. BOOL verifyHandshake;
  72. }
  73. /**
  74. * Callback delegate for websocket events.
  75. **/
  76. @property(nonatomic,retain) id<WebSocketDelegate> delegate;
  77. /**
  78. * Timeout used for sending messages, not establishing the socket connection. A
  79. * value of -1 will result in no timeouts being applied.
  80. **/
  81. @property(nonatomic,assign) NSTimeInterval timeout;
  82. /**
  83. * URL of the websocket
  84. **/
  85. @property(nonatomic,readonly) NSURL* url;
  86. /**
  87. * Origin is used more in a browser setting, but it is intended to prevent cross-site scripting. If
  88. * nil, the client will fill this in using the url provided by the websocket.
  89. **/
  90. @property(nonatomic,readonly) NSString* origin;
  91. /**
  92. * Represents the state of the connection. It can have the following values:
  93. * - WebSocketReadyStateConnecting: The connection has not yet been established.
  94. * - WebSocketReadyStateOpen: The WebSocket connection is established and communication is possible.
  95. * - WebSocketReadyStateClosing: The connection is going through the closing handshake.
  96. * - WebSocketReadyStateClosed: The connection has been closed or could not be opened.
  97. **/
  98. @property(nonatomic,readonly) WebSocketReadyState readystate;
  99. /**
  100. * Settings for securing the connection using SSL/TLS.
  101. *
  102. * The possible keys and values for the TLS settings are well documented.
  103. * Some possible keys are:
  104. * - kCFStreamSSLLevel
  105. * - kCFStreamSSLAllowsExpiredCertificates
  106. * - kCFStreamSSLAllowsExpiredRoots
  107. * - kCFStreamSSLAllowsAnyRoot
  108. * - kCFStreamSSLValidatesCertificateChain
  109. * - kCFStreamSSLPeerName
  110. * - kCFStreamSSLCertificates
  111. * - kCFStreamSSLIsServer
  112. *
  113. * Please refer to Apple's documentation for associated values, as well as other possible keys.
  114. *
  115. * If the value is nil or an empty dictionary, then the websocket cannot be secured.
  116. **/
  117. @property(nonatomic,readonly) NSDictionary* tlsSettings;
  118. /**
  119. * The subprotocols supported by the client. Each subprotocol is represented by an NSString.
  120. **/
  121. @property(nonatomic,readonly) NSArray* protocols;
  122. /**
  123. * True if the client should verify the handshake values sent by the server. Since many of
  124. * the web socket servers may not have been updated to support this, set to false to ignore
  125. * and simply accept the connection to the server.
  126. **/
  127. @property(nonatomic,readonly) BOOL verifyHandshake;
  128. /**
  129. * The subprotocol selected by the server, nil if none was selected
  130. **/
  131. @property(nonatomic,readonly) NSString* serverProtocol;
  132. + (id) webSocketWithURLString:(NSString*) aUrlString delegate:(id<WebSocketDelegate>) aDelegate origin:(NSString*) aOrigin protocols:(NSArray*) aProtocols tlsSettings:(NSDictionary*) aTlsSettings verifyHandshake:(BOOL) aVerifyHandshake;
  133. - (id) initWithURLString:(NSString *) aUrlString delegate:(id<WebSocketDelegate>) aDelegate origin:(NSString*) aOrigin protocols:(NSArray*) aProtocols tlsSettings:(NSDictionary*) aTlsSettings verifyHandshake:(BOOL) aVerifyHandshake;
  134. /**
  135. * Connect the websocket and prepare it for reading and writing.
  136. **/
  137. - (void)open;
  138. /**
  139. * Finish all reads/writes and close the websocket.
  140. **/
  141. - (void)close;
  142. /**
  143. * Write a UTF-8 encoded NSString message to the websocket.
  144. **/
  145. - (void)send:(NSString*)message;
  146. extern NSString *const WebSocketException;
  147. extern NSString *const WebSocketErrorDomain;
  148. @end