TCViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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)viewDidAppear:(BOOL)animated;
  49. {
  50. [super viewDidAppear:animated];
  51. [_inputView becomeFirstResponder];
  52. }
  53. - (void)viewDidDisappear:(BOOL)animated
  54. {
  55. [super viewDidDisappear:animated];
  56. _webSocket.delegate = nil;
  57. [_webSocket close];
  58. _webSocket = nil;
  59. }
  60. #pragma mark - UITableViewController
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  62. {
  63. return _messages.count;
  64. }
  65. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  66. {
  67. TCChatCell *chatCell = (id)cell;
  68. TCMessage *message = [_messages objectAtIndex:indexPath.row];
  69. chatCell.textView.text = message.message;
  70. chatCell.nameLabel.text = message.fromMe ? @"Me" : @"Other";
  71. }
  72. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  73. {
  74. TCMessage *message = [_messages objectAtIndex:indexPath.row];
  75. return [self.tableView dequeueReusableCellWithIdentifier:message.fromMe ? @"SentCell" : @"ReceivedCell"];
  76. }
  77. #pragma mark - SRWebSocketDelegate
  78. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  79. {
  80. NSLog(@"Websocket Connected");
  81. self.title = @"Connected!";
  82. }
  83. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  84. {
  85. NSLog(@":( Websocket Failed With Error %@", error);
  86. self.title = @"Connection Failed! (see logs)";
  87. _webSocket = nil;
  88. }
  89. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  90. {
  91. NSLog(@"Received \"%@\"", message);
  92. [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:NO]];
  93. [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  94. [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
  95. }
  96. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  97. {
  98. NSLog(@"WebSocket closed");
  99. self.title = @"Connection Closed! (see logs)";
  100. _webSocket = nil;
  101. }
  102. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
  103. {
  104. if ([text rangeOfString:@"\n"].location != NSNotFound) {
  105. NSString *message = [[textView.text stringByReplacingCharactersInRange:range withString:text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  106. [_webSocket send:message];
  107. [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:YES]];
  108. [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  109. [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
  110. textView.text = @"";
  111. return NO;
  112. }
  113. return YES;
  114. }
  115. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
  116. {
  117. return YES;
  118. }
  119. @end
  120. @implementation TCMessage
  121. @synthesize message = _message;
  122. @synthesize fromMe = _fromMe;
  123. - (id)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
  124. {
  125. self = [super init];
  126. if (self) {
  127. _fromMe = fromMe;
  128. _message = message;
  129. }
  130. return self;
  131. }
  132. @end