forked from pusher/libPusher
/
PTPusher.m
357 lines (288 loc) · 11.1 KB
/
PTPusher.m
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// PTPusher.m
// PusherEvents
//
// Created by Luke Redpath on 22/03/2010.
// Copyright 2010 LJR Software Limited. All rights reserved.
//
#import "PTPusher.h"
#import "PTEventListener.h"
#import "PTPusherEvent.h"
#import "PTPusherChannel.h"
#import "PTPusherEventDispatcher.h"
#import "PTTargetActionEventListener.h"
#import "PTBlockEventListener.h"
#import "PTPusherErrors.h"
#import "PTPusherChannelAuthorizationOperation.h"
#define kPUSHER_HOST @"ws.pusherapp.com"
NSURL *PTPusherConnectionURL(NSString *host, NSString *key, NSString *clientID, BOOL secure);
NSString *const PTPusherEventReceivedNotification = @"PTPusherEventReceivedNotification";
NSString *const PTPusherEventUserInfoKey = @"PTPusherEventUserInfoKey";
NSString *const PTPusherErrorDomain = @"PTPusherErrorDomain";
NSString *const PTPusherErrorUnderlyingEventKey = @"PTPusherErrorUnderlyingEventKey";
NSURL *PTPusherConnectionURL(NSString *host, NSString *key, NSString *clientID, BOOL encrypted)
{
NSString *scheme = ((encrypted == YES) ? @"wss" : @"ws");
NSString *URLString = [NSString stringWithFormat:@"%@://%@/app/%@?client=%@&protocol=%d&version=%f",
scheme, host, key, clientID, kPTPusherClientProtocolVersion, kPTPusherClientLibraryVersion];
return [NSURL URLWithString:URLString];
}
#define kPTPusherDefaultReconnectDelay 5.0
@interface PTPusher ()
@property (nonatomic, strong, readwrite) PTPusherConnection *connection;
- (void)subscribeToChannel:(PTPusherChannel *)channel;
- (void)reconnectAfterDelay;
@end
@interface PTPusherChannel ()
/* These methods should only be called internally */
- (void)subscribeWithAuthorization:(NSDictionary *)authData;
- (void)unsubscribe;
- (void)markAsUnsubscribed;
@end
#pragma mark -
@implementation PTPusher {
NSOperationQueue *authorizationQueue;
}
@synthesize connection = _connection;
@synthesize delegate;
@synthesize reconnectAutomatically;
@synthesize reconnectDelay;
@synthesize authorizationURL;
- (id)initWithConnection:(PTPusherConnection *)connection connectAutomatically:(BOOL)connectAutomatically
{
if (self = [super init]) {
dispatcher = [[PTPusherEventDispatcher alloc] init];
channels = [[NSMutableDictionary alloc] init];
authorizationQueue = [[NSOperationQueue alloc] init];
authorizationQueue.maxConcurrentOperationCount = 5;
authorizationQueue.name = @"com.pusher.libPusher.authorizationQueue";
self.connection = connection;
self.connection.delegate = self;
self.reconnectAutomatically = NO;
self.reconnectDelay = kPTPusherDefaultReconnectDelay;
if (connectAutomatically) {
[self connect];
}
}
return self;
}
+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate
{
return [self pusherWithKey:key delegate:delegate encrypted:YES];
}
+ (id)pusherWithKey:(NSString *)key delegate:(id<PTPusherDelegate>)delegate encrypted:(BOOL)isEncrypted
{
PTPusher *pusher = [self pusherWithKey:key connectAutomatically:NO encrypted:isEncrypted];
pusher.delegate = delegate;
[pusher connect];
return pusher;
}
+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically
{
return [self pusherWithKey:key connectAutomatically:connectAutomatically encrypted:YES];
}
+ (id)pusherWithKey:(NSString *)key connectAutomatically:(BOOL)connectAutomatically encrypted:(BOOL)isEncrypted
{
NSURL *serviceURL = PTPusherConnectionURL(kPUSHER_HOST, key, @"libPusher", isEncrypted);
PTPusherConnection *connection = [[PTPusherConnection alloc] initWithURL:serviceURL];
PTPusher *pusher = [[self alloc] initWithConnection:connection connectAutomatically:connectAutomatically];
return pusher;
}
- (void)dealloc;
{
[_connection setDelegate:nil];
[_connection disconnect];
}
#pragma mark - Connection management
- (void)connect
{
[self.connection connect];
}
- (void)disconnect
{
[self.connection disconnect];
}
#pragma mark - Binding to events
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName target:(id)target action:(SEL)selector
{
return [dispatcher addEventListenerForEventNamed:eventName target:target action:selector];
}
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName handleWithBlock:(PTPusherEventBlockHandler)block
{
return [self bindToEventNamed:eventName handleWithBlock:block queue:dispatch_get_main_queue()];
}
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName handleWithBlock:(PTPusherEventBlockHandler)block queue:(dispatch_queue_t)queue
{
return [dispatcher addEventListenerForEventNamed:eventName block:block queue:queue];
}
- (void)removeBinding:(PTPusherEventBinding *)binding
{
[dispatcher removeBinding:binding];
}
- (void)removeAllBindings
{
[dispatcher removeAllBindings];
}
#pragma mark - Subscribing to channels
- (PTPusherChannel *)subscribeToChannelNamed:(NSString *)name
{
PTPusherChannel *channel = [channels objectForKey:name];
if (channel == nil) {
channel = [PTPusherChannel channelWithName:name pusher:self];
[channels setObject:channel forKey:name];
}
// private/presence channels require a socketID to authenticate
if (self.connection.isConnected && self.connection.socketID) {
[self subscribeToChannel:channel];
}
return channel;
}
- (PTPusherPrivateChannel *)subscribeToPrivateChannelNamed:(NSString *)name
{
return (PTPusherPrivateChannel *)[self subscribeToChannelNamed:[NSString stringWithFormat:@"private-%@", name]];
}
- (PTPusherPresenceChannel *)subscribeToPresenceChannelNamed:(NSString *)name
{
return (PTPusherPresenceChannel *)[self subscribeToChannelNamed:[NSString stringWithFormat:@"presence-%@", name]];
}
- (PTPusherPresenceChannel *)subscribeToPresenceChannelNamed:(NSString *)name delegate:(id<PTPusherPresenceChannelDelegate>)presenceDelegate
{
PTPusherPresenceChannel *channel = [self subscribeToPresenceChannelNamed:name];
channel.presenceDelegate = presenceDelegate;
return channel;
}
- (PTPusherChannel *)channelNamed:(NSString *)name
{
return [channels objectForKey:name];
}
- (void)__unsubscribeFromChannel:(PTPusherChannel *)channel
{
NSParameterAssert(channel != nil);
if (channel.isSubscribed == NO) return;
[self sendEventNamed:@"pusher:unsubscribe"
data:[NSDictionary dictionaryWithObject:channel.name forKey:@"channel"]];
[channel removeAllBindings];
[channel markAsUnsubscribed];
if ([self.delegate respondsToSelector:@selector(pusher:didUnsubscribeFromChannel:)]) {
[self.delegate pusher:self didUnsubscribeFromChannel:channel];
}
[channels removeObjectForKey:channel.name];
}
- (void)unsubscribeFromChannel:(PTPusherChannel *)channel
{
[self __unsubscribeFromChannel:channel];
}
- (void)subscribeToChannel:(PTPusherChannel *)channel
{
[channel authorizeWithCompletionHandler:^(BOOL isAuthorized, NSDictionary *authData, NSError *error) {
if (isAuthorized && self.connection.isConnected) {
[channel subscribeWithAuthorization:authData];
}
else {
if (error == nil) {
error = [NSError errorWithDomain:PTPusherErrorDomain code:PTPusherSubscriptionUnknownAuthorisationError userInfo:nil];
}
if ([self.delegate respondsToSelector:@selector(pusher:didFailToSubscribeToChannel:withError:)]) {
[self.delegate pusher:self didFailToSubscribeToChannel:channel withError:error];
}
}
}];
}
#pragma mark - Sending events
- (void)sendEventNamed:(NSString *)name data:(id)data
{
[self sendEventNamed:name data:data channel:nil];
}
- (void)sendEventNamed:(NSString *)name data:(id)data channel:(NSString *)channelName
{
NSParameterAssert(name);
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
[payload setObject:name forKey:PTPusherEventKey];
if (data) {
[payload setObject:data forKey:PTPusherDataKey];
}
if (channelName) {
[payload setObject:channelName forKey:PTPusherChannelKey];
}
[self.connection send:payload];
}
#pragma mark - PTPusherConnection delegate methods
- (void)pusherConnectionDidConnect:(PTPusherConnection *)connection
{
if ([self.delegate respondsToSelector:@selector(pusher:connectionDidConnect:)]) {
[self.delegate pusher:self connectionDidConnect:connection];
}
for (PTPusherChannel *channel in [channels allValues]) {
[self subscribeToChannel:channel];
}
}
- (void)pusherConnection:(PTPusherConnection *)connection didDisconnectWithCode:(NSInteger)errorCode reason:(NSString *)reason wasClean:(BOOL)wasClean
{
NSError *error = nil;
if (errorCode > 0) {
error = [NSError errorWithDomain:PTPusherErrorDomain code:errorCode userInfo:[NSDictionary dictionaryWithObject:reason forKey:@"reason"]];
}
[self handleDisconnection:connection error:error];
}
- (void)pusherConnection:(PTPusherConnection *)connection didFailWithError:(NSError *)error wasConnected:(BOOL)wasConnected
{
if ([self.delegate respondsToSelector:@selector(pusher:connection:failedWithError:)]) {
[self.delegate pusher:self connection:connection failedWithError:error];
}
if (wasConnected) {
[self handleDisconnection:connection error:error];
}
else if(self.shouldReconnectAutomatically) {
[self reconnectAfterDelay];
}
}
- (void)pusherConnection:(PTPusherConnection *)connection didReceiveEvent:(PTPusherEvent *)event
{
if ([event isKindOfClass:[PTPusherErrorEvent class]]) {
if ([self.delegate respondsToSelector:@selector(pusher:didReceiveErrorEvent:)]) {
[self.delegate pusher:self didReceiveErrorEvent:(PTPusherErrorEvent *)event];
}
}
if (event.channel) {
[[channels objectForKey:event.channel] dispatchEvent:event];
}
[dispatcher dispatchEvent:event];
[[NSNotificationCenter defaultCenter]
postNotificationName:PTPusherEventReceivedNotification
object:self
userInfo:[NSDictionary dictionaryWithObject:event forKey:PTPusherEventUserInfoKey]];
}
- (void)handleDisconnection:(PTPusherConnection *)connection error:(NSError *)error
{
[authorizationQueue cancelAllOperations];
for (PTPusherChannel *channel in [channels allValues]) {
[channel markAsUnsubscribed];
}
if ([self.delegate respondsToSelector:@selector(pusher:connectionDidDisconnect:)]) { // deprecated call
[self.delegate pusher:self connectionDidDisconnect:connection];
}
if ([self.delegate respondsToSelector:@selector(pusher:connection:didDisconnectWithError:)]) {
[self.delegate pusher:self connection:connection didDisconnectWithError:error];
}
if (self.shouldReconnectAutomatically) {
[self reconnectAfterDelay];
}
}
#pragma mark - Private
- (void)beginAuthorizationOperation:(PTPusherChannelAuthorizationOperation *)operation
{
[authorizationQueue addOperation:operation];
}
- (void)reconnectAfterDelay
{
if ([self.delegate respondsToSelector:@selector(pusher:connectionWillReconnect:afterDelay:)]) {
[self.delegate pusher:self connectionWillReconnect:_connection afterDelay:self.reconnectDelay];
}
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, self.reconnectDelay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (self.reconnectAutomatically) { // check this hasn't been changed since we queued this block
[_connection connect];
}
});
}
@end