Skip to content

Commit

Permalink
v0.4.1
Browse files Browse the repository at this point in the history
# By Menno Pruijssers (2) and others
  fix unix timecode bug in handshake url. close pkyeck#118.
  Instantiate errorInfo NSMutableDictionary using mutableCopy as suggested by Elshad. close pkyeck#125.
  introduce closed flag to XHR transport. fix pkyeck#130.
  Removed nill-checks
  Changed delegates from unsafe_unretained to weak
  Fix improper use of NSLocalizedDescriptionKey.
  • Loading branch information
pkyeck committed Nov 18, 2013
2 parents 26748e0 + 2af028d commit b753a4a
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 28 deletions.
6 changes: 3 additions & 3 deletions SocketIO.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIO.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -67,7 +67,7 @@ typedef enum {
NSString *_endpoint;
NSDictionary *_params;

__unsafe_unretained id<SocketIODelegate> _delegate;
__weak id<SocketIODelegate> _delegate;

NSObject <SocketIOTransport> *_transport;

Expand Down Expand Up @@ -100,7 +100,7 @@ typedef enum {
@property (nonatomic, readonly) NSTimeInterval heartbeatTimeout;
@property (nonatomic) BOOL useSecure;
@property (nonatomic, readonly) BOOL isConnected, isConnecting;
@property (nonatomic, unsafe_unretained) id<SocketIODelegate> delegate;
@property (nonatomic, weak) id<SocketIODelegate> delegate;
@property (nonatomic) BOOL returnAllDataFromAck;

- (id) initWithDelegate:(id<SocketIODelegate>)delegate;
Expand Down
10 changes: 6 additions & 4 deletions SocketIO.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIO.m
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -38,7 +38,7 @@
#endif

static NSString* kResourceName = @"socket.io";
static NSString* kHandshakeURL = @"%@://%@%@/%@/1/?t=%d%@";
static NSString* kHandshakeURL = @"%@://%@%@/%@/1/?t=%.0f%@";
static NSString* kForceDisconnectURL = @"%@://%@%@/%@/1/xhr-polling/%@?disconnect";

float const defaultConnectionTimeout = 10.0f;
Expand Down Expand Up @@ -135,7 +135,8 @@ - (void) connectToHost:(NSString *)host
// do handshake via HTTP request
NSString *protocol = _useSecure ? @"https" : @"http";
NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @"";
NSString *handshakeUrl = [NSString stringWithFormat:kHandshakeURL, protocol, _host, port, kResourceName, rand(), query];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
NSString *handshakeUrl = [NSString stringWithFormat:kHandshakeURL, protocol, _host, port, kResourceName, time, query];

DEBUGLOG(@"Connecting to socket with URL: %@", handshakeUrl);
query = nil;
Expand Down Expand Up @@ -676,7 +677,8 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
_isConnecting = NO;

if ([_delegate respondsToSelector:@selector(socketIO:onError:)]) {
NSMutableDictionary *errorInfo = [NSDictionary dictionaryWithObject:error forKey:NSLocalizedDescriptionKey];
NSMutableDictionary *errorInfo = [[NSDictionary dictionaryWithObject:error
forKey:NSUnderlyingErrorKey] mutableCopy];

NSError *err = [NSError errorWithDomain:SocketIOError
code:SocketIOHandshakeFailed
Expand Down
2 changes: 1 addition & 1 deletion SocketIOJSONSerialization.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOJSONSerialization.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down
2 changes: 1 addition & 1 deletion SocketIOJSONSerialization.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOJSONSerialization.m
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down
2 changes: 1 addition & 1 deletion SocketIOPacket.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOPacket.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down
2 changes: 1 addition & 1 deletion SocketIOPacket.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOPacket.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down
4 changes: 2 additions & 2 deletions SocketIOTransport.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOTransport.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -45,6 +45,6 @@
- (BOOL) isReady;
- (void) send:(NSString *)request;

@property (nonatomic, unsafe_unretained) id <SocketIOTransportDelegate> delegate;
@property (nonatomic, weak) id <SocketIOTransportDelegate> delegate;

@end
4 changes: 2 additions & 2 deletions SocketIOTransportWebsocket.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOTransportWebsocket.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -31,6 +31,6 @@
SRWebSocket *_webSocket;
}

@property (nonatomic, unsafe_unretained) id <SocketIOTransportDelegate> delegate;
@property (nonatomic, weak) id <SocketIOTransportDelegate> delegate;

@end
18 changes: 12 additions & 6 deletions SocketIOTransportWebsocket.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOTransportWebsocket.m
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -99,7 +99,9 @@ - (void) send:(NSString*)request

- (void) webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
[delegate onData:message];
if([delegate respondsToSelector:@selector(onData:)]) {
[delegate onData:message];
}
}

- (void) webSocketDidOpen:(SRWebSocket *)webSocket
Expand All @@ -111,7 +113,9 @@ - (void) webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
DEBUGLOG(@"Socket failed with error ... %@", [error localizedDescription]);
// Assuming this resulted in a disconnect
[delegate onDisconnect:error];
if([delegate respondsToSelector:@selector(onDisconnect:)]) {
[delegate onDisconnect:error];
}
}

- (void) webSocket:(SRWebSocket *)webSocket
Expand All @@ -120,9 +124,11 @@ - (void) webSocket:(SRWebSocket *)webSocket
wasClean:(BOOL)wasClean
{
DEBUGLOG(@"Socket closed. %@", reason);
[delegate onDisconnect:[NSError errorWithDomain:SocketIOError
code:SocketIOWebSocketClosed
userInfo:nil]];
if([delegate respondsToSelector:@selector(onDisconnect:)]) {
[delegate onDisconnect:[NSError errorWithDomain:SocketIOError
code:SocketIOWebSocketClosed
userInfo:nil]];
}
}

@end
6 changes: 4 additions & 2 deletions SocketIOTransportXHR.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOTransportXHR.h
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -30,8 +30,10 @@
NSString *_url;
NSMutableData *_data;
NSMutableDictionary *_polls;
BOOL _isClosed;
}

