This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPubNubClient.m
71 lines (61 loc) · 2.07 KB
/
PubNubClient.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
//
// PubNubClient.m
// PubNubClient
//
// Created by Luke Alonso on 4/9/15.
// Copyright (c) 2015 Twitter. All rights reserved.
//
#import "PubNubClient.h"
#import "PubNubChannel.h"
static NSString* kPubNubPresenceSuffix = @"-pnpres";
@implementation PubNubClient
{
NSString* _clientUuid;
PubNubChannel* _channel;
PubNubChannel* _presenceChannel;
}
- (instancetype)initWithChannel:(NSString*)channel uuid:(NSString*)uuid authKey:(NSString*)authKey subscriberKey:(NSString*)subscriberKey publisherKey:(NSString*)publisherKey
{
self = [super init];
if (self) {
_clientUuid = uuid;
_channel = [[PubNubChannel alloc] initWithName:channel clientUuid:_clientUuid authKey:authKey subscriberKey:subscriberKey publisherKey:publisherKey heartbeat:YES];
_channel.delegate = self;
_presenceChannel = [[PubNubChannel alloc] initWithName:[channel stringByAppendingString:kPubNubPresenceSuffix] clientUuid:_clientUuid authKey:authKey subscriberKey:subscriberKey publisherKey:publisherKey heartbeat:NO];
_presenceChannel.delegate = self;
}
return self;
}
- (void)connectWithCompletion:(void (^)())completion
{
[_channel subscribeWithUserInfo:_userInfo completion:^{
[_presenceChannel subscribeWithUserInfo:_userInfo completion:^{
completion();
}];
}];
}
- (void)disconnectWithCompletion:(void (^)())completion
{
[_channel unsubscribeWithCompletion:^{
[_presenceChannel unsubscribeWithCompletion:^{
completion();
}];
}];
}
- (void)pubNubChannel:(PubNubChannel *)channel messages:(NSArray *)messages receivedDate:(NSDate*)receivedDate
{
if (self.delegate) {
if (channel == _presenceChannel) {
[self.delegate pubNubClient:self presenceEvents:messages];
} else {
[self.delegate pubNubClient:self messages:messages receivedDate:receivedDate];
}
}
}
- (void)pubNubChannel:(PubNubChannel *)channel state:(PubNubChannelState)state
{
if (channel == _channel) {
[self.delegate pubNubClient:self state:state];
}
}
@end