Skip to content

Instantly share code, notes, and snippets.

@MariuszWisniewski
Created May 24, 2014 21:59
Show Gist options
  • Save MariuszWisniewski/62bf8c669d4a31f7d3bb to your computer and use it in GitHub Desktop.
Save MariuszWisniewski/62bf8c669d4a31f7d3bb to your computer and use it in GitHub Desktop.
Using SyncServer
@interface SyncanoCommunication : NSObject
@property (strong, nonatomic) SyncanoSyncServer *syncServer;
- (void)openSyncServerConnection;
- (void)subscribeToCollectionChanges;
- (void)sendMessage:(NSString *)message;
@end
@implementation SyncanoCommunication
- (SyncanoSyncServer *)syncServer {
if (_syncServer == nil) {
_syncServer = [SyncanoSyncServer syncanoSyncServerForDomain:@"DOMAIN_NAME" apiKey:@"YOUR_API_KEY"];
}
return _syncServer;
}
/**
User this version to handle all communication by delegate protocol
*/
- (void)openSyncServerConnection {
[self.syncServer connect:nil];
}
/**
Use this version to handle all communication by blocks
*/
/*
- (void)openSyncServerConnection {
[self.syncServer connect:nil connectionOpen:^{
} connectionClosed:^(NSError *error) {
} messageReceived:^(id message) {
} historyReceived:^(id message, BOOL isLastHistoryItem) {
} syncServerError:^(NSString *error) {
} notificationDeleted:^(NSArray *deletedIDs) {
} notificationChanged:^(NSArray *dataChanges) {
} notificationAdded:^(SyncanoData *addedData) {
}];
}
*/
- (void)subscribeToCollectionChanges {
/**
User it to subscribe to changes in collection. They will be received
in delegate or block callbacks after you opened SyncServer connection.
Possible contexts:
- client (default) - subscribe all connections of current API client,
- session - store subscription in current session,
- connection - subscribe current connection only (requires Sync Server connection).
*/
SyncanoParameters_Subscriptions_SubscribeCollection *params = [[SyncanoParameters_Subscriptions_SubscribeCollection alloc] initWithProjectId:@"707" collectionId:@"3511" context:@"client"];
[self.syncServer subscriptionSubscribeCollection:params callback: ^(SyncanoResponse *response) {
NSLog(@"Response: %@", response);
}];
}
- (void)sendMessage:(NSString *)message {
SyncanoParameters_Notifications_Send *params = [[SyncanoParameters_Notifications_Send alloc] init];
/**
After opening connection you can obtain it from SyncServer using
[self.syncServer valueForKey:@"uuid"];
Set it, if you want to send message only to specified UUID connection
(so after opening connection, you would have to send it e.g. as DataObject,
and it could be downloade by added app instance
If it is not set, message will be send to everyone connected to
sync server on your domain.
*/
params.uuid = @"UUID";
/**
Everything send in additional is the message you want to send.
As it is dictionary, it can be as complicated structure as you want,
it will be transformed to JSON, and when received on other end, it will
be dictionary again.
e.g.
params.additional = @{@"position":NSStringFromCGPoint(CGPointMake(10, 20)),
@"hitStrength":@"20",
@"name":@"Nathan"};
Or just use it to send simple message
*/
params.additional = @{ @"message":message };
[self.syncServer notificationSend:params callback: ^(SyncanoResponse *response) {
NSLog(@"Response: %@", response);
}];
/**
Or use universal method to send requests:
[self.syncServer sendRequest:params callback: ^(SyncanoResponse *response) {
NSLog(@"Response: %@", response);
}];
You can always use SyncServer to send any other request! E.g.
SyncanoParameters_DataObjects_New *params = [[SyncanoParameters_DataObjects_New alloc] initWithProjectId:@"707" collectionId:@"3511" state:@"pending"];
params.text = text;
[self.syncServer sendRequest:params callback: ^(SyncanoResponse *response) {
NSLog(@"Response: %@", response);
}];
*/
}
#pragma mark - protocol SyncanoSyncServerDelegate <NSObject>
#pragma required
- (void)syncServerConnectionOpened:(SyncanoSyncServer *)syncServer {
NSLog(@"Connection opened!");
}
- (void)syncServer:(SyncanoSyncServer *)syncServer connectionClosedWithError:(NSError *)error {
NSLog(@"Connection closed with error: %@", error);
}
#pragma optional
- (void)syncServer:(SyncanoSyncServer *)syncServer messageReceived:(id)message {
NSLog(@"Received message: %@", message);
}
//- (void)syncServer:(SyncanoSyncServer *)syncServer historyReceived:(id)message isLastHistoryItem:(BOOL)isLastHistoryItem;
//- (void)syncServer:(SyncanoSyncServer *)syncServer errorReceived:(NSString *)error;
- (void)syncServer:(SyncanoSyncServer *)syncServer notificationDeleted:(NSArray *)deletedIds {
NSLog(@"Removed Data Objects with ids: %@", deletedIds);
}
- (void)syncServer:(SyncanoSyncServer *)syncServer notificationChanged:(NSArray *)dataChanges {
NSLog(@"Changed data object, its changes: %@", dataChanges);
}
- (void)syncServer:(SyncanoSyncServer *)syncServer notificationAdded:(SyncanoData *)addedData {
NSLog(@"Added new data object: %@", addedData);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment