mirror of
https://github.com/status-im/js-waku.git
synced 2025-02-23 02:18:25 +00:00
Nim-interop: nim sends message (not working)
This commit is contained in:
parent
8ed47b2cf7
commit
ee38df8757
@ -84,8 +84,6 @@ test('Nim-interop: js node subscribes to default waku topic (only checking js si
|
|||||||
|
|
||||||
const peerId = node.peerId.toB58String();
|
const peerId = node.peerId.toB58String();
|
||||||
|
|
||||||
console.log(`js peer id: ${peerId}`);
|
|
||||||
|
|
||||||
const localMultiaddr = node.multiaddrs.find((addr) =>
|
const localMultiaddr = node.multiaddrs.find((addr) =>
|
||||||
addr.toString().match(/127\.0\.0\.1/)
|
addr.toString().match(/127\.0\.0\.1/)
|
||||||
);
|
);
|
||||||
@ -104,6 +102,40 @@ test('Nim-interop: js node subscribes to default waku topic (only checking js si
|
|||||||
t.true(subscribers.includes(nimPeerId));
|
t.true(subscribers.includes(nimPeerId));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Nim-interop: nim node sends message', async (t) => {
|
||||||
|
const node = await createNode();
|
||||||
|
|
||||||
|
const peerId = node.peerId.toB58String();
|
||||||
|
|
||||||
|
console.log(`js peer id: ${peerId}`);
|
||||||
|
|
||||||
|
const localMultiaddr = node.multiaddrs.find((addr) =>
|
||||||
|
addr.toString().match(/127\.0\.0\.1/)
|
||||||
|
);
|
||||||
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
||||||
|
|
||||||
|
const nimWaku = new NimWaku();
|
||||||
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
||||||
|
|
||||||
|
const wakuRelayNode = new WakuRelay(node.pubsub);
|
||||||
|
await wakuRelayNode.subscribe();
|
||||||
|
|
||||||
|
// Setup the promise before publishing to ensure the event is not missed
|
||||||
|
const promise = waitForNextData(node.pubsub);
|
||||||
|
|
||||||
|
const message = Message.fromString('This is a message.');
|
||||||
|
|
||||||
|
await delay(500);
|
||||||
|
|
||||||
|
await nimWaku.sendMessage(message);
|
||||||
|
|
||||||
|
await delay(1000);
|
||||||
|
|
||||||
|
const received = await promise;
|
||||||
|
|
||||||
|
t.true(received.isEqualTo(message));
|
||||||
|
});
|
||||||
|
|
||||||
function waitForNextData(pubsub: Pubsub): Promise<Message> {
|
function waitForNextData(pubsub: Pubsub): Promise<Message> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
pubsub.once(TOPIC, resolve);
|
pubsub.once(TOPIC, resolve);
|
||||||
|
@ -7,7 +7,7 @@ export class Message {
|
|||||||
public contentTopic: number;
|
public contentTopic: number;
|
||||||
public version: number;
|
public version: number;
|
||||||
|
|
||||||
private constructor(private protobuf: WakuMessage) {
|
private constructor(public protobuf: WakuMessage) {
|
||||||
this.protobuf = protobuf;
|
this.protobuf = protobuf;
|
||||||
|
|
||||||
const msg = protobuf.toObject();
|
const msg = protobuf.toObject();
|
||||||
@ -27,7 +27,7 @@ export class Message {
|
|||||||
// This is the content topic commonly used at this time
|
// This is the content topic commonly used at this time
|
||||||
wakuMsg.setContentTopic(1);
|
wakuMsg.setContentTopic(1);
|
||||||
|
|
||||||
wakuMsg.setPayload(Buffer.from(message));
|
wakuMsg.setPayload(message);
|
||||||
|
|
||||||
return new Message(wakuMsg);
|
return new Message(wakuMsg);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,9 @@ import Multiaddr from 'multiaddr';
|
|||||||
import multiaddr from 'multiaddr';
|
import multiaddr from 'multiaddr';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
|
import { Message } from '../lib/waku_message';
|
||||||
|
import { TOPIC } from '../lib/waku_relay';
|
||||||
|
|
||||||
import waitForLine from './log_file';
|
import waitForLine from './log_file';
|
||||||
|
|
||||||
const openAsync = promisify(fs.open);
|
const openAsync = promisify(fs.open);
|
||||||
@ -77,6 +80,37 @@ export class NimWaku {
|
|||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async sendMessage(message: Message) {
|
||||||
|
this.checkProcess();
|
||||||
|
|
||||||
|
let payload;
|
||||||
|
if (typeof message.payload === 'string') {
|
||||||
|
payload = strToHex(message.payload);
|
||||||
|
} else {
|
||||||
|
payload = bufToHex(message.payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rpcMessage = {
|
||||||
|
payload,
|
||||||
|
contentTopic: message.contentTopic,
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await this.rpcCall('post_waku_v2_relay_v1_message', [
|
||||||
|
TOPIC,
|
||||||
|
rpcMessage,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return res.result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async messages() {
|
||||||
|
this.checkProcess();
|
||||||
|
|
||||||
|
const res = await this.rpcCall('get_waku_v2_relay_v1_messages', [TOPIC]);
|
||||||
|
|
||||||
|
return res.result;
|
||||||
|
}
|
||||||
|
|
||||||
get peerId(): PeerId {
|
get peerId(): PeerId {
|
||||||
return NIM_WAKU_PEER_ID;
|
return NIM_WAKU_PEER_ID;
|
||||||
}
|
}
|
||||||
@ -143,3 +177,27 @@ export function mergeArguments(args: Args): Args {
|
|||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Test this
|
||||||
|
function strToHex(str: string): string {
|
||||||
|
let hex: string;
|
||||||
|
try {
|
||||||
|
hex = unescape(encodeURIComponent(str))
|
||||||
|
.split('')
|
||||||
|
.map(function (v) {
|
||||||
|
return v.charCodeAt(0).toString(16).toUpperCase();
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
} catch (e) {
|
||||||
|
hex = str;
|
||||||
|
console.log('invalid text input: ' + str);
|
||||||
|
}
|
||||||
|
return '0x' + hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Test this
|
||||||
|
function bufToHex(buffer: Uint8Array) {
|
||||||
|
return Array.prototype.map
|
||||||
|
.call(buffer, (x) => ('00' + x.toString(16)).slice(-2))
|
||||||
|
.join('');
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user