-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
82 lines (79 loc) · 1.91 KB
/
index.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
import {
NativeModules,
NativeEventEmitter,
} from 'react-native'
import React from 'react'
var NativeManager = NativeModules.NetworkManager
var NativeEmitter = new NativeEventEmitter(NativeModules.NetworkManager)
class NetworkManager {
// kind can be one of "WIFI", "BT", and "WIFI-BT"
browse(kind) {
NativeManager.browse(kind)
}
// kind can be one of "WIFI", "BT", and "WIFI-BT"
advertise(kind) {
NativeManager.advertise(kind)
}
stopAdvertising() {
NativeManager.stopAdvertising()
}
stopBrowsing() {
NativeManager.stopBrowsing()
}
disconnectFromPeer(peerId) {
NativeManager.disconnectFromPeer(peerId)
}
inviteUser(peerId) {
NativeManager.inviteUser(peerId)
}
sendMessage(message, peerId) {
NativeManager.sendMessage(message, peerId)
}
acceptInvitation(peerId) {
NativeManager.acceptInvitation(peerId)
}
getNearbyPeers(callback) {
NativeManager.getNearbyPeers((peers) => {
callback(peers)
})
}
getConnectedPeers(callback) {
NativeManager.getConnectedPeers((peers) => {
callback(peers)
})
}
/*listener callbacks
peer contains .id (string), type(string), connected(bool), message(string), display name(string)
*/
addPeerDetectedListener(callback) {
return NativeEmitter.addListener(
'detectedUser',
(peer) => callback(peer)
)
}
addPeerLostListener(callback) {
return NativeEmitter.addListener(
'lostUser',
(peer) => callback(peer)
)
}
addReceivedMessageListener(callback) {
return NativeEmitter.addListener(
'messageReceived',
(peer) => callback(peer)
);
}
addInviteListener(callback) {
return NativeEmitter.addListener(
'receivedInvitation',
(peer) => callback(peer)
)
}
addConnectedListener(callback) {
return NativeEmitter.addListener(
'connectedToUser',
(peer) => callback(peer)
)
}
}
module.exports = new NetworkManager()