forked from lwansbrough/react-native-multipeer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCTMultipeerConnectivity.m
217 lines (189 loc) · 9 KB
/
RCTMultipeerConnectivity.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
#import "RCTMultipeerConnectivity.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
//#import "ObjectStore.h"
@implementation RCTMultipeerConnectivity
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(advertise:(NSString *)channel data:(NSDictionary *)data) {
self.advertiser =
[[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peerID discoveryInfo:data serviceType:channel];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];
}
RCT_EXPORT_METHOD(browse:(NSString *)channel)
{
self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID serviceType:channel];
self.browser.delegate = self;
[self.browser startBrowsingForPeers];
}
RCT_EXPORT_METHOD(invite:(NSString *)peerUUID callback:(RCTResponseSenderBlock)callback) {
MCPeerID *peerID = [self.peers valueForKey:peerUUID];
[self.browser invitePeer:peerID toSession:self.session withContext:nil timeout:30];
callback(@[[NSNull null]]);
}
RCT_EXPORT_METHOD(rsvp:(NSString *)inviteID accept:(BOOL)accept callback:(RCTResponseSenderBlock)callback) {
void (^invitationHandler)(BOOL, MCSession *) = [self.invitationHandlers valueForKey:inviteID];
invitationHandler(accept, self.session);
[self.invitationHandlers removeObjectForKey:inviteID];
callback(@[[NSNull null]]);
}
RCT_EXPORT_METHOD(broadcast:(NSDictionary *)data callback:(RCTResponseSenderBlock)callback) {
[self sendData:[self.connectedPeers allKeys] data:data callback:callback];
}
RCT_EXPORT_METHOD(send:(NSArray *)recipients data:(NSDictionary *)data callback:(RCTResponseSenderBlock)callback) {
[self sendData:recipients data:data callback:callback];
}
RCT_EXPORT_METHOD(disconnect:(RCTResponseSenderBlock)callback) {
[self.session disconnect];
callback(@[[NSNull null]]);
}
// TODO: Waiting for module interop and/or streams over JS bridge
//RCT_EXPORT_METHOD(createStreamForPeer:(NSString *)peerUUID name:(NSString *)name callback:(RCTResponseSenderBlock)callback) {
// NSError *error = nil;
// NSString *outputStreamUUID = [[ObjectStore shared] putObject:[self.session startStreamWithName:name toPeer:[self.peers valueForKey:peerUUID] error:&error]];
// if (error != nil) {
// callback(@[[error description]]);
// }
// else {
// callback(@[[NSNull null], outputStreamUUID]);
// }
//}
- (instancetype)init {
self = [super init];
self.peers = [NSMutableDictionary dictionary];
self.connectedPeers = [NSMutableDictionary dictionary];
self.invitationHandlers = [NSMutableDictionary dictionary];
self.peerID = [[MCPeerID alloc] initWithDisplayName:[[NSUUID UUID] UUIDString]];
self.session = [[MCSession alloc] initWithPeer:self.peerID securityIdentity:nil encryptionPreference:MCEncryptionNone];
self.session.delegate = self;
return self;
}
- (void)sendData:(NSArray *)recipients data:(NSDictionary *)data callback:(RCTResponseSenderBlock)callback {
NSError *error = nil;
NSMutableArray *peers = [NSMutableArray array];
for (NSString *peerUUID in recipients) {
[peers addObject:[self.peers valueForKey:peerUUID]];
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
[self.session sendData:jsonData toPeers:peers withMode:MCSessionSendDataReliable error:&error];
if (error == nil) {
callback(@[[NSNull null]]);
}
else {
callback(@[[error description]]);
}
}
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
if ([peerID.displayName isEqualToString:self.peerID.displayName]) return;
[self.peers setValue:peerID forKey:peerID.displayName];
if (info == nil) {
info = [NSDictionary dictionary];
}
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityPeerFound"
body:@{
@"peer": @{
@"id": peerID.displayName,
@"info": info
}
}];
}
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID {
if ([peerID.displayName isEqualToString:self.peerID.displayName]) return;
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityPeerLost"
body:@{
@"peer": @{
@"id": peerID.displayName
}
}];
[self.peers removeObjectForKey:peerID.displayName];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL accept, MCSession *session))invitationHandler {
NSString *invitationUUID = [[NSUUID UUID] UUIDString];
[self.invitationHandlers setValue:[invitationHandler copy] forKey:invitationUUID];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityInviteReceived"
body:@{
@"invite": @{
@"id": invitationUUID
},
@"peer": @{
@"id": peerID.displayName
}
}];
}
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
if ([peerID.displayName isEqualToString:self.peerID.displayName]) return;
if (state == MCSessionStateConnected) {
[self.connectedPeers setValue:peerID forKey:peerID.displayName];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityPeerConnected"
body:@{
@"peer": @{
@"id": peerID.displayName
}
}];
}
else if (state == MCSessionStateConnecting) {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityPeerConnecting"
body:@{
@"peer": @{
@"id": peerID.displayName
}
}];
}
else if (state == MCSessionStateNotConnected) {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityPeerDisconnected"
body:@{
@"peer": @{
@"id": peerID.displayName
}
}];
[self.connectedPeers removeObjectForKey:peerID.displayName];
}
}
- (void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler {
certificateHandler(YES);
}
// TODO: Waiting for module interop and/or streams over JS bridge
//- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID {
// NSString *streamId = [[ObjectStore shared] putObject:stream];
// [self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityStreamOpened"
// body:@{
// @"stream": @{
// @"id": streamId,
// @"name": streamName
// },
// @"peer": @{
// @"id": peerID.displayName
// }
// }];
//}
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
NSError *error = nil;
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSDictionary *parsedJSON = [NSDictionary dictionary];
if([object isKindOfClass:[NSDictionary class]]) {
parsedJSON = object;
}
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RCTMultipeerConnectivityDataReceived"
body:@{
@"peer": @{
@"id": peerID.displayName
},
@"data": parsedJSON
}];
}
// TODO: Support file transfers once we have a general spec for representing files
//
//- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error {
// NSURL *destinationURL = [NSURL fileURLWithPath:@"/path/to/destination"];
// if (![[NSFileManager defaultManager] moveItemAtURL:localURL toURL:destinationURL error:&error]) {
// NSLog(@"[Error] %@", error);
// }
//}
//
//- (void)session:(MCSession *)session
//didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
//{
//
//}
@end