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

View File

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

View File

@ -1,6 +1,9 @@
#import <React/RCTEventEmitter.h>
#import <React/RCTBridgeModule.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: RCT_EXTERN_METHOD(defaultPubsubTopic:
(RCTPromiseResolveBlock)resolve (RCTPromiseResolveBlock)resolve

View File

@ -1,7 +1,44 @@
import Gowaku import Gowaku
import React
@objc(ReactNative) @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:) @objc(defaultPubsubTopic:withRejecter:)
func defaultPubsubTopic(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void { func defaultPubsubTopic(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
@ -10,6 +47,8 @@ class ReactNative: NSObject {
@objc(newNode:withResolver:withRejecter:) @objc(newNode:withResolver:withRejecter:)
func newNode(config: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { func newNode(config: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
signalHandler = DefaultEventHandler(p:self)
GowakuSetMobileSignalHandler(signalHandler)
resolve(GowakuNewNode(config)) 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' import {decode, encode} from 'base-64'
const LINKING_ERROR = const LINKING_ERROR =
@ -35,10 +35,7 @@ export class WakuMessage {
} }
} }
/* var eventEmitter = new NativeEventEmitter(NativeModules.ReactNative);
// TODO: this has been only implemented in android for now
var eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
export function onMessage(cb: (arg0:any) => void) { export function onMessage(cb: (arg0:any) => void) {
eventEmitter.addListener("message", event => { 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.version = msg.version || 0;
signal.event.wakuMessage.contentTopic = msg.contentTopic; signal.event.wakuMessage.contentTopic = msg.contentTopic;
signal.event.wakuMessage.payload = new Uint8Array(decode(msg.payload).split("").map((c:any) => c.charCodeAt(0))); signal.event.wakuMessage.payload = new Uint8Array(decode(msg.payload).split("").map((c:any) => c.charCodeAt(0)));
cb(signal); cb(signal.event);
}) })
} }
*/
export class Config { export class Config {
host: String | null = null host: String | null = null