WebSocket.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #import <Foundation/Foundation.h>
  2. @class HTTPMessage;
  3. @class GCDAsyncSocket;
  4. #define WebSocketDidDieNotification @"WebSocketDidDie"
  5. @interface WebSocket : NSObject
  6. {
  7. dispatch_queue_t websocketQueue;
  8. HTTPMessage *request;
  9. GCDAsyncSocket *asyncSocket;
  10. NSData *term;
  11. BOOL isStarted;
  12. BOOL isOpen;
  13. BOOL isVersion76;
  14. id __unsafe_unretained delegate;
  15. }
  16. + (BOOL)isWebSocketRequest:(HTTPMessage *)request;
  17. - (id)initWithRequest:(HTTPMessage *)request socket:(GCDAsyncSocket *)socket;
  18. /**
  19. * Delegate option.
  20. *
  21. * In most cases it will be easier to subclass WebSocket,
  22. * but some circumstances may lead one to prefer standard delegate callbacks instead.
  23. **/
  24. @property (/* atomic */ unsafe_unretained) id delegate;
  25. /**
  26. * The WebSocket class is thread-safe, generally via it's GCD queue.
  27. * All public API methods are thread-safe,
  28. * and the subclass API methods are thread-safe as they are all invoked on the same GCD queue.
  29. **/
  30. @property (nonatomic, readonly) dispatch_queue_t websocketQueue;
  31. /**
  32. * Public API
  33. *
  34. * These methods are automatically called by the HTTPServer.
  35. * You may invoke the stop method yourself to close the WebSocket manually.
  36. **/
  37. - (void)start;
  38. - (void)stop;
  39. /**
  40. * Public API
  41. *
  42. * Sends a message over the WebSocket.
  43. * This method is thread-safe.
  44. **/
  45. - (void)sendMessage:(NSString *)msg;
  46. /**
  47. * Public API
  48. *
  49. * Sends a message over the WebSocket.
  50. * This method is thread-safe.
  51. **/
  52. - (void)sendData:(NSData *)msg;
  53. /**
  54. * Subclass API
  55. *
  56. * These methods are designed to be overriden by subclasses.
  57. **/
  58. - (void)didOpen;
  59. - (void)didReceiveMessage:(NSString *)msg;
  60. - (void)didClose;
  61. @end
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. #pragma mark -
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. /**
  66. * There are two ways to create your own custom WebSocket:
  67. *
  68. * - Subclass it and override the methods you're interested in.
  69. * - Use traditional delegate paradigm along with your own custom class.
  70. *
  71. * They both exist to allow for maximum flexibility.
  72. * In most cases it will be easier to subclass WebSocket.
  73. * However some circumstances may lead one to prefer standard delegate callbacks instead.
  74. * One such example, you're already subclassing another class, so subclassing WebSocket isn't an option.
  75. **/
  76. @protocol WebSocketDelegate
  77. @optional
  78. - (void)webSocketDidOpen:(WebSocket *)ws;
  79. - (void)webSocket:(WebSocket *)ws didReceiveMessage:(NSString *)msg;
  80. - (void)webSocketDidClose:(WebSocket *)ws;
  81. @end