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

I've added a simple category on socket.IO-objc to use block callbacks instead of delegate callbacks. #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions SocketTesterARC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
4ADCCBF315790FDF0022990C /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4ADCCBF215790FDF0022990C /* SystemConfiguration.framework */; };
4ADCCD4D157915F00022990C /* SocketIO.m in Sources */ = {isa = PBXBuildFile; fileRef = 4ADCCBC715790DEC0022990C /* SocketIO.m */; };
C9E391A215E2A1B00004693A /* SocketIOJSONSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E391A115E2A1B00004693A /* SocketIOJSONSerialization.m */; };
FA4408951849FDCC006FA1EA /* SocketIO+Blocks.m in Sources */ = {isa = PBXBuildFile; fileRef = FA4408941849FDCC006FA1EA /* SocketIO+Blocks.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -98,6 +99,8 @@
4ADCCBF215790FDF0022990C /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
C9E391A015E2A1B00004693A /* SocketIOJSONSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocketIOJSONSerialization.h; sourceTree = SOURCE_ROOT; };
C9E391A115E2A1B00004693A /* SocketIOJSONSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocketIOJSONSerialization.m; sourceTree = SOURCE_ROOT; };
FA4408931849FDCC006FA1EA /* SocketIO+Blocks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SocketIO+Blocks.h"; sourceTree = "<group>"; };
FA4408941849FDCC006FA1EA /* SocketIO+Blocks.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SocketIO+Blocks.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -197,6 +200,7 @@
4ADCCBA715790D760022990C /* SocketTesterARC */ = {
isa = PBXGroup;
children = (
FA4408921849FDC1006FA1EA /* SocketIOBlocks */,
4A4453791589EE7D00B44ABB /* SocketRocket */,
4A4453781589EE6300B44ABB /* SBJson */,
4ADCCBB015790D760022990C /* AppDelegate.h */,
Expand Down Expand Up @@ -231,6 +235,15 @@
name = "Supporting Files";
sourceTree = "<group>";
};
FA4408921849FDC1006FA1EA /* SocketIOBlocks */ = {
isa = PBXGroup;
children = (
FA4408931849FDCC006FA1EA /* SocketIO+Blocks.h */,
FA4408941849FDCC006FA1EA /* SocketIO+Blocks.m */,
);
path = SocketIOBlocks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -302,6 +315,7 @@
4A4453821589EE9100B44ABB /* NSData+SRB64Additions.m in Sources */,
4A4453831589EE9100B44ABB /* SRWebSocket.m in Sources */,
C9E391A215E2A1B00004693A /* SocketIOJSONSerialization.m in Sources */,
FA4408951849FDCC006FA1EA /* SocketIO+Blocks.m in Sources */,
4AD96DF41680853E00D9E42D /* SocketIOPacket.m in Sources */,
4AD96DF816808B7900D9E42D /* SocketIOTransportWebsocket.m in Sources */,
4AD96DFC16822DE300D9E42D /* SocketIOTransportXHR.m in Sources */,
Expand Down
32 changes: 32 additions & 0 deletions SocketTesterARC/SocketIOBlocks/SocketIO+Blocks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// SocketIO+Blocks.h
//
// Created by Anton Domashnev on 06.11.13.
// Copyright (c) 2013 Anton Domashnev. All rights reserved.
//

#import "SocketIO.h"

@interface SocketIOBlocksDelegate : NSObject<SocketIODelegate>

@end

@interface SocketIO (Blocks)

- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withCallback:(void (^)(SocketIO *, NSError *))callback;
- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withCallback:(void (^)(SocketIO *, NSError *))callback;
- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint withCallback:(void (^)(SocketIO *, NSError *))callback;

- (void)addErrorHandler:(void (^)(SocketIO *, NSError *))handler forKey:(NSString *)key;
- (void)removeErrorHandlerForKey:(NSString *)key;

- (void)addMessageHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key;
- (void)removeMessageHandlerForKey:(NSString *)key;

- (void)addJSONHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key;
- (void)removeJSONHandlerForKey:(NSString *)key;

- (void)addEventHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key;
- (void)removeEventHandlerForKey:(NSString *)key;

@end
210 changes: 210 additions & 0 deletions SocketTesterARC/SocketIOBlocks/SocketIO+Blocks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//
// SocketIO+Blocks.m
//
// Created by Anton Domashnev on 06.11.13.
// Copyright (c) 2013 Anton Domashnev. All rights reserved.
//

#import "SocketIO+Blocks.h"

#import <objc/runtime.h>

#define SocketIOBlockSafeRun(block, ...) block ? block(__VA_ARGS__) : nil

@interface SocketIOBlocksDelegate()<SocketIODelegate>

@property (nonatomic, copy) void (^onStart)(SocketIO *, NSError *);

@property (nonatomic, strong) NSMutableDictionary *onErrorBlocksDictionary;
@property (nonatomic, strong) NSMutableDictionary *onMessageBlocksDictionary;
@property (nonatomic, strong) NSMutableDictionary *onJSONBlocksDictionary;
@property (nonatomic, strong) NSMutableDictionary *onEventBlocksDictionary;

@end

@implementation SocketIOBlocksDelegate

- (instancetype)init{

if(self = [super init]){

self.onErrorBlocksDictionary = [NSMutableDictionary new];
self.onMessageBlocksDictionary = [NSMutableDictionary new];
self.onJSONBlocksDictionary = [NSMutableDictionary new];
self.onEventBlocksDictionary = [NSMutableDictionary new];
}

return self;
}

- (void)socketIODidConnect:(SocketIO *)socket{

SocketIOBlockSafeRun(self.onStart, socket, nil);
}

- (void)socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error{

SocketIOBlockSafeRun(self.onStart, socket, error);
}

- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet{

[self.onMessageBlocksDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
void(^block)(SocketIO *, SocketIOPacket *) = obj;
SocketIOBlockSafeRun(block, socket, packet);
}];
}

- (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet{

[self.onJSONBlocksDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
void(^block)(SocketIO *, SocketIOPacket *) = obj;
SocketIOBlockSafeRun(block, socket, packet);
}];
}

- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet{

[self.onEventBlocksDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
void(^block)(SocketIO *, SocketIOPacket *) = obj;
SocketIOBlockSafeRun(block, socket, packet);
}];
}

- (void) socketIO:(SocketIO *)socket onError:(NSError *)error{

[self.onErrorBlocksDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
void(^block)(SocketIO *, NSError *) = obj;
SocketIOBlockSafeRun(block, socket ,error);
}];
}

