blob: ad74a1ae5a7d9dd020f47b5d4c5e1b5219be0066 (
plain)
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
|
//
// ChatMessagesDataSource.m
// NearMe
//
// Created by TW on 9/29/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "ChatMessagesDataSource.h"
#import "ChatViewController.h"
@implementation ChatMessagesDataSource
@synthesize ws;
@synthesize messages;
@synthesize chatTableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [messages count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ChatCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
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)initWithTableView:(UITableView *)tableView
{
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];
chatTableView = tableView;
}
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];
[chatTableView reloadData];
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
|