Commits

a761007 committed 19995c7

add flush messages api, and update the example.

  • Participants
  • Parent commits a45c5d0

Comments (0)

Files changed (6)

File ezcomet_api.xcodeproj/project.pbxproj

 				6510C14315C5AE6F00C731F8 /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 /* End XCConfigurationList section */
 	};

File ezcomet_api/DemoView.xib

 <?xml version="1.0" encoding="UTF-8"?>
 <archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
 	<data>
-		<int key="IBDocument.SystemTarget">1296</int>
-		<string key="IBDocument.SystemVersion">11D50d</string>
-		<string key="IBDocument.InterfaceBuilderVersion">2182</string>
-		<string key="IBDocument.AppKitVersion">1138.32</string>
-		<string key="IBDocument.HIToolboxVersion">568.00</string>
+		<int key="IBDocument.SystemTarget">1536</int>
+		<string key="IBDocument.SystemVersion">12C54</string>
+		<string key="IBDocument.InterfaceBuilderVersion">2840</string>
+		<string key="IBDocument.AppKitVersion">1187.34</string>
+		<string key="IBDocument.HIToolboxVersion">625.00</string>
 		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
 			<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
-			<string key="NS.object.0">1181</string>
+			<string key="NS.object.0">1926</string>
 		</object>
 		<array key="IBDocument.IntegratedClassDependencies">
+			<string>IBProxyObject</string>
+			<string>IBUIButton</string>
 			<string>IBUILabel</string>
-			<string>IBUIButton</string>
+			<string>IBUITextField</string>
 			<string>IBUIView</string>
-			<string>IBUITextField</string>
-			<string>IBProxyObject</string>
 		</array>
 		<array key="IBDocument.PluginDependencies">
 			<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 						<object class="NSColor" key="IBUITextColor" id="582897771">
 							<int key="NSColorSpace">1</int>
 							<bytes key="NSRGB">MCAwIDAAA</bytes>
