-
Notifications
You must be signed in to change notification settings - Fork 0
Plug
Guilherme Souza edited this page Oct 19, 2013
·
2 revisions
Implement a delegate to listen for events like connection opened, message received and connection closed:
// .h
#import "Plug.h"
@interface MyPlugDelegate : NSObject<PlugDelegate>
@end
// .m
#import "MyPlugDelegate .h"
@implementation MyPlugDelegate
- (void)plug:(Plug *)plug receivedMessage:(PlugMsgType)type data:(id)data {
NSLog(@"received %d %@", type, data);
}
- (void)plugHasConnected:(Plug *)plug {
NSLog(@"connected");
}
- (void)plug:(Plug *)plug hasClosedWithError:(BOOL)error {
NSLog(@"closed %hhd", error);
}
@end
Connect and set the delegate:
Plug* plug = [[Plug alloc] init];
plug.delegate = [[MyPlugDelegate alloc] init];
Send a message:
[self.plug sendMessage:msg_type data:msg_data];
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull
- All dictionary keys are instances of NSString
- Numbers are not NaN or infinity
- All messages are always delivered and received completly (no data loss) and in order, sequentially dispatched. Otherwise the connection is closed automatically
- hasConnected is called only when the connection is really ready for sending and receiving messages
- hasClosed is called even if the connection hasn't opened, like when the server is down (or any error in the connection process)