Skip to content

Commit

Permalink
Removed dependency on JSONKit. Using NSJSONSerialization instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Luscher committed Feb 11, 2013
1 parent 623236a commit 348017a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3,158 deletions.
1 change: 0 additions & 1 deletion FayeObjc/FayeClient.h
Expand Up @@ -30,7 +30,6 @@
#import <UIKit/UIKit.h>
#endif

#import "JSONKit.h"
#import "SRWebSocket.h"

enum _fayeStates {
Expand Down
63 changes: 43 additions & 20 deletions FayeObjc/FayeClient.m
Expand Up @@ -46,7 +46,10 @@ - (void) handshake;
- (void) subscribe:(NSString *)channel;
- (void) unsubscribe:(NSString *)channel;
- (void) publish:(NSDictionary *)messageDict channel:(NSString *)channel withExt:(NSDictionary *)extension;
- (void) parseFayeMessage:(NSString *)message;
- (void) parseFayeMessages:(NSArray *)messages;

- (void) send:(id)object;
- (void) receive:(id)data;

@end

Expand Down Expand Up @@ -159,9 +162,9 @@ - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
}
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(NSString *)message;
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
{
[self parseFayeMessage:message];
[self receive:message];
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
Expand Down Expand Up @@ -192,6 +195,33 @@ - (void) passFayeClientError:(NSError *)error {
}
}

- (void) send:(id)object
{
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:0 error:&writeError];

if (writeError) {
NSLog(@"Could not serialize object as JSON string: %@", [writeError localizedDescription]);
} else {
[webSocket send:jsonData];
}
}

- (void) receive:(id)data
{
if ([data isKindOfClass:[NSString class]]) {
data = [data dataUsingEncoding:NSUTF8StringEncoding];
}

NSError *readError = nil;
NSArray *messages = [NSJSONSerialization JSONObjectWithData:data options:0 error:&readError];

if (readError) {
NSLog(@"Could not deserialize JSON as object: %@", [readError localizedDescription]);
} else {
[self parseFayeMessages:messages];
}
}

#pragma mark -
#pragma mark WebSocket connection
Expand Down Expand Up @@ -222,8 +252,7 @@ - (void) closeWebSocketConnection {
- (void) handshake {
NSArray *connTypes = [NSArray arrayWithObjects:@"long-polling", @"callback-polling", @"iframe", @"websocket", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:HANDSHAKE_CHANNEL, @"channel", @"1.0", @"version", @"1.0beta", @"minimumVersion", connTypes, @"supportedConnectionTypes", nil];
NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

/*
Expand All @@ -234,8 +263,7 @@ - (void) handshake {
*/
- (void) connect {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:CONNECT_CHANNEL, @"channel", self.fayeClientId, @"clientId", @"websocket", @"connectionType", nil];
NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

/*
Expand All @@ -246,8 +274,7 @@ - (void) connect {
*/
- (void) disconnect {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:DISCONNECT_CHANNEL, @"channel", self.fayeClientId, @"clientId", nil];
NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

/*
Expand All @@ -269,8 +296,7 @@ - (void) subscribe:(NSString *)channel {
dict = [NSDictionary dictionaryWithObjectsAndKeys:SUBSCRIBE_CHANNEL, @"channel", self.fayeClientId, @"clientId", channel, @"subscription", self.connectionExtension, @"ext", nil];
}

NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

/*
Expand All @@ -282,8 +308,7 @@ - (void) subscribe:(NSString *)channel {
*/
- (void) unsubscribe:(NSString *)channel {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:UNSUBSCRIBE_CHANNEL, @"channel", self.fayeClientId, @"clientId", channel, @"subscription", nil];
NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

/*
Expand Down Expand Up @@ -315,17 +340,15 @@ - (void) publish:(NSDictionary *)messageDict channel:(NSString *)channel withExt
dict = [NSDictionary dictionaryWithObjectsAndKeys:channel, @"channel", self.fayeClientId, @"clientId", messageDict, @"data", messageId, @"id", extension, @"ext",nil];
}

NSString *json = [dict JSONString];
[webSocket send:json];
[self send:dict];
}

#pragma mark -
#pragma mark Faye message handling
- (void) parseFayeMessage:(NSString *)message {
// interpret the message(s)
NSArray *messageArray = [message objectFromJSONString];
for(NSDictionary *messageDict in messageArray) {
FayeMessage *fm = [[FayeMessage alloc] initWithDict:messageDict];
- (void) parseFayeMessages:(NSArray *)messages {
// interpret the message(s)
for(NSDictionary *messageDict in messages) {
FayeMessage *fm = [[FayeMessage alloc] initWithDict:messageDict];

if ([fm.channel isEqualToString:HANDSHAKE_CHANNEL]) {
if ([fm.successful boolValue]) {
Expand Down
195 changes: 0 additions & 195 deletions FayeObjc/JSONKit/JSONKit.h

This file was deleted.

0 comments on commit 348017a

Please sign in to comment.