+							<string key="IBUIColorCocoaTouchKeyPath">darkTextColor</string>
 						</object>
 						<nil key="IBUIHighlightedColor"/>
 						<int key="IBUIBaselineAdjustment">0</int>
 						<string key="NSFrame">{{20, 170}, {280, 31}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
 						<reference key="NSWindow"/>
-						<reference key="NSNextKeyView" ref="396280593"/>
+						<reference key="NSNextKeyView" ref="421877825"/>
 						<string key="NSReuseIdentifierKey">_NS:9</string>
 						<bool key="IBUIOpaque">NO</bool>
 						<bool key="IBUIClipsSubviews">YES</bool>
 					<object class="IBUIButton" id="479718656">
 						<reference key="NSNextResponder" ref="191373211"/>
 						<int key="NSvFlags">292</int>
-						<string key="NSFrame">{{228, 209}, {72, 37}}</string>
+						<string key="NSFrame">{{228, 208}, {72, 44}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
 						<reference key="NSWindow"/>
 						<reference key="NSNextKeyView"/>
 						<int key="IBUIContentHorizontalAlignment">0</int>
 						<int key="IBUIContentVerticalAlignment">0</int>
 						<int key="IBUIButtonType">1</int>
-						<string key="IBUINormalTitle">Sumit</string>
+						<string key="IBUINormalTitle">Push</string>
 						<object class="NSColor" key="IBUIHighlightedTitleColor" id="401307291">
 							<int key="NSColorSpace">3</int>
 							<bytes key="NSWhite">MQA</bytes>
 					<object class="IBUIButton" id="396280593">
 						<reference key="NSNextResponder" ref="191373211"/>
 						<int key="NSvFlags">292</int>
-						<string key="NSFrame">{{148, 209}, {72, 37}}</string>
+						<string key="NSFrame">{{148, 208}, {72, 44}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
 						<reference key="NSWindow"/>
 						<reference key="NSNextKeyView" ref="479718656"/>
 						<int key="IBUIContentHorizontalAlignment">0</int>
 						<int key="IBUIContentVerticalAlignment">0</int>
 						<int key="IBUIButtonType">1</int>
-						<string key="IBUINormalTitle">get</string>
+						<string key="IBUINormalTitle">Get</string>
+						<reference key="IBUIHighlightedTitleColor" ref="401307291"/>
+						<object class="NSColor" key="IBUINormalTitleColor">
+							<int key="NSColorSpace">1</int>
+							<bytes key="NSRGB">MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA</bytes>
+						</object>
+						<reference key="IBUINormalTitleShadowColor" ref="591875060"/>
+						<reference key="IBUIFontDescription" ref="894289921"/>
+						<reference key="IBUIFont" ref="420809152"/>
+					</object>
+					<object class="IBUIButton" id="421877825">
+						<reference key="NSNextResponder" ref="191373211"/>
+						<int key="NSvFlags">292</int>
+						<string key="NSFrame">{{67, 208}, {73, 44}}</string>
+						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
+						<reference key="NSNextKeyView" ref="396280593"/>
+						<string key="NSReuseIdentifierKey">_NS:9</string>
+						<bool key="IBUIOpaque">NO</bool>
+						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+						<int key="IBUIContentHorizontalAlignment">0</int>
+						<int key="IBUIContentVerticalAlignment">0</int>
+						<int key="IBUIButtonType">1</int>
+						<string key="IBUINormalTitle">Flush</string>
 						<reference key="IBUIHighlightedTitleColor" ref="401307291"/>
 						<object class="NSColor" key="IBUINormalTitleColor">
 							<int key="NSColorSpace">1</int>
 				</object>
 				<object class="IBConnectionRecord">
 					<object class="IBCocoaTouchOutletConnection" key="connection">
-						<string key="label">submit</string>
-						<reference key="source" ref="372490531"/>
-						<reference key="destination" ref="479718656"/>
-					</object>
-					<int key="connectionID">16</int>
-				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBCocoaTouchOutletConnection" key="connection">
 						<string key="label">username</string>
 						<reference key="source" ref="372490531"/>
 						<reference key="destination" ref="571852446"/>
 					<int key="connectionID">18</int>
 				</object>
 				<object class="IBConnectionRecord">
+					<object class="IBCocoaTouchOutletConnection" key="connection">
+						<string key="label">flushMessagesButton</string>
+						<reference key="source" ref="372490531"/>
+						<reference key="destination" ref="421877825"/>
+					</object>
+					<int key="connectionID">24</int>
+				</object>
+				<object class="IBConnectionRecord">
+					<object class="IBCocoaTouchOutletConnection" key="connection">
+						<string key="label">getMessagesButton</string>
+						<reference key="source" ref="372490531"/>
+						<reference key="destination" ref="396280593"/>
+					</object>
+					<int key="connectionID">25</int>
+				</object>
+				<object class="IBConnectionRecord">
+					<object class="IBCocoaTouchOutletConnection" key="connection">
+						<string key="label">pushMessageButton</string>
+						<reference key="source" ref="372490531"/>
+						<reference key="destination" ref="479718656"/>
+					</object>
+					<int key="connectionID">26</int>
+				</object>
+				<object class="IBConnectionRecord">
 					<object class="IBCocoaTouchEventConnection" key="connection">
 						<string key="label">pushMessage:</string>
 						<reference key="source" ref="479718656"/>
 					</object>
 					<int key="connectionID">22</int>
 				</object>
+				<object class="IBConnectionRecord">
+					<object class="IBCocoaTouchEventConnection" key="connection">
+						<string key="label">flushMessages:</string>
+						<reference key="source" ref="421877825"/>
+						<reference key="destination" ref="372490531"/>
+						<int key="IBEventType">7</int>
+					</object>
+					<int key="connectionID">27</int>
+				</object>
 			</array>
 			<object class="IBMutableOrderedSet" key="objectRecords">
 				<array key="orderedObjects">
 							<reference ref="675686845"/>
 							<reference ref="325127020"/>
 							<reference ref="716905373"/>
-							<reference ref="479718656"/>
 							<reference ref="687787162"/>
 							<reference ref="571852446"/>
 							<reference ref="396280593"/>
+							<reference ref="421877825"/>
+							<reference ref="479718656"/>
 						</array>
 						<reference key="parent" ref="0"/>
 					</object>
 						<reference key="object" ref="396280593"/>
 						<reference key="parent" ref="191373211"/>
 					</object>
+					<object class="IBObjectRecord">
+						<int key="objectID">23</int>
+						<reference key="object" ref="421877825"/>
+						<reference key="parent" ref="191373211"/>
+					</object>
 				</array>
 			</object>
 			<dictionary class="NSMutableDictionary" key="flattenedProperties">
 				<string key="11.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 				<string key="12.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 				<string key="20.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
+				<string key="23.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 				<string key="6.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 				<string key="7.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 				<string key="8.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 			<nil key="activeLocalization"/>
 			<dictionary class="NSMutableDictionary" key="localizations"/>
 			<nil key="sourceID"/>
-			<int key="maxID">22</int>
+			<int key="maxID">27</int>
 		</object>
 		<object class="IBClassDescriber" key="IBDocument.Classes">
 			<array class="NSMutableArray" key="referencedPartialClassDescriptions">
 					<string key="className">DemoViewController</string>
 					<string key="superclassName">UIViewController</string>
 					<dictionary class="NSMutableDictionary" key="actions">
+						<string key="flushMessages:">id</string>
 						<string key="getMessages:">id</string>
 						<string key="pushMessage:">id</string>
 					</dictionary>
 					<dictionary class="NSMutableDictionary" key="actionInfosByName">
+						<object class="IBActionInfo" key="flushMessages:">
+							<string key="name">flushMessages:</string>
+							<string key="candidateClassName">id</string>
+						</object>
 						<object class="IBActionInfo" key="getMessages:">
 							<string key="name">getMessages:</string>
 							<string key="candidateClassName">id</string>
 					</dictionary>
 					<dictionary class="NSMutableDictionary" key="outlets">
 						<string key="channel">UITextField</string>
+						<string key="flushMessagesButton">UIButton</string>
+						<string key="getMessagesButton">UIButton</string>
 						<string key="message">UITextField</string>
-						<string key="submit">UIButton</string>
+						<string key="pushMessageButton">UIButton</string>
 						<string key="username">UITextField</string>
 					</dictionary>
 					<dictionary class="NSMutableDictionary" key="toOneOutletInfosByName">
 							<string key="name">channel</string>
 							<string key="candidateClassName">UITextField</string>
 						</object>
+						<object class="IBToOneOutletInfo" key="flushMessagesButton">
+							<string key="name">flushMessagesButton</string>
+							<string key="candidateClassName">UIButton</string>
+						</object>
+						<object class="IBToOneOutletInfo" key="getMessagesButton">
+							<string key="name">getMessagesButton</string>
+							<string key="candidateClassName">UIButton</string>
+						</object>
 						<object class="IBToOneOutletInfo" key="message">
 							<string key="name">message</string>
 							<string key="candidateClassName">UITextField</string>
 						</object>
-						<object class="IBToOneOutletInfo" key="submit">
-							<string key="name">submit</string>
+						<object class="IBToOneOutletInfo" key="pushMessageButton">
+							<string key="name">pushMessageButton</string>
 							<string key="candidateClassName">UIButton</string>
 						</object>
 						<object class="IBToOneOutletInfo" key="username">
 		<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
 		<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
 			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
-			<real value="1296" key="NS.object.0"/>
+			<real value="1536" key="NS.object.0"/>
 		</object>
 		<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
 		<int key="IBDocument.defaultPropertyAccessControl">3</int>
-		<string key="IBCocoaTouchPluginVersion">1181</string>
+		<string key="IBCocoaTouchPluginVersion">1926</string>
 	</data>
 </archive>

File ezcomet_api/example/DemoViewController.h

 @property (nonatomic, retain) IBOutlet UITextField *username;
 @property (nonatomic, retain) IBOutlet UITextField *channel;
 @property (nonatomic, retain) IBOutlet UITextField *message;
-@property (nonatomic, retain) IBOutlet UIButton *submit;
+@property (nonatomic, retain) IBOutlet UIButton *pushMessageButton;
+@property (nonatomic, retain) IBOutlet UIButton *getMessagesButton;
+@property (nonatomic, retain) IBOutlet UIButton *flushMessagesButton;
 
 - (IBAction)pushMessage:(id)sender;
 - (IBAction)getMessages:(id)sender;
+- (IBAction)flushMessages:(id)sender;
 
 @end

File ezcomet_api/example/DemoViewController.m

 @synthesize username;
 @synthesize channel;
 @synthesize message;
-@synthesize submit;
+@synthesize pushMessageButton;
+@synthesize getMessagesButton;
+@synthesize flushMessagesButton;
+
+- (void) ezcomet:(EZComet *)ezcomet flushMessagesDidSucessWithTick:(NSString *)tick
+{
+    NSLog(@"%s", __PRETTY_FUNCTION__);
+    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"msg"
+                                                    message:tick
+                                                   delegate:nil
+                                          cancelButtonTitle:@"OK"
+                                          otherButtonTitles:nil, nil];
+    [alert show];    
+}
+
+- (void) ezcomet:(EZComet *)ezcomet flushMessagesDidFailWithErrorMessage:(NSString *)err_msg
+{
+    NSLog(@"%s:%@", __PRETTY_FUNCTION__, err_msg);
+    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"msg"
+                                                    message:err_msg
+                                                   delegate:nil
+                                          cancelButtonTitle:@"OK"
+                                          otherButtonTitles:nil, nil];
+    [alert show];
+}
+
+- (IBAction)flushMessages:(id)sender
+{
+    NSLog(@"%s", __PRETTY_FUNCTION__);
+    EZComet *comet = [EZComet shareInstance];
+    comet.delegate = self;
+    [comet flushMessagesWithUserName:username.text
+                             Channel:channel.text];
+    
+}
 
 - (void) ezcomet:(EZComet *)ezcomet pushMessageDidSucessWithTick:(NSString *)tick
 {

File ezcomet_api/ezcomet/EZComet.h

 - (void) ezcomet:(EZComet *)ezcomet getMessagesDidSucessWithMessages:(NSArray *)messages;
 - (void) ezcomet:(EZComet *)ezcomet getMessagesDidFailWithErrorMessage:(NSString *)err_msg;
 
+- (void) ezcomet:(EZComet *)ezcomet flushMessagesDidSucessWithTick:(NSString *)tick;
+- (void) ezcomet:(EZComet *)ezcomet flushMessagesDidFailWithErrorMessage:(NSString *)err_msg;
 @end
 
 @interface EZComet : NSObject <ASIHTTPRequestDelegate>
                          Channel:(NSString *)channel 
                             Tick:(NSString *)tick;
 
+- (void) flushMessagesWithUserName:(NSString *)user_name Channel:(NSString *)channel;
+
 @end

File ezcomet_api/ezcomet/EZComet.m

 #define WRITE_ENDPOINT @"/write"
 #define READ_ENDPOINT @"/read"
 
-#define API_KEY @"your api key" 
+#define API_KEY @"your api key"
 
 @implementation EZComet
 
     [request startAsynchronous];
 }
 
+#pragma mark -
+#pragma flush request messages delegate
+- (void) flushMessagesRequestFinished:(ASIHTTPRequest *)request
+{
+    NSLog(@"%s", __PRETTY_FUNCTION__);
+    NSDictionary *data = [[request responseString] JSONValue];
+    
+    if (![[data objectForKey:@"code"] isEqualToString:@"ok"]) {
+        [self flushMessagesRequestFailed:request];
+    } else {
+        NSLog(@"%@", [request responseString]);
+        if (self.delegate != nil && 
+            [self.delegate respondsToSelector:@selector(ezcomet:flushMessagesDidSucessWithTick:)]) {
+            [self.delegate ezcomet:self flushMessagesDidSucessWithTick:[data objectForKey:@"tick"]];
+        }
+    }
+}
+
+- (void) flushMessagesRequestFailed:(ASIHTTPRequest *)request
+{
+    NSLog(@"%s", __PRETTY_FUNCTION__);
+    NSDictionary *data = [[request responseString] JSONValue];
+    NSString *err_msg = [data objectForKey:@"msg"];
+    NSLog(@"%@", [request responseString]);
+    if (self.delegate != nil && 
+        [self.delegate respondsToSelector:@selector(ezcomet:flushMessagesDidFailWithErrorMessage:)]) {
+        [self.delegate ezcomet:self flushMessagesDidFailWithErrorMessage:err_msg];
+    }
+}
+#pragma mark -
+#pragma mark flush messages
+- (void) flushMessagesWithUserName:(NSString *)user_name Channel:(NSString *)channel
+{
+    NSLog(@"%s", __PRETTY_FUNCTION__);
+    NSString *url = [NSString stringWithFormat:@"%@%@", 
+                     API_HOSTNAME, WRITE_ENDPOINT];
+    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
+    [request setValidatesSecureCertificate:NO];
+    [request setDelegate:self];
+    [request setDidFinishSelector:@selector(flushMessagesRequestFinished:)];
+    [request setDidFailSelector:@selector(flushMessagesRequestFailed:)];
+    [request addPostValue:API_KEY forKey:@"api_key"];
+    [request addPostValue:[NSString stringWithFormat:@"%@-%@", user_name, channel] forKey:@"qname"];
+    [request addPostValue:@"1" forKey:@"flush"];
+    [request startAsynchronous];
+}
+
 @end