feat: expose peers fuction

This commit is contained in:
Richard Ramos 2022-05-11 18:04:53 -04:00
parent ef9475eecf
commit 9251967b78
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
2 changed files with 44 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { defaultPubsubTopic, newNode, onMessage, peerID, relayPublish, relaySubscribe, start, stop, WakuMessage } from '@waku/react-native';
import { defaultPubsubTopic, listenAddresses, newNode, onMessage, peerID, relayEnoughPeers, relayPublish, relaySubscribe, relayUnsubscribe, start, stop, WakuMessage, connect, peerCnt, peers } from '@waku/react-native';
@ -20,10 +20,21 @@ export default function App() {
await relaySubscribe()
onMessage(event => {
console.log("EVENT RECEIVED: ", event)
console.log("Message Received: ", event)
})
console.log("enoughPeers?", await relayEnoughPeers())
console.log("addresses", await listenAddresses())
console.log("connecting...")
await connect("/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm", 5000)
console.log("connected!")
console.log("PeerCNT", await peerCnt())
console.log("Peers", await peers())
let msg: WakuMessage = new WakuMessage()
msg.contentTopic = "ABC"
msg.payload = new Uint8Array([1, 2, 3, 4, 5])
@ -34,6 +45,7 @@ export default function App() {
console.log("The messageID", messageID)
await relayUnsubscribe();
await stop(); // TODO: This must be called only once
})();

View File

@ -151,11 +151,11 @@ export function relaySubscribe(topic: String = ""): Promise<void> {
});
}
export function defaultPubsubTopic(): Promise<string> {
export function defaultPubsubTopic(): Promise<String> {
return ReactNative.defaultPubsubTopic();
}
export function listenAddresses(): Promise<Array<string>> {
export function listenAddresses(): Promise<Array<String>> {
return new Promise<Array<string>>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.listenAddresses());
if(response.error){
@ -166,7 +166,7 @@ export function listenAddresses(): Promise<Array<string>> {
});
}
export function addPeer(multiAddress: String, protocol: String): Promise<string> {
export function addPeer(multiAddress: String, protocol: String): Promise<String> {
return new Promise<string>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.addPeer(multiAddress, protocol));
if(response.error){
@ -177,9 +177,9 @@ export function addPeer(multiAddress: String, protocol: String): Promise<string>
});
}
export function connect(multiAddress: String): Promise<void> {
export function connect(multiAddress: String, ms: Number = 0): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.connect(multiAddress));
let response = JSON.parse(await ReactNative.connect(multiAddress, ms));
if(response.error){
reject(response.error);
} else {
@ -188,9 +188,9 @@ export function connect(multiAddress: String): Promise<void> {
});
}
export function connectPeerID(peerID: String): Promise<void> {
export function connectPeerID(peerID: String, ms: Number = 0): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.connectPeerID(peerID));
let response = JSON.parse(await ReactNative.connectPeerID(peerID, ms));
if(response.error){
reject(response.error);
} else {
@ -331,9 +331,29 @@ export function lightpushPublishEncSymmetric(msg: WakuMessage, symmetricKey: Str
});
}
export class Peer {
addrs: Array<String> = Array()
connected: Boolean = false
peerID: String = ""
protocols: Array<String> = Array()
constructor(addrs: Array<String>, connected: Boolean, peerID: String, protocols: Array<String>){
this.addrs = addrs;
this.connected = connected;
this.peerID = peerID;
this.protocols = protocols;
}
}
export function peers(): Promise<Array<Peer>> {
return new Promise<Array<Peer>>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.peers());
if(response.error){
reject(response.error);
} else {
resolve(response.result.map((x:any) => new Peer(x.addrs, x.connected, x.peerID, x.protocols)));
}
})
}
// TODO: peers
// TODO: relayStoreQuery
// TODO: storeQuery