@property (nonatomic, unsafe_unretained) id <SocketIOTransportDelegate> delegate;
@property (nonatomic, weak) id <SocketIOTransportDelegate> delegate;
@property (nonatomic) BOOL isClosed;

@end
19 changes: 14 additions & 5 deletions SocketIOTransportXHR.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIOTransportXHR.m
// v0.4.0.1 ARC
// v0.4.1 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -45,7 +45,8 @@ - (void) poll:(NSString *)data retryNumber:(int)retry;

@implementation SocketIOTransportXHR

@synthesize delegate;
@synthesize delegate,
isClosed = _isClosed;

- (id) initWithDelegate:(id<SocketIOTransportDelegate>)delegate_
{
Expand Down Expand Up @@ -83,6 +84,8 @@ - (void) close
[conn cancel];
}
[_polls removeAllObjects];

_isClosed = YES;
}

- (BOOL) isReady
Expand All @@ -101,6 +104,10 @@ - (void) send:(NSString *)request

- (void) checkAndStartPoll
{
if (_isClosed) {
return;
}

BOOL restart = NO;
// no polls currently running -> start one
if ([_polls count] == 0) {
Expand Down Expand Up @@ -240,9 +247,11 @@ - (void) connectionDidFinishLoading:(NSURLConnection *)connection

if (![message isEqualToString:@"1"]) {
NSArray *messages = [self packetsFromPayload:message];
[messages enumerateObjectsUsingBlock:^(NSString *message, NSUInteger idx, BOOL *stop) {
[delegate onData:message];
}];
if([delegate respondsToSelector:@selector(onData:)]) {
[messages enumerateObjectsUsingBlock:^(NSString *message, NSUInteger idx, BOOL *stop) {
[delegate onData:message];
}];
}
}

// remove current connection from pool
Expand Down

0 comments on commit b753a4a

Please sign in to comment.