@end

@interface SocketIO(Private)

@property (nonatomic, strong) SocketIOBlocksDelegate *blocksDelegate;

@end

@implementation SocketIO (Blocks)

- (void)setBlocksDelegate:(SocketIOBlocksDelegate *)blocksDelegate{

objc_setAssociatedObject(self, @"SocketIOBlocksDelegate", blocksDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (SocketIOBlocksDelegate *)blocksDelegate{

return objc_getAssociatedObject(self, @"SocketIOBlocksDelegate");
}

#pragma mark - Helpers

- (void)setBlocksDelegateIfNeeded{

if(!self.delegate || ![self.delegate isKindOfClass:[SocketIOBlocksDelegate class]]){
self.blocksDelegate = [SocketIOBlocksDelegate new];
self.delegate = self.blocksDelegate;
}
}

- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withCallback:(void (^)(SocketIO *, NSError *))callback{

[self connectToHost:host onPort:port withParams:nil withCallback:callback];
}

- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withCallback:(void (^)(SocketIO *, NSError *))callback{

[self connectToHost:host onPort:port withParams:params withNamespace:nil withCallback:callback];
}

- (void)connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint withCallback:(void (^)(SocketIO *, NSError *))callback{

[self setBlocksDelegateIfNeeded];
((SocketIOBlocksDelegate *)self.delegate).onStart = callback;

[self connectToHost:host onPort:port withParams:params withNamespace:endpoint];
}

#pragma mark - @selector(socketIO:onError:)

- (void)addErrorHandler:(void (^)(SocketIO *, NSError *))handler forKey:(NSString *)key{

NSParameterAssert(key);
NSParameterAssert(handler);

[self setBlocksDelegateIfNeeded];
((SocketIOBlocksDelegate *)self.delegate).onErrorBlocksDictionary[key] = handler;
}

- (void)removeErrorHandlerForKey:(NSString *)key{

NSParameterAssert(key);

if(self.delegate){
[((SocketIOBlocksDelegate *)self.delegate).onErrorBlocksDictionary removeObjectForKey:key];
}
}

#pragma mark - @selector(socketIO:didReceiveMessage:)

- (void)addMessageHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key{

NSParameterAssert(key);
NSParameterAssert(handler);

[self setBlocksDelegateIfNeeded];
((SocketIOBlocksDelegate *)self.delegate).onMessageBlocksDictionary[key] = handler;
}

- (void)removeMessageHandlerForKey:(NSString *)key{

NSParameterAssert(key);

if(self.delegate){
[((SocketIOBlocksDelegate *)self.delegate).onMessageBlocksDictionary removeObjectForKey:key];
}
}

#pragma mark - @selector(socketIO:didReceiveJSON:)

- (void)addJSONHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key{

NSParameterAssert(key);
NSParameterAssert(handler);

[self setBlocksDelegateIfNeeded];
((SocketIOBlocksDelegate *)self.delegate).onJSONBlocksDictionary[key] = handler;
}

- (void)removeJSONHandlerForKey:(NSString *)key{

NSParameterAssert(key);

if(self.delegate){
[((SocketIOBlocksDelegate *)self.delegate).onJSONBlocksDictionary removeObjectForKey:key];
}
}

#pragma mark - @selector(socketIO:didReceiveEvent:)

- (void)addEventHandler:(void (^)(SocketIO *, SocketIOPacket *))handler forKey:(NSString *)key{

NSParameterAssert(key);
NSParameterAssert(handler);

[self setBlocksDelegateIfNeeded];
((SocketIOBlocksDelegate *)self.delegate).onEventBlocksDictionary[key] = handler;
}

- (void)removeEventHandlerForKey:(NSString *)key{

NSParameterAssert(key);

if(self.delegate){
[((SocketIOBlocksDelegate *)self.delegate).onEventBlocksDictionary removeObjectForKey:key];
}
}

@end