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