Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add ability to add callbacks to specific events.

  • Loading branch information...
commit 002370b8253d0889582f2bcc8dc86299a3004b37 1 parent e202b55
@pashields pashields authored
View
4 AZSocketIO/AZSocketIO.h
@@ -38,4 +38,8 @@ typedef void (^ACKCallback)(NSArray *args);
- (BOOL)send:(id)data error:(NSError * __autoreleasing *)error;
- (BOOL)emit:(NSString *)name args:(id)args error:(NSError *__autoreleasing *)error ack:(ACKCallback)callback;
- (BOOL)emit:(NSString *)name args:(id)args error:(NSError * __autoreleasing *)error;
+
+- (void)addCallbackForEventName:(NSString *)name callback:(EventRecievedBlock)block;
+- (BOOL)removeCallbackForEvent:(NSString *)name callback:(EventRecievedBlock)block;
+- (NSInteger)removeCallbacksForEvent:(NSString *)name;
@end
View
46 AZSocketIO/AZSocketIO.m
@@ -24,6 +24,8 @@ @interface AZSocketIO ()
@property(nonatomic, strong)NSMutableDictionary *ackCallbacks;
@property(nonatomic, assign)NSInteger ackCount;
@property(nonatomic, strong)NSTimer *heartbeatTimer;
+
+@property(nonatomic, strong)NSMutableDictionary *specificEventBlocks;
@end
@implementation AZSocketIO
@@ -47,6 +49,8 @@ @implementation AZSocketIO
@synthesize ackCount;
@synthesize heartbeatTimer;
+@synthesize specificEventBlocks;
+
- (id)initWithHost:(NSString *)_host andPort:(NSString *)_port
{
self = [super init];
@@ -56,6 +60,7 @@ - (id)initWithHost:(NSString *)_host andPort:(NSString *)_port
self.httpClient = [[AFHTTPClient alloc] initWithBaseURL:nil];
self.ackCallbacks = [NSMutableDictionary dictionary];
self.ackCount = 0;
+ self.specificEventBlocks = [NSMutableDictionary new];
}
return self;
}
@@ -176,6 +181,36 @@ - (BOOL)sendPacket:(AZSocketIOPacket *)packet error:(NSError * __autoreleasing *
return YES;
}
+#pragma mark event callback registration
+- (void)addCallbackForEventName:(NSString *)name callback:(EventRecievedBlock)block
+{
+ NSMutableArray *callbacks = [self.specificEventBlocks objectForKey:name];
+ if (callbacks == nil) {
+ callbacks = [NSMutableArray array];
+ [self.specificEventBlocks setValue:callbacks forKey:name];
+ }
+ [callbacks addObject:block];
+}
+- (BOOL)removeCallbackForEvent:(NSString *)name callback:(EventRecievedBlock)block
+{
+ NSMutableArray *callbacks = [self.specificEventBlocks objectForKey:name];
+ if (callbacks != nil) {
+ NSInteger count = [callbacks count];
+ [callbacks removeObject:block];
+ if ([callbacks count] == 0) {
+ [self.specificEventBlocks removeObjectForKey:name];
+ return YES;
+ }
+ return count != [callbacks count];
+ }
+ return NO;
+}
+- (NSInteger)removeCallbacksForEvent:(NSString *)name
+{
+ NSMutableArray *callbacks = [self.specificEventBlocks objectForKey:name];
+ [self.specificEventBlocks removeObjectForKey:name];
+ return [callbacks count];
+}
#pragma mark heartbeat
- (void)clearHeartbeatTimeout
{
@@ -201,7 +236,7 @@ - (void)didReceiveMessage:(NSString *)message
{
[self startHeartbeatTimeout];
AZSocketIOPacket *packet = [[AZSocketIOPacket alloc] initWithString:message];
- id outData; AZSocketIOACKMessage *ackMessage; ACKCallback callback;
+ id outData; AZSocketIOACKMessage *ackMessage; ACKCallback callback; NSArray *callbackList;
switch (packet.type) {
case DISCONNECT:
[self disconnect];
@@ -224,7 +259,14 @@ - (void)didReceiveMessage:(NSString *)message
break;
case EVENT:
outData = AFJSONDecode([packet.data dataUsingEncoding:NSUTF8StringEncoding], nil);
- self.eventRecievedBlock([outData objectForKey:@"name"], [outData objectForKey:@"args"]);
+ callbackList = [self.specificEventBlocks objectForKey:[outData objectForKey:@"name"]];
+ if (callbackList != nil) {
+ for (EventRecievedBlock block in callbackList) {
+ block([outData objectForKey:@"name"], [outData objectForKey:@"args"]);
+ }
+ } else {
+ self.eventRecievedBlock([outData objectForKey:@"name"], [outData objectForKey:@"args"]);
+ }
break;
case ACK:
ackMessage = [[AZSocketIOACKMessage alloc] initWithPacket:packet];
View
8 AZSocketIOTests/AZSocketIOTests.m
@@ -65,10 +65,10 @@
__block NSArray *args = [NSArray arrayWithObject:@"bar"];
__block NSString *recievedName;
__block NSArray *recievedArgs;
- socket.eventRecievedBlock = ^(NSString *_name, id _args) {
- recievedName = _name;
- recievedArgs = _args;
- };
+ [socket addCallbackForEventName:@"foo" callback:^(NSString *eventName, id data) {
+ recievedName = eventName;
+ recievedArgs = data;
+ }];
[socket emit:name args:args error:nil];
[[expectFutureValue(recievedName) shouldEventuallyBeforeTimingOutAfter(5.0)] equal:name];
[[expectFutureValue(recievedArgs) shouldEventuallyBeforeTimingOutAfter(5.0)] equal:args];
Please sign in to comment.
Something went wrong with that request. Please try again.