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