forked from pkyeck/socket.IO-objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketIOPacket.m
101 lines (88 loc) · 1.89 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
//
// 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) {
_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:(int)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;
}
}
- (NSNumber *) typeAsNumber
{
NSUInteger index = [_types indexOfObject:self.type];
NSNumber *num = [NSNumber numberWithUnsignedInteger:index];
return num;
}
- (NSString *) typeForIndex:(int)index
{
return [_types objectAtIndex:index];
}
- (void) dealloc
{
_types = nil;
type = nil;
pId = nil;
name = nil;
ack = nil;
data = nil;
args = nil;
endpoint = nil;
}
@end