Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit cc48dd0

Browse files
committed
v0.4
update example code to also include the new setResourceName: method allow socket.io resource to be renamed from outside. close #80. cleaned up URL schemas and their usage try forced disconnect in SocketTester example adjust the forced disconnect method a bit change deployment target to iOS 5 (because socket-rocket needs it and we're using __weak now) update submodules SocketIO: don't use NSURLConnection delegate property in -dealloc - not available without BlocksKit Synchronous disconnect Fixed for sending events before socket is connected Ensure to cleanup properly in -disconnect & -dealloc - fixes crashes Add initial connection timeout Fixed disconnect error loop Changed timeout timer from NSTimer to GCD timer to avoid retain cycle.
2 parents 79fac8e + ea22d26 commit cc48dd0

16 files changed

+235
-147
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Socket.IO / Objective C Library (ARC version)
1+
# Socket.IO / Objective C Library
22

33
Interface to communicate between Objective C and [Socket.IO](http://socket.io/)
44
with the help of websockets or [Long-Polling](http://en.wikipedia.org/wiki/Push_technology#Long_polling). Originally based on fpotter's [socketio-cocoa](https://github.com/fpotter/socketio-cocoa)
@@ -11,6 +11,10 @@
1111
* [json-framework](https://github.com/stig/json-framework/) (optional)
1212
* [JSONKit](https://github.com/johnezang/JSONKit/) (optional)
1313

14+
## Requirements
15+
16+
As of version 0.4, this library requires at least OS X 10.7 or iOS 5.0.
17+
1418

1519
## Non-ARC version
1620

@@ -119,7 +123,7 @@ Different Socket Libraries + Error Handling by taiyangc <https://github.com/taiy
119123

120124
(The MIT License)
121125

122-
Copyright (c) 2011-12 Philipp Kyeck <http://beta-interactive.de>
126+
Copyright (c) 2011-13 Philipp Kyeck <http://beta-interactive.de>
123127

124128
Permission is hereby granted, free of charge, to any person obtaining a copy
125129
of this software and associated documentation files (the "Software"), to deal

SocketIO.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIO.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
@@ -79,7 +79,7 @@ typedef enum {
7979

8080
// heartbeat
8181
NSTimeInterval _heartbeatTimeout;
82-
NSTimer *_timeout;
82+
dispatch_source_t _timeout;
8383

8484
NSMutableArray *_queue;
8585

@@ -107,7 +107,10 @@ typedef enum {
107107
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port;
108108
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params;
109109
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint;
110+
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint withConnectionTimeout: (NSTimeInterval) connectionTimeout;
111+
110112
- (void) disconnect;
113+
- (void) disconnectForced;
111114

112115
- (void) sendMessage:(NSString *)data;
113116
- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function;
@@ -117,4 +120,6 @@ typedef enum {
117120
- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function;
118121
- (void) sendAcknowledgement:(NSString*)pId withArgs:(NSArray *)data;
119122

123+
- (void) setResourceName:(NSString *)name;
124+
120125
@end

SocketIO.m

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIO.m
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
@@ -37,10 +37,11 @@
3737
#define DEBUGLOG(...)
3838
#endif
3939

40-
static NSString* kInsecureHandshakeURL = @"http://%@/socket.io/1/?t=%d%@";
41-
static NSString* kInsecureHandshakePortURL = @"http://%@:%d/socket.io/1/?t=%d%@";
42-
static NSString* kSecureHandshakePortURL = @"https://%@:%d/socket.io/1/?t=%d%@";
43-
static NSString* kSecureHandshakeURL = @"https://%@/socket.io/1/?t=%d%@";
40+
static NSString* kResourceName = @"socket.io";
41+
static NSString* kHandshakeURL = @"%@://%@%@/%@/1/?t=%d%@";
42+
static NSString* kForceDisconnectURL = @"%@://%@%@/%@/1/xhr-polling/%@?disconnect";
43+
44+
float const defaultConnectionTimeout = 10.0f;
4445

4546
NSString* const SocketIOError = @"SocketIOError";
4647
NSString* const SocketIOException = @"SocketIOException";
@@ -95,15 +96,27 @@ - (id) initWithDelegate:(id<SocketIODelegate>)delegate
9596

9697
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port
9798
{
98-
[self connectToHost:host onPort:port withParams:nil withNamespace:@""];
99+
[self connectToHost:host onPort:port withParams:nil withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
99100
}
100101

101102
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params
102103
{
103-
[self connectToHost:host onPort:port withParams:params withNamespace:@""];
104+
[self connectToHost:host onPort:port withParams:params withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
104105
}
105106

106-
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint
107+
- (void) connectToHost:(NSString *)host
108+
onPort:(NSInteger)port
109+
withParams:(NSDictionary *)params
110+
withNamespace:(NSString *)endpoint
111+
{
112+
[self connectToHost:host onPort:port withParams:params withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
113+
}
114+
115+
- (void) connectToHost:(NSString *)host
116+
onPort:(NSInteger)port
117+
withParams:(NSDictionary *)params
118+
withNamespace:(NSString *)endpoint
119+
withConnectionTimeout:(NSTimeInterval)connectionTimeout
107120
{
108121
if (!_isConnected && !_isConnecting) {
109122
_isConnecting = YES;
@@ -120,31 +133,22 @@ - (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDic
120133
}];
121134

122135
// do handshake via HTTP request
123-
NSString *s;
124-
NSString *format;
125-
if (_port) {
126-
format = _useSecure ? kSecureHandshakePortURL : kInsecureHandshakePortURL;
127-
s = [NSString stringWithFormat:format, _host, _port, rand(), query];
128-
}
129-
else {
130-
format = _useSecure ? kSecureHandshakeURL : kInsecureHandshakeURL;
131-
s = [NSString stringWithFormat:format, _host, rand(), query];
132-
}
133-
DEBUGLOG(@"Connecting to socket with URL: %@", s);
134-
NSURL *url = [NSURL URLWithString:s];
136+
NSString *protocol = _useSecure ? @"https" : @"http";
137+
NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @"";
138+
NSString *handshakeUrl = [NSString stringWithFormat:kHandshakeURL, protocol, _host, port, kResourceName, rand(), query];
139+
140+
DEBUGLOG(@"Connecting to socket with URL: %@", handshakeUrl);
135141
query = nil;
136-
137142

138143
// make a request
139-
NSURLRequest *request = [NSURLRequest requestWithURL:url
144+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:handshakeUrl]
140145
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
141-
timeoutInterval:10.0];
146+
timeoutInterval:connectionTimeout];
142147

143-
_handshake = [[NSURLConnection alloc] initWithRequest:request
144-
delegate:self startImmediately:NO];
145-
[_handshake scheduleInRunLoop:[NSRunLoop mainRunLoop]
146-
forMode:NSDefaultRunLoopMode];
148+
_handshake = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
149+
[_handshake scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
147150
[_handshake start];
151+
148152
if (_handshake) {
149153
_httpRequestData = [NSMutableData data];
150154
}
@@ -162,9 +166,31 @@ - (void) disconnect
162166
}
163167
else if (_isConnecting) {
164168
[_handshake cancel];
169+
[self onDisconnect: nil];
165170
}
166171
}
167172

173+
- (void) disconnectForced
174+
{
175+
NSString *protocol = [self useSecure] ? @"https" : @"http";
176+
NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @"";
177+
NSString *urlString = [NSString stringWithFormat:kForceDisconnectURL, protocol, _host, port, kResourceName, _sid];
178+
NSURL *url = [NSURL URLWithString:urlString];
179+
DEBUGLOG(@"Force disconnect at: %@", urlString);
180+
181+
NSURLRequest *request = [NSURLRequest requestWithURL:url];
182+
NSError *error = nil;
183+
NSHTTPURLResponse *response = nil;
184+
185+
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
186+
187+
if (error || [response statusCode] != 200) {
188+
DEBUGLOG(@"Error during disconnect: %@", error);
189+
}
190+
191+
[self onDisconnect:error];
192+
}
193+
168194
- (void) sendMessage:(NSString *)data
169195
{
170196
[self sendMessage:data withAcknowledge:nil];
@@ -224,13 +250,19 @@ - (void) sendAcknowledgement:(NSString *)pId withArgs:(NSArray *)data
224250
[self send:packet];
225251
}
226252

253+
- (void) setResourceName:(NSString *)name
254+
{
255+
kResourceName = [name copy];
256+
}
257+
227258
# pragma mark -
228259
# pragma mark private methods
229260

230261
- (void) sendDisconnect
231262
{
232263
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"disconnect"];
233264
[self send:packet];
265+
[self onDisconnect:nil];
234266
}
235267

236268
- (void) sendConnect
@@ -246,7 +278,11 @@ - (void) sendHeartbeat
246278
}
247279

248280
- (void) send:(SocketIOPacket *)packet
249-
{
281+
{
282+
if (![self isConnected] && ![self isConnecting]) {
283+
DEBUGLOG(@"Already disconnected!");
284+
return;
285+
}
250286
DEBUGLOG(@"send()");
251287
NSNumber *type = [packet typeAsNumber];
252288
NSMutableArray *encoded = [NSMutableArray arrayWithObject:type];
@@ -359,6 +395,11 @@ - (void) removeAcknowledgeForKey:(NSString *)key
359395

360396
- (void) onTimeout
361397
{
398+
if (_timeout) {
399+
dispatch_source_cancel(_timeout);
400+
_timeout = NULL;
401+
}
402+
362403
DEBUGLOG(@"Timed out waiting for heartbeat.");
363404
[self onDisconnect:[NSError errorWithDomain:SocketIOError
364405
code:SocketIOHeartbeatTimeout
@@ -368,16 +409,29 @@ - (void) onTimeout
368409
- (void) setTimeout
369410
{
370411
DEBUGLOG(@"start/reset timeout");
371-
if (_timeout != nil) {
372-
[_timeout invalidate];
373-
_timeout = nil;
412+
if (_timeout) {
413+
dispatch_source_cancel(_timeout);
414+
_timeout = NULL;
374415
}
375416

376-
_timeout = [NSTimer scheduledTimerWithTimeInterval:_heartbeatTimeout
377-
target:self
378-
selector:@selector(onTimeout)
379-
userInfo:nil
380-
repeats:NO];
417+
_timeout = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
418+
0,
419+
0,
420+
dispatch_get_main_queue());
421+
422+
dispatch_source_set_timer(_timeout,
423+
dispatch_time(DISPATCH_TIME_NOW, _heartbeatTimeout * NSEC_PER_SEC),
424+
0,
425+
0);
426+
427+
__weak SocketIO *weakSelf = self;
428+
429+
dispatch_source_set_event_handler(_timeout, ^{
430+
[weakSelf onTimeout];
431+
});
432+
433+
dispatch_resume(_timeout);
434+
381435
}
382436

383437

@@ -556,9 +610,9 @@ - (void) onDisconnect:(NSError *)error
556610
[_queue removeAllObjects];
557611

558612
// Kill the heartbeat timer
559-
if (_timeout != nil) {
560-
[_timeout invalidate];
561-
_timeout = nil;
613+
if (_timeout) {
614+
dispatch_source_cancel(_timeout);
615+
_timeout = NULL;
562616
}
563617

564618
// Disconnect the websocket, just in case
@@ -762,14 +816,20 @@ - (void) connection:(NSURLConnection *)connection
762816

763817
- (void) dealloc
764818
{
819+
[_handshake cancel];
820+
_handshake = nil;
821+
765822
_host = nil;
766823
_sid = nil;
767824
_endpoint = nil;
768825

826+
_transport.delegate = nil;
769827
_transport = nil;
770828

771-
[_timeout invalidate];
772-
_timeout = nil;
829+
if (_timeout) {
830+
dispatch_source_cancel(_timeout);
831+
_timeout = NULL;
832+
}
773833

774834
_queue = nil;
775835
_acks = nil;

SocketIOJSONSerialization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOJSONSerialization.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOJSONSerialization.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOJSONSerialization.m
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOPacket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOPacket.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOPacket.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOPacket.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOTransport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOTransport.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOTransportWebsocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOTransportWebsocket.h
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

SocketIOTransportWebsocket.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SocketIOTransportWebsocket.m
3-
// v0.3.3 ARC
3+
// v0.4 ARC
44
//
55
// based on
66
// socketio-cocoa https://github.com/fpotter/socketio-cocoa

0 commit comments

Comments
 (0)