-
Notifications
You must be signed in to change notification settings - Fork 2
/
SISocketIOClient.m
219 lines (147 loc) · 5.05 KB
/
SISocketIOClient.m
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// SISocketIO.m
// Socket.IO-iOS
//
// Created by masafumi yoshida on 2014/06/12.
// Copyright (c) 2014年 masafumi yoshida. All rights reserved.
//
#import "SISocketIOClient.h"
#import "SISocketIOTransportFactory.h"
@interface SISocketIOClient ()
@end
@implementation SISocketIOClient
- (id) init{
self = [super init];
if(self){
self.transports = @[@"polling",@"websocket"];
self.readyStatus = SISocketIOClientStatusClosed;
self.writeBuffer = [NSMutableArray array];
}
return self;
}
- (id) initWithHost:(NSString *)host onPort:(NSInteger)port{
self = [self init];
if(self){
self.host = host;
self.port = port;
}
return self;
}
- (id) initWithHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params{
self = [self init];
if(self){
}
return self;
}
- (id) initWithHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withPath:(NSString *)path{
self = [self init];
if(self){
}
return self;
}
-(void)send:(NSData*)data{
[self sendPacket:SIEngineIOPacketTypeMessage data:data];
}
-(void)sendPacket:(SIEngineIOPacketType)type data:(NSData*)data{
SIEngineIOPacket *packet =[[SIEngineIOPacket alloc] init];
packet.type = type;
packet.data = data;
[self.writeBuffer addObject:packet];
[self flush];
}
-(void)flush{
if(self.readyStatus != SISocketIOClientStatusClosed && self.transport.writable
&& !self.upgrading && self.writeBuffer.count > 0){
[self.transport send:self.writeBuffer];
self.prevBufferLen = self.writeBuffer.count;
}
else{
[self.transport send:self.writeBuffer];
self.prevBufferLen = self.writeBuffer.count;
}
}
-(void)open{
NSString *transport;
if (self.rememberUpgrade && [self.transports containsObject:@"websocket"]) {
transport = @"websocket";
} else {
transport = [self.transports firstObject];
}
self.readyStatus = SISocketIOClientStatusOpening;
self.transport = (SISocketIOTransportPolling*)[SISocketIOTransportFactory createTransport:transport];
self.transport.delegate = self;
self.transport.host = self.host;
self.transport.port = self.port;
self.transport.path = self.path ? self.path : @"socket.io";
[self.transport open];
}
#pragma socketiostransportdelegate
- (void) onPacket:(SISocketIOTransport*)transport packet:(SIEngineIOPacket*)packet{
switch (packet.type) {
case SIEngineIOPacketTypeOpen:
[self onHandshake:transport packet:packet];
break;
case SIEngineIOPacketTypeMessage:
[self.delegate socketIOClientOnPacket:self packet:packet];
break;
case SIEngineIOPacketTypeClose:
[self.delegate socketIOClientOnClose:self];
break;
case SIEngineIOPacketTypePing:
break;
case SIEngineIOPacketTypePong:
break;
case SIEngineIOPacketTypeNoop:
break;
case SIEngineIOPacketTypeUpgrade:
break;
case SIEngineIOPacketTypeError:
break;
default:
break;
}
}
-(NSArray*)filterUpgrades:(NSArray*)upgrades{
NSMutableArray *filtered = [NSMutableArray array];
for(NSString *transportName in upgrades){
if([self.transports containsObject:transportName]){
[filtered addObject:transportName];
}
}
return filtered;
}
-(void)onHandshake:(SISocketIOTransport*)transport packet:(SIEngineIOPacket*)packet{
if(packet.type == SIEngineIOPacketTypeOpen){
self.sid =[packet.message objectForKey:@"sid"];
self.upgrades = [self filterUpgrades:[packet.message objectForKey:@"upgrades"]];
self.pingInterval =[[packet.message objectForKey:@"pingInterval"] integerValue];
self.pingTimeout = [[packet.message objectForKey:@"pingTimeout"] integerValue];
transport.sid = self.sid;
[self onOpen:transport];
}
}
-(void)setPing{
}
-(void)ping{
}
-(void)onOpen:(SISocketIOTransport*)transport{
self.transport.readyStatus = SISocketIOTransportStatusOpen;
[self.delegate socketIOClientOnOpen:self];
/*
if([transport isEqual:self.transportPolling]){
self.transportWebSocket = (SISocketIOTransportWebSocket*)[SISocketIOTransportFactory createTransport:@"websocket"];
self.transportWebSocket.host = self.host;
self.transportWebSocket.port = self.port;
self.transportWebSocket.sid = self.transportPolling.sid;
self.transportWebSocket.path = self.path ? self.path : @"socket.io";
[self.transportWebSocket open];
}
else{
}
*/
}
-(void)onClose:(SISocketIOTransport*)transport{
}
-(void)onError:(SISocketIOTransport*)transport error:(NSError *)error{
}
@end