From fa5214dd2506ae255151260ecb5fb53e6cf017de Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Tue, 21 Oct 2014 14:39:00 -0400 Subject: [PATCH 1/3] Remove Public SRWebSocket Declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SocketRocket is a dependency, but not part of the public interface to Pusher. If you build an app using the OS X framework, you’ll get an error saying SRWebSocket cannot be found. --- Library/PTPusherConnection.h | 3 +-- Library/PTPusherConnection.m | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Library/PTPusherConnection.h b/Library/PTPusherConnection.h index fa9768e1..6be29718 100644 --- a/Library/PTPusherConnection.h +++ b/Library/PTPusherConnection.h @@ -7,7 +7,6 @@ // #import -#import "SRWebSocket.h" #import "PTPusherMacros.h" @class PTPusherConnection; @@ -32,7 +31,7 @@ typedef enum { PTPusherConnectionConnected } PTPusherConnectionState; -@interface PTPusherConnection : NSObject +@interface PTPusherConnection : NSObject @property (nonatomic, weak) id delegate; diff --git a/Library/PTPusherConnection.m b/Library/PTPusherConnection.m index b2023ea3..24ed5dbf 100644 --- a/Library/PTPusherConnection.m +++ b/Library/PTPusherConnection.m @@ -16,7 +16,7 @@ NSString *const PTPusherConnectionPingEvent = @"pusher:ping"; NSString *const PTPusherConnectionPongEvent = @"pusher:pong"; -@interface PTPusherConnection () +@interface PTPusherConnection () @property (nonatomic, copy) NSString *socketID; @property (nonatomic, assign) PTPusherConnectionState state; @property (nonatomic, strong) NSTimer *pingTimer; From 68663defdec4ac482d7a5c6300bd6cb8bf754375 Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Thu, 23 Oct 2014 12:27:19 -0400 Subject: [PATCH 2/3] Fix PTPusherMockConnection without SocketRocket Instead of calling the SocketRocket delegate methods directly, perform the socketID and state functions that would normally be performed by the SocketRocket delegate manually. --- Library/PTPusherMockConnection.m | 40 ++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Library/PTPusherMockConnection.m b/Library/PTPusherMockConnection.m index 423ef47d..37855722 100644 --- a/Library/PTPusherMockConnection.m +++ b/Library/PTPusherMockConnection.m @@ -10,11 +10,17 @@ #import "PTJSON.h" #import "PTPusherEvent.h" +@interface PTPusherMockConnection () +@property (nonatomic, copy) NSString *socketID; +@property (nonatomic, assign) PTPusherConnectionState state; +@end + @implementation PTPusherMockConnection { NSMutableArray *sentClientEvents; } @synthesize sentClientEvents; +@synthesize socketID = _socketID; - (id)init { @@ -26,7 +32,7 @@ - (id)init - (void)connect { - [self webSocketDidOpen:nil]; + self.state = PTPusherConnectionConnecting; NSInteger socketID = (NSInteger)[NSDate timeIntervalSinceReferenceDate]; @@ -36,7 +42,8 @@ - (void)connect - (void)disconnect { - [self webSocket:nil didCloseWithCode:0 reason:nil wasClean:YES]; + self.state = PTPusherConnectionDisconnecting; + self.socketID = nil; } - (void)send:(id)object @@ -53,26 +60,39 @@ - (void)simulateServerEventNamed:(NSString *)name data:(id)data - (void)simulateServerEventNamed:(NSString *)name data:(id)data channel:(NSString *)channelName { - NSMutableDictionary *event = [NSMutableDictionary dictionary]; + NSMutableDictionary *eventDict = [NSMutableDictionary dictionary]; - event[PTPusherEventKey] = name; + eventDict[PTPusherEventKey] = name; if (data) { - event[PTPusherDataKey] = data; + eventDict[PTPusherDataKey] = data; } if (channelName) { - event[PTPusherChannelKey] = channelName; + eventDict[PTPusherChannelKey] = channelName; } - NSString *message = [[PTJSON JSONParser] JSONStringFromObject:event]; - - [self webSocket:nil didReceiveMessage:message]; + NSString *message = [[PTJSON JSONParser] JSONStringFromObject:eventDict]; + + NSDictionary *messageDictionary = [[PTJSON JSONParser] objectFromJSONString:message]; + PTPusherEvent *event = [PTPusherEvent eventFromMessageDictionary:messageDictionary]; + + if ([event.name isEqualToString:PTPusherConnectionEstablishedEvent]) { + self.socketID = (event.data)[@"socket_id"]; + self.state = PTPusherConnectionConnected; + + [self.delegate pusherConnectionDidConnect:self]; + } + + [self.delegate pusherConnection:self didReceiveEvent:event]; } - (void)simulateUnexpectedDisconnection { - [self webSocket:nil didCloseWithCode:kPTPusherSimulatedDisconnectionErrorCode reason:nil wasClean:NO]; + self.state = PTPusherConnectionDisconnected; + self.socketID = nil; + // we always call this last, to prevent a race condition if the delegate calls 'connect' + [self.delegate pusherConnection:self didDisconnectWithCode:kPTPusherSimulatedDisconnectionErrorCode reason:nil wasClean:NO]; } #pragma mark - Client event handling From bd6595952d8637344dc7bec02b07377bb518220c Mon Sep 17 00:00:00 2001 From: Matt Thomas Date: Thu, 23 Oct 2014 14:10:26 -0400 Subject: [PATCH 3/3] Declare class extension conforming to SRWebSocket Rather than duplicating functionality. --- Library/PTPusherMockConnection.m | 40 ++++++++++---------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/Library/PTPusherMockConnection.m b/Library/PTPusherMockConnection.m index 37855722..ec5dd341 100644 --- a/Library/PTPusherMockConnection.m +++ b/Library/PTPusherMockConnection.m @@ -9,10 +9,9 @@ #import "PTPusherMockConnection.h" #import "PTJSON.h" #import "PTPusherEvent.h" +#import "SRWebSocket.h" -@interface PTPusherMockConnection () -@property (nonatomic, copy) NSString *socketID; -@property (nonatomic, assign) PTPusherConnectionState state; +@interface PTPusherConnection () @end @implementation PTPusherMockConnection { @@ -20,7 +19,6 @@ @implementation PTPusherMockConnection { } @synthesize sentClientEvents; -@synthesize socketID = _socketID; - (id)init { @@ -32,7 +30,7 @@ - (id)init - (void)connect { - self.state = PTPusherConnectionConnecting; + [self webSocketDidOpen:nil]; NSInteger socketID = (NSInteger)[NSDate timeIntervalSinceReferenceDate]; @@ -42,8 +40,7 @@ - (void)connect - (void)disconnect { - self.state = PTPusherConnectionDisconnecting; - self.socketID = nil; + [self webSocket:nil didCloseWithCode:0 reason:nil wasClean:YES]; } - (void)send:(id)object @@ -60,39 +57,26 @@ - (void)simulateServerEventNamed:(NSString *)name data:(id)data - (void)simulateServerEventNamed:(NSString *)name data:(id)data channel:(NSString *)channelName { - NSMutableDictionary *eventDict = [NSMutableDictionary dictionary]; + NSMutableDictionary *event = [NSMutableDictionary dictionary]; - eventDict[PTPusherEventKey] = name; + event[PTPusherEventKey] = name; if (data) { - eventDict[PTPusherDataKey] = data; + event[PTPusherDataKey] = data; } if (channelName) { - eventDict[PTPusherChannelKey] = channelName; + event[PTPusherChannelKey] = channelName; } - NSString *message = [[PTJSON JSONParser] JSONStringFromObject:eventDict]; - - NSDictionary *messageDictionary = [[PTJSON JSONParser] objectFromJSONString:message]; - PTPusherEvent *event = [PTPusherEvent eventFromMessageDictionary:messageDictionary]; - - if ([event.name isEqualToString:PTPusherConnectionEstablishedEvent]) { - self.socketID = (event.data)[@"socket_id"]; - self.state = PTPusherConnectionConnected; - - [self.delegate pusherConnectionDidConnect:self]; - } - - [self.delegate pusherConnection:self didReceiveEvent:event]; + NSString *message = [[PTJSON JSONParser] JSONStringFromObject:event]; + + [self webSocket:nil didReceiveMessage:message]; } - (void)simulateUnexpectedDisconnection { - self.state = PTPusherConnectionDisconnected; - self.socketID = nil; - // we always call this last, to prevent a race condition if the delegate calls 'connect' - [self.delegate pusherConnection:self didDisconnectWithCode:kPTPusherSimulatedDisconnectionErrorCode reason:nil wasClean:NO]; + [self webSocket:nil didCloseWithCode:kPTPusherSimulatedDisconnectionErrorCode reason:nil wasClean:NO]; } #pragma mark - Client event handling