forked from lwansbrough/react-native-multipeer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultipeerConnection.js
135 lines (114 loc) · 3.79 KB
/
MultipeerConnection.js
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
var React = require('react-native');
var { DeviceEventEmitter, NativeModules } = React;
var EventEmitter = require('events').EventEmitter;
var RCTMultipeerConnectivity = NativeModules.MultipeerConnectivity;
var Peer = require('./Peer');
class MultipeerConnection extends EventEmitter {
constructor() {
super();
this._peers = {};
this._connectedPeers = {};
var peerFound = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityPeerFound',
((event) => {
var peer = new Peer(event.peer.id, event.peer.info.name);
this._peers[peer.id] = peer;
this.emit('peerFound', { peer });
}).bind(this));
var peerLost = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityPeerLost',
((event) => {
var peer = this._peers[event.peer.id];
peer.emit('lost');
this.emit('peerLost', { peer: { id: peer.id } });
delete this._peers[event.peer.id];
delete this._connectedPeers[event.peer.id];
}).bind(this));
var peerConnected = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityPeerConnected',
((event) => {
this._peers[event.peer.id].emit('connected');
this._connectedPeers[event.peer.id] = this._peers[event.peer.id];
this.emit('peerConnected', event);
}).bind(this));
var peerConnecting = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityPeerConnecting',
((event) => {
this._peers[event.peer.id].emit('connecting');
this.emit('peerConnecting', event);
}).bind(this));
var peerDisconnected = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityPeerDisconnected',
((event) => {
this._peers[event.peer.id].emit('disconnected');
delete this._connectedPeers[event.peer.id];
this.emit('peerDisconnected', event);
}).bind(this));
var streamOpened = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityStreamOpened',
((event) => {
this.emit('streamOpened', event);
}).bind(this));
var invited = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityInviteReceived',
((event) => {
event.sender = this._peers[event.peer.id];
this.emit('invite', event);
}).bind(this));
var dataReceived = DeviceEventEmitter.addListener(
'RCTMultipeerConnectivityDataReceived',
((event) => {
event.sender = this._peers[event.sender.id];
this.emit('data', event);
}).bind(this));
}
getAllPeers() {
return this._peers;
}
getConnectedPeers() {
return this._connectedPeers;
}
send(recipients, data, callback) {
if (!callback) {
callback = () => {};
}
var recipientIds = recipients.map((recipient) => {
if (recipient instanceof Peer) {
return recipient.id;
}
return recipient;
});
RCTMultipeerConnectivity.send(recipientIds, data, callback);
}
broadcast(data, callback) {
if (!callback) {
callback = () => {};
}
RCTMultipeerConnectivity.broadcast(data, callback);
}
invite(peerId, callback) {
if (!callback) {
callback = () => {};
}
RCTMultipeerConnectivity.invite(peerId, callback);
}
rsvp(inviteId, accept, callback) {
if (!callback) {
callback = () => {};
}
RCTMultipeerConnectivity.rsvp(inviteId, accept, callback);
}
advertise(channel, info) {
RCTMultipeerConnectivity.advertise(channel, info);
}
browse(channel) {
RCTMultipeerConnectivity.browse(channel);
}
// createStreamForPeer(peerId, name, callback) {
// if (!callback) {
// callback = () => {};
// }
// RCTMultipeerConnectivity.createStreamForPeer(peerId, name, callback);
// }
}
module.exports = MultipeerConnection;