TCViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // TCViewController.m
  3. // TestChat
  4. //
  5. // Created by Mike Lewis on 1/28/12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "TCViewController.h"
  9. #import "SRWebSocket.h"
  10. #import "TCChatCell.h"
  11. @interface TCMessage : NSObject
  12. - (id)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
  13. @property (nonatomic, retain, readonly) NSString *message;
  14. @property (nonatomic, readonly) BOOL fromMe;
  15. @end
  16. @interface TCViewController () <SRWebSocketDelegate, UITextViewDelegate>
  17. @end
  18. @implementation TCViewController {
  19. SRWebSocket *_webSocket;
  20. NSMutableArray *_messages;
  21. }
  22. @synthesize inputView = _inputView;
  23. #pragma mark - View lifecycle
  24. - (void)viewDidLoad;
  25. {
  26. [super viewDidLoad];
  27. _messages = [[NSMutableArray alloc] init];
  28. [self.tableView reloadData];
  29. }
  30. - (void)_reconnect;
  31. {
  32. _webSocket.delegate = nil;
  33. [_webSocket close];
  34. _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://localhost:9000/chat"]]];
  35. _webSocket.delegate = self;
  36. self.title = @"Opening Connection...";
  37. [_webSocket open];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. [self _reconnect];
  43. }
  44. - (void)reconnect:(id)sender;
  45. {
  46. [self _reconnect];
  47. }
  48. - (void)sendPing:(id)sender;
  49. {
  50. [_webSocket sendPing:nil];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated;
  53. {
  54. [super viewDidAppear:animated];
  55. [_inputView becomeFirstResponder];
  56. }
  57. - (void)viewDidDisappear:(BOOL)animated
  58. {
  59. [super viewDidDisappear:animated];
  60. _webSocket.delegate = nil;
  61. [_webSocket close];
  62. _webSocket = nil;
  63. }
  64. #pragma mark - UITableViewController
  65. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  66. {
  67. return _messages.count;
  68. }
  69. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  70. {
  71. TCChatCell *chatCell = (id)cell;
  72. TCMessage *message = [_messages objectAtIndex:indexPath.row];
  73. chatCell.textView.text = message.message;
  74. chatCell.nameLabel.text = message.fromMe ? @"Me" : @"Other";
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  77. {
  78. TCMessage *message = [_messages objectAtIndex:indexPath.row];
  79. return [self.tableView dequeueReusableCellWithIdentifier:message.fromMe ? @"SentCell" : @"ReceivedCell"];
  80. }
  81. #pragma mark - SRWebSocketDelegate
  82. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  83. {
  84. NSLog(@"Websocket Connected");
  85. self.title = @"Connected!";
  86. }
  87. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  88. {
  89. NSLog(@":( Websocket Failed With Error %@", error);
  90. self.title = @"Connection Failed! (see logs)";
  91. _webSocket = nil;
  92. }
  93. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  94. {
  95. NSLog(@"Received \"%@\"", message);
  96. [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:NO]];
  97. [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  98. [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
  99. }
  100. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  101. {
  102. NSLog(@"WebSocket closed");
  103. self.title = @"Connection Closed! (see logs)";
  104. _webSocket = nil;
  105. }
  106. - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
  107. {
  108. NSLog(@"Websocket received pong");
  109. }
  110. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
  111. {
  112. if ([text rangeOfString:@"\n"].location != NSNotFound) {
  113. NSString *message = [[textView.text stringByReplacingCharactersInRange:range withString:text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  114. [_webSocket send:message];
  115. [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:YES]];
  116. [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  117. [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
  118. textView.text = @"";
  119. return NO;
  120. }
  121. return YES;
  122. }
  123. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
  124. {
  125. return YES;
  126. }
  127. @end
  128. @implementation TCMessage
  129. @synthesize message = _message;
  130. @synthesize fromMe = _fromMe;
  131. - (id)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
  132. {
  133. self = [super init];
  134. if (self) {
  135. _fromMe = fromMe;
  136. _message = message;
  137. }
  138. return self;
  139. }
  140. @end