forked from fpotter/socketio-cocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketIoClient.h
142 lines (112 loc) · 4.27 KB
/
SocketIoClient.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// SocketIoClient.h
// SocketIoCocoa
//
// Created by Fred Potter on 11/11/10.
// Copyright 2010 Fred Potter. All rights reserved.
//
#import <Foundation/Foundation.h>
@class WebSocket;
@protocol SocketIoClientDelegate;
extern NSString *SocketIoClientErrorDomain;
enum {
/**
* ConnectionTimeout indicates an error waiting for the SocketIo sessionid
* handshake. It is also possible to receive an underlying connection timeout
* error (due to WebSocket handshake timeout or TCP timeout).
*/
SocketIoClientErrorConnectionTimeout,
/**
* If the heartbeat times out, the connection is closed after you receive this
* error.
*/
SocketIoClientErrorHeartbeatTimeout,
};
typedef enum {
SocketIoClientStateDisconnected,
SocketIoClientStateConnecting,
SocketIoClientStateConnected
} SocketIoClientState;
@interface SocketIoClient : NSObject {
NSString *_host;
NSInteger _port;
WebSocket *_webSocket;
NSTimeInterval _connectTimeout;
NSTimeInterval _heartbeatTimeout;
NSTimer *_timeout;
BOOL _isConnected;
BOOL _isConnecting;
NSString *_sessionId;
id<SocketIoClientDelegate> _delegate;
NSMutableArray *_queue;
}
@property (nonatomic, retain, readonly) NSString *host;
@property (nonatomic, readonly) NSInteger port;
@property (nonatomic, retain, readonly) NSString *sessionId;
@property (nonatomic, readonly) SocketIoClientState state;
@property (nonatomic, assign) id<SocketIoClientDelegate> delegate;
@property (nonatomic, assign) NSTimeInterval connectTimeout;
@property (nonatomic, assign) NSTimeInterval heartbeatTimeout;
- (id)initWithHost:(NSString *)host port:(NSInteger)port;
/**
* Attempt the connection. Delegate will receive either
* -socketIoClientDidConnect: or -socketIoClient:connectDidFailWithError:,
* unless connection is cancelled with |disconnect|.
*/
- (void)connect;
/**
* If state is SocketIoClientStateConnecting, immediately cancels the
* pending connection and delegate does not receive any notification.
* If state is SocketIoClientStateConnected, disconnects; delegate receives
* socketIoClientDidDisconnect:withError:, with nil for error.
*/
- (void)disconnect;
/**
* Rather than coupling this with any specific JSON library, you always
* pass in a string (either _the_ string, or the the JSON-encoded version
* of your object), and indicate whether or not you're passing a JSON object.
*/
- (void)send:(NSString *)data isJSON:(BOOL)isJSON;
/**
* Deprecated. Do not use.
*/
- (BOOL)isConnected;
- (BOOL)isConnecting;
@end
@protocol SocketIoClientDelegate <NSObject>
@optional
/**
* Message is always returned as a string, even when the message was meant to come
* in as a JSON object. Decoding the JSON is left as an exercise for the receiver.
*/
- (void)socketIoClient:(SocketIoClient *)client didReceiveMessage:(NSString *)message isJSON:(BOOL)isJSON;
/**
* Sent when the socket has connected and both WebSocket and SocketIo
* handshaking has completed.
*/
- (void)socketIoClientDidConnect:(SocketIoClient *)client;
/**
* If the socket was successfully opened (socketIoClientDidConnect: was called)
* but closes due to error or a call to -disconnect, this method is
* called. This is the last call |delegate| will receive unless the socket is
* reconnected with a call to -connect. It is safe to call |connect| from this
* method since the socket is already closed.
*
* If the disconnection was requested with a call to -disconnect, error will be
* nil. Otherwise, it will be set to the error that triggered disconnection.
* By the time this method is called, isConnecting and isConnected are both
* already NO.
*
* The domain of the error will be WebSocketErrorDomain or
* SocketIoClientErrorDomain.
*/
- (void)socketIoClient:(SocketIoClient *)client didDisconnectWithError:(NSError *)error;
/**
* If -connect was called, but the connection has failed due to a timeout,
* handshaking error, other networking problem, this method is called. This is
* the last call |delegate| will receive unless connection is retried with a
* call to |connect|. It is safe to call |connect| from this method.
**/
- (void)socketIoClient:(SocketIoClient *)client connectDidFailWithError:(NSError *)error;
- (void)socketIoClient:(SocketIoClient *)client didSendMessage:(NSString *)message isJSON:(BOOL)isJSON;
@end