diff options
Diffstat (limited to 'Classes')
| -rw-r--r-- | Classes/ChatMessagesDataSource.h | 13 | ||||
| -rw-r--r-- | Classes/ChatMessagesDataSource.m | 107 | ||||
| -rw-r--r-- | Classes/ChatViewController.h | 2 | ||||
| -rw-r--r-- | Classes/ChatViewController.m | 9 | 
4 files changed, 125 insertions, 6 deletions
| diff --git a/Classes/ChatMessagesDataSource.h b/Classes/ChatMessagesDataSource.h index b259b9b..51c0ce2 100644 --- a/Classes/ChatMessagesDataSource.h +++ b/Classes/ChatMessagesDataSource.h @@ -7,10 +7,19 @@  //  #import <Foundation/Foundation.h> +#import "WebSocket.h" -@interface ChatMessagesDataSource : NSObject <UITableViewDataSource> { - +@interface ChatMessagesDataSource : NSObject <UITableViewDataSource, WebSocketDelegate> { +	NSMutableArray *messages; +	@private +		WebSocket *ws;  } +@property (nonatomic, retain) NSMutableArray *messages; +@property (nonatomic, readonly) WebSocket *ws; + +- (void)startMyWebSocket; +- (void)sendMessage:(NSString *)message; +  @end diff --git a/Classes/ChatMessagesDataSource.m b/Classes/ChatMessagesDataSource.m index f3a60cb..80f449f 100644 --- a/Classes/ChatMessagesDataSource.m +++ b/Classes/ChatMessagesDataSource.m @@ -11,8 +11,11 @@  @implementation ChatMessagesDataSource +@synthesize ws; +@synthesize messages; +  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { -	return 5; +	return [messages count];  }  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { @@ -24,10 +27,110 @@  	}  	// Set up the cell... -	NSString *cellValue = @"test"; +	NSString *cellValue = [messages objectAtIndex:[indexPath row]];  	cell.textLabel.text = cellValue;  	return cell;  } +- (void)sendMessage:(NSString *)message { +	if (WebSocketReadyStateOpen) { +		[self.ws sendText:message]; +	} +} + +#pragma mark WebSocket + +- (void) startMyWebSocket +{ +    [self.ws open]; +     +    //continue processing other stuff +    //... +} + +#pragma mark Lifecycle + +- (id)init +{ +    self = [super init]; +    if (self)  +    { +        //make sure to use the right url, it must point to your specific web socket endpoint or the handshake will fail +        //create a connect config and set all our info here +        WebSocketConnectConfig* config = [WebSocketConnectConfig configWithURLString:@"ws://poddb.com:9394/" origin:nil protocols:nil tlsSettings:nil headers:nil verifySecurityKey:YES extensions:nil ]; +        config.closeTimeout = 15.0; +        config.keepAlive = 15.0; //sends a ws ping every 15s to keep socket alive + +        //open using the connect config, it will be populated with server info, such as selected protocol/etc +        ws = [[WebSocket webSocketWithConfig:config delegate:self] retain]; +		 +		messages = [[NSMutableArray alloc] init]; +    } +    return self; +     +} + +- (void)dealloc  +{ +	[messages release]; +    [ws release]; +    [super dealloc]; +} + +#pragma mark WebSocket Delegate methods + +/** + * Called when the web socket connects and is ready for reading and writing. + **/ +- (void) didOpen +{ +    NSLog(@"Socket is open for business."); +} + +/** + * Called when the web socket closes. aError will be nil if it closes cleanly. + **/ +- (void) didClose:(NSUInteger) aStatusCode message:(NSString*) aMessage error:(NSError*) aError +{ +    NSLog(@"Oops. It closed."); +} + +/** + * Called when the web socket receives an error. Such an error can result in the + socket being closed. + **/ +- (void) didReceiveError:(NSError*) aError +{ +    NSLog(@"Oops. An error occurred."); +} + +/** + * Called when the web socket receives a message. + **/ +- (void) didReceiveTextMessage:(NSString*) aMessage +{ +    //Hooray! I got a message to print. +//    NSLog(@"Did receive message: %@", aMessage); +	 +	[messages addObject:aMessage]; +	NSLog(@"TABLE DATA: %@", [messages description]); +} + +/** + * Called when the web socket receives a message. + **/ +- (void) didReceiveBinaryMessage:(NSData*) aMessage +{ +    //Hooray! I got a binary message. +} + +/** + * Called when pong is sent... For keep-alive optimization. + **/ +- (void) didSendPong:(NSData*) aMessage +{ +    NSLog(@"Yay! Pong was sent!"); +} +  @end diff --git a/Classes/ChatViewController.h b/Classes/ChatViewController.h index c6c517c..1170bd9 100644 --- a/Classes/ChatViewController.h +++ b/Classes/ChatViewController.h @@ -15,9 +15,11 @@  	UITableView *chatTableView;  	UIView *containerView;      HPGrowingTextView *textView; +	ChatMessagesDataSource *dataSource;  }  @property (nonatomic, retain) UITableView *chatTableView; +@property (nonatomic, retain) ChatMessagesDataSource *dataSource;  -(void)resignTextView; diff --git a/Classes/ChatViewController.m b/Classes/ChatViewController.m index b0bd903..971363c 100644 --- a/Classes/ChatViewController.m +++ b/Classes/ChatViewController.m @@ -12,6 +12,7 @@  @implementation ChatViewController  @synthesize chatTableView = _chatTableView; +@synthesize dataSource = _dataSource;  -(id)init  { @@ -39,7 +40,8 @@      self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1];  	chatTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; -	ChatMessagesDataSource *dataSource = [[ChatMessagesDataSource alloc] init]; +	dataSource = [[ChatMessagesDataSource alloc] init]; +	[dataSource startMyWebSocket];  	[chatTableView setDataSource:dataSource];  	[self.view addSubview:chatTableView]; @@ -86,7 +88,7 @@  	UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];  	doneBtn.frame = CGRectMake(containerView.frame.size.width - 69, 8, 63, 27);      doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin; -	[doneBtn setTitle:@"Done" forState:UIControlStateNormal]; +	[doneBtn setTitle:@"Post" forState:UIControlStateNormal];      [doneBtn setTitleShadowColor:[UIColor colorWithWhite:0 alpha:0.4] forState:UIControlStateNormal];      doneBtn.titleLabel.shadowOffset = CGSizeMake (0.0, -1.0); @@ -102,6 +104,9 @@  -(void)resignTextView  { +	[dataSource sendMessage:textView.text]; +	textView.text = @""; +	[chatTableView reloadData];  	[textView resignFirstResponder];  } | 
