Skip to content
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
5 changes: 3 additions & 2 deletions Pod/Classes/Phoenix.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@interface Phoenix : NSObject

@property (nonatomic, strong, readonly) NSURL *url;
@property (nonatomic, assign) id<PhoenixDelegate> delegate;
@property (nonatomic, weak) id<PhoenixDelegate> delegate;

- (instancetype)initWithURL:(NSURL*)url;

Expand All @@ -37,6 +37,7 @@
@interface PhoenixChannel : NSObject

typedef void(^HandleEventBlock)(id message);
typedef void(^HandleReplyBlock)(id reply);

@property (nonatomic, strong, readonly) NSString *topic;
@property (nonatomic, strong, readonly) NSDictionary *payload;
Expand All @@ -47,7 +48,7 @@ typedef void(^HandleEventBlock)(id message);
- (BOOL)join;
- (BOOL)leave;

- (void)sendEvent:(NSString*)event payload:(id)payload;
- (void)sendEvent:(NSString*)event payload:(id)payload onReply:(HandleReplyBlock)handleReplyBlock;

- (void)on:(NSString*)event handleEventBlock:(HandleEventBlock)handleEventBlock;

Expand Down
40 changes: 28 additions & 12 deletions Pod/Classes/Phoenix.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface Phoenix()<SRWebSocketDelegate>

@property (nonatomic, strong) SRWebSocket *webSocket;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, assign) NSInteger ref;

@property (nonatomic, strong) NSMutableDictionary *channels;

Expand All @@ -25,8 +26,9 @@ @interface Phoenix()<SRWebSocketDelegate>
@interface PhoenixChannel()

@property (nonatomic, strong) NSMutableDictionary *listeners;
@property (nonatomic, strong) NSMutableDictionary *replyHandlers;

- (void)handleEvent:(NSString*)event withMessage:(id)message;
- (void)handleEvent:(NSString*)event withMessage:(id)message ref:(NSString *)ref;

@end

Expand Down Expand Up @@ -70,7 +72,7 @@ - (BOOL)joinChannel:(PhoenixChannel*)channel {
_channels[channel.topic] = channel;

// Joins channel
[self send:channel.topic event:@"join" payload:channel.payload];
[self send:channel.topic event:@"phx_join" payload:channel.payload];

return YES;
}
Expand All @@ -84,7 +86,7 @@ - (BOOL)leaveChannel:(PhoenixChannel*)channel {
[_channels removeObjectForKey:channel.topic];

// Leaves channel
[self send:channel.topic event:@"leave" payload:nil];
[self send:channel.topic event:@"phx_leave" payload:nil];

return YES;
}
Expand All @@ -97,12 +99,13 @@ - (void)sendHeartbeat {

#pragma mark - Helpers

- (void)send:(NSString*)topic event:(NSString*)event payload:(id)payload {

- (NSString *)send:(NSString*)topic event:(NSString*)event payload:(id)payload {
NSString *ref = @(_ref++).stringValue;
NSDictionary *message = @{
@"topic": topic,
@"event": event,
@"payload": payload ?: [NSNull null]
@"payload": payload ?: [NSNull null],
@"ref": ref
};

NSData *data = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil];
Expand All @@ -115,7 +118,7 @@ - (void)send:(NSString*)topic event:(NSString*)event payload:(id)payload {
[_delegate phoenix:self sentEvent:event onTopic:topic withPayload:payload];
}
}];

return ref;
}

#pragma mark - SRWebSocketDelegate
Expand All @@ -124,7 +127,8 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
NSDictionary *resp = [NSJSONSerialization JSONObjectWithData:[message dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

PhoenixChannel *channel = _channels[resp[@"topic"]];
[channel handleEvent:resp[@"event"] withMessage:resp[@"payload"]];
NSString *ref = [resp[@"ref"] isKindOfClass:[NSString class]] ? resp[@"ref"] : nil;
[channel handleEvent:resp[@"event"] withMessage:resp[@"payload"] ref:ref];
}

- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
Expand Down Expand Up @@ -172,7 +176,8 @@ - (instancetype)initWithTopic:(NSString*)topic payload:(NSDictionary*)payload wi
_payload = payload;
_phoenix = phoenix;

_listeners = @{}.mutableCopy;
_listeners = [NSMutableDictionary new];
_replyHandlers = [NSMutableDictionary new];
}
return self;
}
Expand All @@ -187,8 +192,11 @@ - (BOOL)leave {
return [_phoenix leaveChannel:self];
}

- (void)sendEvent:(NSString*)event payload:(id)payload {
[_phoenix send:_topic event:event payload:payload];
- (void)sendEvent:(NSString*)event payload:(id)payload onReply:(HandleReplyBlock)handleReplyBlock {
NSString *msgRef = [_phoenix send:_topic event:event payload:payload];
if (handleReplyBlock) {
_replyHandlers[msgRef] = handleReplyBlock;
}
}

- (void)on:(NSString*)event handleEventBlock:(HandleEventBlock)handleEventBlock {
Expand All @@ -197,11 +205,19 @@ - (void)on:(NSString*)event handleEventBlock:(HandleEventBlock)handleEventBlock

#pragma mark - Private

- (void)handleEvent:(NSString*)event withMessage:(id)message {
- (void)handleEvent:(NSString*)event withMessage:(id)message ref:(NSString *)ref {
HandleEventBlock block = _listeners[event];
if (block) {
block(message);
}

if (ref) {
HandleReplyBlock replyBlock = _replyHandlers[ref];
if (replyBlock) {
replyBlock(message);
[_replyHandlers removeObjectForKey:ref];
}
}
}

@end