feat: add support for receiving messages in ios

This commit is contained in:
Richard Ramos 2022-05-15 11:36:30 -04:00
parent 1ad33ba4c8
commit ff6618cb4c
5 changed files with 50 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { defaultPubsubTopic, newNode, start, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage/*, onMessage*/ } from '@waku/react-native';
import { defaultPubsubTopic, newNode, start, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage, onMessage } from '@waku/react-native';
export default function App() {
const [result, setResult] = React.useState<string | undefined>();
@ -16,12 +16,9 @@ export default function App() {
await relaySubscribe()
/*
// TODO: this is only implemented in android for now
onMessage(event => {
console.log("Message Received: ", event)
})
*/
console.log("enoughPeers?", await relayEnoughPeers())
console.log("addresses", await listenAddresses())

View File

@ -1,2 +1,3 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
#import <React/RCTEventDispatcher.h>

View File

@ -1,6 +1,9 @@
#import <React/RCTEventEmitter.h>
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(ReactNative, NSObject)
@interface RCT_EXTERN_MODULE(ReactNative, RCTEventEmitter)
RCT_EXTERN_METHOD(supportedEvents)
RCT_EXTERN_METHOD(defaultPubsubTopic:
(RCTPromiseResolveBlock)resolve

View File

@ -1,8 +1,45 @@
import Gowaku
import React
@objc(ReactNative)
class ReactNative: NSObject {
class ReactNative: RCTEventEmitter {
var hasListener: Bool = false
override func startObserving() {
hasListener = true
}
override func stopObserving() {
hasListener = false
}
func sendEvent(signalJson: String) {
if hasListener {
self.sendEvent(withName:"message", body:["signal": signalJson]);
}
}
@objc
override func supportedEvents() -> [String]! {
return ["message"];
}
var signalHandler: GowakuSignalHandlerProtocol?
class DefaultEventHandler : NSObject, GowakuSignalHandlerProtocol {
var parent: ReactNative
init(p: ReactNative) {
parent = p
super.init()
}
public func handleSignal(_ p0: String?) {
parent.sendEvent(signalJson: p0!)
}
}
@objc(defaultPubsubTopic:withRejecter:)
func defaultPubsubTopic(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDefaultPubsubTopic())
@ -10,6 +47,8 @@ class ReactNative: NSObject {
@objc(newNode:withResolver:withRejecter:)
func newNode(config: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
signalHandler = DefaultEventHandler(p:self)
GowakuSetMobileSignalHandler(signalHandler)
resolve(GowakuNewNode(config))
}

View File

@ -1,4 +1,4 @@
import { NativeModules, Platform /*, NativeEventEmitter */} from 'react-native';
import { NativeModules, Platform, NativeEventEmitter} from 'react-native';
import {decode, encode} from 'base-64'
const LINKING_ERROR =
@ -35,10 +35,7 @@ export class WakuMessage {
}
}
/*
// TODO: this has been only implemented in android for now
var eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
var eventEmitter = new NativeEventEmitter(NativeModules.ReactNative);
export function onMessage(cb: (arg0:any) => void) {
eventEmitter.addListener("message", event => {
@ -49,10 +46,9 @@ export function onMessage(cb: (arg0:any) => void) {
signal.event.wakuMessage.version = msg.version || 0;
signal.event.wakuMessage.contentTopic = msg.contentTopic;
signal.event.wakuMessage.payload = new Uint8Array(decode(msg.payload).split("").map((c:any) => c.charCodeAt(0)));
cb(signal);
cb(signal.event);
})
}
*/
export class Config {
host: String | null = null
@ -65,7 +61,7 @@ export class Config {
}
export function newNode(config: Config | null): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.newNode(config ? JSON.stringify(config) : ""));
if(response.error){
reject(response.error);