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

Acknowledgment timeout (#124) #185

Open
wants to merge 1 commit 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
5 changes: 4 additions & 1 deletion SocketIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ typedef enum {

- (void) sendMessage:(NSString *)data;
- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout;
- (void) sendJSON:(NSDictionary *)data;
- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout;
- (void) sendEvent:(NSString *)eventName withData:(id)data;
- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function;
- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout;
- (void) sendAcknowledgement:(NSString*)pId withArgs:(NSArray *)data;

- (void) setResourceName:(NSString *)name;

@end
@end
54 changes: 46 additions & 8 deletions SocketIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (void) sendDisconnect;
- (void) sendHearbeat;
- (void) send:(SocketIOPacket *)packet;

- (NSString *) addAcknowledge:(SocketIOCallback)function;
- (NSString *) addAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout;
- (void) removeAcknowledgeForKey:(NSString *)key;
- (NSMutableArray*) getMatchesFrom:(NSString*)data with:(NSString*)regex;

Expand Down Expand Up @@ -206,10 +206,15 @@ - (void) sendMessage:(NSString *)data
}

- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function
{
[self sendMessage:data withAcknowledge:function withTimeout:0];
}

- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout
{
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"message"];
packet.data = data;
packet.pId = [self addAcknowledge:function];
packet.pId = [self addAcknowledge:function withTimeout:timeout];
[self send:packet];
}

Expand All @@ -219,10 +224,15 @@ - (void) sendJSON:(NSDictionary *)data
}

- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function
{
[self sendJSON:data withAcknowledge:function withTimeout:0];
}

- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout
{
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"json"];
packet.data = [SocketIOJSONSerialization JSONStringFromObject:data error:nil];
packet.pId = [self addAcknowledge:function];
packet.pId = [self addAcknowledge:function withTimeout:timeout];
[self send:packet];
}

Expand All @@ -232,6 +242,11 @@ - (void) sendEvent:(NSString *)eventName withData:(id)data
}

- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function
{
[self sendEvent:eventName withData:data andAcknowledge:function withTimeout:0];
}

- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];

Expand All @@ -242,7 +257,7 @@ - (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(Socke

SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"event"];
packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil];
packet.pId = [self addAcknowledge:function];
packet.pId = [self addAcknowledge:function withTimeout:timeout];
if (function) {
packet.ack = @"data";
}
Expand Down Expand Up @@ -332,7 +347,10 @@ - (void) send:(SocketIOPacket *)packet
else {
DEBUGLOG(@"send() >>> %@", req);
[_transport send:req];

NSTimer *timoutTimer = _acks[packet.pId][@"timer"];
if (timoutTimer) {
[[NSRunLoop currentRunLoop] addTimer:timoutTimer forMode:NSRunLoopCommonModes];
}
if ([_delegate respondsToSelector:@selector(socketIO:didSendMessage:)]) {
[_delegate socketIO:self didSendMessage:packet];
}
Expand Down Expand Up @@ -383,12 +401,20 @@ - (void) onConnect:(SocketIOPacket *)packet
# pragma mark -
# pragma mark Acknowledge methods

- (NSString *) addAcknowledge:(SocketIOCallback)function
- (NSString *) addAcknowledge:(SocketIOCallback)function withTimeout:(NSTimeInterval)timeout;
{
if (function) {
++_ackCount;
NSString *ac = [NSString stringWithFormat:@"%ld", (long)_ackCount];
[_acks setObject:[function copy] forKey:ac];
NSMutableDictionary *ack = [@{@"callback":[function copy]} mutableCopy];
if (timeout) {
ack[@"timer"] = [NSTimer timerWithTimeInterval:timeout
target:self
selector:@selector(acknowledgeTimeout:)
userInfo:@{@"ac":ac}
repeats:NO];
}
[_acks setObject:ack forKey:ac];
return ac;
}
return nil;
Expand All @@ -399,6 +425,16 @@ - (void) removeAcknowledgeForKey:(NSString *)key
[_acks removeObjectForKey:key];
}

- (void)acknowledgeTimeout:(NSTimer *)timer
{
NSString *key = timer.userInfo[@"ac"];
SocketIOCallback callbackFunction = _acks[key][@"callback"];
if (callbackFunction) {
callbackFunction(nil);
[self removeAcknowledgeForKey:key];
}
}

# pragma mark -
# pragma mark Heartbeat methods

Expand Down Expand Up @@ -576,8 +612,10 @@ - (void) onData:(NSString *)data

// get selector for ackId
NSString *key = [NSString stringWithFormat:@"%d", ackId];
SocketIOCallback callbackFunction = [_acks objectForKey:key];
NSDictionary *ack = [_acks objectForKey:key];
SocketIOCallback callbackFunction = ack[@"callback"];
if (callbackFunction != nil) {
[ack[@"timer"] invalidate];
callbackFunction(argsData);
[self removeAcknowledgeForKey:key];
}
Expand Down