forked from francoisTemasys/socket.IO-objc
-
Notifications
You must be signed in to change notification settings - Fork 24
/
SocketIOPacket.m
246 lines (213 loc) · 5.77 KB
/
SocketIOPacket.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// SocketIOPacket.h
// v0.5.1
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
// by Fred Potter <[email protected]>
//
// using
// https://github.com/square/SocketRocket
//
// reusing some parts of
// /socket.io/socket.io.js
//
// Created by Philipp Kyeck http://beta-interactive.de
//
// With help from
// https://github.com/pkyeck/socket.IO-objc/blob/master/CONTRIBUTORS.md
//
#import "SocketIOPacket.h"
#import "SocketIOJSONSerialization.h"
@implementation SocketIOPacket
@synthesize type, pId, name, ack, data, args, endpoint;
- (id) init
{
self = [super init];
if (self)
{
_separator = @":";
_types = [NSArray arrayWithObjects: @"disconnect",
@"connect",
@"heartbeat",
@"message",
@"json",
@"event",
@"ack",
@"error",
@"noop",
nil];
}
return self;
}
- (id) initWithType:(NSString *)packetType
{
self = [self init];
if (self) {
self.type = packetType;
}
return self;
}
- (id) initWithTypeIndex:(NSUInteger)index
{
self = [self init];
if (self) {
self.type = [self typeForIndex:index];
}
return self;
}
- (id) dataAsJSON
{
if (self.data) {
NSData *utf8Data = [self.data dataUsingEncoding:NSUTF8StringEncoding];
return [SocketIOJSONSerialization objectFromJSONData:utf8Data error:nil];
}
else {
return nil;
}
}
- (NSString *) toString
{
NSNumber *typeAsNumber = [self typeAsNumber];
NSMutableArray *encoded = [NSMutableArray arrayWithObject:typeAsNumber];
NSNumber *typeNumber = [self typeAsNumber];
if (!(self.endpoint == nil || [@"/" isEqualToString:self.endpoint]) && [typeNumber intValue] != 6 && [typeNumber intValue] != 2)
{
[encoded addObject:[self.endpoint stringByAppendingString:@","]];
}
NSString *pIdL = self.pId != nil ? self.pId : @"";
if( !([self isKindOfClass:[SocketIOPacketV10x class]]) ){
if ([self.ack isEqualToString:@"data"])
{
//pIdL = [pIdL stringByAppendingString:@"+"];
}
} else {
// Engine.IO 1.0 expects payload with the ping packet
if ([typeAsNumber intValue] == 2) {
[encoded addObject:@"probe"];
}
}
// Do not write pid for acknowledgements
if ([typeNumber intValue] != 6) {
[encoded addObject:pIdL];
}
// Add the end point for the namespace to be used, as long as it is not
// an ACK, heartbeat, or disconnect packet
/*if ([type intValue] != 6 && [type intValue] != 2 && [type intValue] != 0) {
[encoded addObject:endpoint];
}
else {
[encoded addObject:@""];
}*/
if (data != nil)
{
NSString *ackpId = @"";
// This is an acknowledgement packet, so, prepend the ack pid to the data
if ([typeNumber intValue] == 6)
{
if( !([self isKindOfClass:[SocketIOPacketV10x class]]) )
ackpId = [NSString stringWithFormat:@":%@%@", pIdL, @"+"];
}
[encoded addObject:[NSString stringWithFormat:@"%@%@", ackpId, data]];
}
return [encoded componentsJoinedByString:_separator];
}
- (NSNumber *) typeAsNumber
{
NSUInteger index = [_types indexOfObject:self.type];
NSNumber *num = [NSNumber numberWithUnsignedInteger:index];
return num;
}
- (NSString *) typeForIndex:(NSUInteger)index
{
return [_types objectAtIndex:index];
}
+ (SocketIOPacket *) createPacketWithType:(NSString *)type
version:(SocketIOVersion) version
{
switch (version) {
case V09x:
return [[SocketIOPacket alloc] initWithType:type];
break;
case V10x:
return [[SocketIOPacketV10x alloc] initWithType:type];
break;
}
}
+ (SocketIOPacket *) createPacketWithTypeIndex:(NSUInteger) type
version:(SocketIOVersion) version
{
switch (version) {
case V09x:
return [[SocketIOPacket alloc] initWithTypeIndex:type];
break;
case V10x:
return [[SocketIOPacketV10x alloc] initWithTypeIndex:type];
break;
}
}
- (void) dealloc
{
_types = nil;
type = nil;
pId = nil;
name = nil;
ack = nil;
data = nil;
args = nil;
endpoint = nil;
}
@end
@implementation SocketIOPacketV10x
- (id) init
{
self = [super init];
_separator = @"";
_types = [NSArray arrayWithObjects: @"disconnected",
@"connected",
@"heartbeat",
@"pong",
@"message",
@"upgrade",
@"noop",
nil];
_typesMessage = [NSArray arrayWithObjects: @"connect",
@"disconnect",
@"event",
@"ack",
@"error",
@"binarevent",
@"binaryack",
nil];
return self;
}
- (NSNumber *) typeAsNumber
{
NSNumber *num = 0;
NSUInteger index;
if(_typesMessage != nil && (index = [_typesMessage indexOfObject:self.type]) != NSNotFound)
{
//it's a message type
index = [_typesMessage indexOfObject:self.type];
num = @([num integerValue] + 40);
}
else
{
index = [_types indexOfObject:self.type];
}
num = @([num integerValue] + [[NSNumber numberWithUnsignedInteger:index] integerValue]);
return num;
}
- (void) dealloc
{
_typesMessage = nil;
_types = nil;
type = nil;
pId = nil;
name = nil;
ack = nil;
data = nil;
args = nil;
endpoint = nil;
}
@end