Skip to content

Commit

Permalink
Merge pull request #2 from JoaoCaixinha/master
Browse files Browse the repository at this point in the history
update native sdk to run timers on main thread
  • Loading branch information
realtime-framework committed Nov 4, 2015
2 parents 26f29b1 + fcfcb2e commit ac2d451
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
62 changes: 47 additions & 15 deletions RCTRealtimeCloudStorage/OrtcClient/OrtcClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,10 @@ + (void) setDEVICE_TOKEN:(NSString *) deviceToken {

- (void) startHeartbeatLoop{
if(heartbeatTimer == nil && heartbeatActive)
heartbeatTimer = [NSTimer scheduledTimerWithTimeInterval:heartbeatTime target:self selector:@selector(heartbeatLoop) userInfo:nil repeats:YES];
}
dispatch_sync(dispatch_get_main_queue(), ^{
heartbeatTimer = [NSTimer scheduledTimerWithTimeInterval:heartbeatTime target:self selector:@selector(heartbeatLoop) userInfo:nil repeats:YES];
});
}
- (void) stopHeartbeatLoop{
if(heartbeatTimer != nil)
[heartbeatTimer invalidate];
Expand Down Expand Up @@ -1279,28 +1281,53 @@ - (void)opReceive:(NSString*) message {
[messagesBuffer setObject:msgSentDict forKey:messageId];
}
aMessage = [self escapeRecvChars:aMessage];
channelSubscription.onMessage(self, aChannel, aMessage);
}

aMessage = [self checkForEmoji:aMessage];
channelSubscription.onMessage(self, aChannel, aMessage);
}
}
}
}
}


- (NSString*)checkForEmoji:(NSString*)str{
for (int i = 0; i < str.length; i++) {
unichar ascii = [str characterAtIndex:i];
if(ascii == '\\'){
i = i + 1;
int next = [str characterAtIndex:i];

if(next == 'u'){
NSString *emoji = [str substringWithRange:NSMakeRange(i - 1, 12)];
NSData *pos = [emoji dataUsingEncoding:NSUTF8StringEncoding];
emoji = [[NSString alloc] initWithData:pos encoding:NSNonLossyASCIIStringEncoding];

str = [str stringByReplacingCharactersInRange:NSMakeRange(i - 1, 12) withString:emoji];
}
}
}
return str;
}

- (NSString*)escapeRecvChars:(NSString*) str{
str = [self simulateJsonParse:str];
str = [self simulateJsonParse:str];
return str;
}
- (NSString*)simulateJsonParse:(NSString*) str{

- (NSString*)simulateJsonParse:(NSString*)str{
NSMutableString *ms = [NSMutableString string];
for(int i =0; i < [str length]; i++){
unichar ascii = [str characterAtIndex:i];

if(ascii > 128){ //unicode
[ms appendFormat:@"%@", [NSString stringWithCharacters:&ascii length:1]];
} else { //ascii
}else { //ascii
if(ascii == '\\'){
i = i + 1;
int next = [str characterAtIndex:i];

if(next == '\\'){
[ms appendString:@"\\"];
} else if(next == 'n'){
Expand All @@ -1315,13 +1342,15 @@ - (NSString*)simulateJsonParse:(NSString*) str{
[ms appendString:@"\r"];
} else if(next == 't'){
[ms appendString:@"\t"];
}
} else if(next == 'u'){
[ms appendString:@"\\u"];
}
} else {
[ms appendFormat:@"%c", ascii];
}
}
}
return ms;
return ms ;
}

- (NSString*)generateId:(int) size
Expand Down Expand Up @@ -1429,8 +1458,9 @@ - (void)processConnect:(id) sender

}
else {
[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(processConnect:) userInfo:nil repeats:NO];

dispatch_sync(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(processConnect:) userInfo:nil repeats:NO];
});
}

}];
Expand Down Expand Up @@ -1589,8 +1619,9 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
[self doConnect:self];
}
else {

[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(doConnect:) userInfo:nil repeats:NO];
dispatch_sync(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(doConnect:) userInfo:nil repeats:NO];
});
}
}
}
Expand All @@ -1610,7 +1641,9 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code r
[self doConnect:self];
}
else {
[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(doConnect:) userInfo:nil repeats:NO];
dispatch_sync(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:connectionTimeout target:self selector:@selector(doConnect:) userInfo:nil repeats:NO];
});
}
}
}
Expand Down Expand Up @@ -1684,8 +1717,7 @@ - (id)initWithConfig:(id<OrtcClientDelegate>) aDelegate
- (void) receivedNotification:(NSNotification *) notification
{
// [notification name] should be @"ApnsNotification" for received Apns Notififications
if ([[notification name] isEqualToString:@"ApnsNotification"]) {

if ([[notification name] isEqualToString:@"ApnsNotification"]) {
NSDictionary *notificaionInfo = [[NSDictionary alloc] initWithDictionary:[notification userInfo]];
if ([[notificaionInfo objectForKey:@"A"] isEqualToString:applicationKey]) {

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-realtimestorage-ios",
"version": "1.0.3",
"version": "1.0.4",
"description": "The Realtime Framework Cloud Storage client for React-Native",
"main": "RCTRealtimeCloudStorageIOS.js",
"scripts": {
Expand Down

0 comments on commit ac2d451

Please sign in to comment.