2022-12-08 20:33:06 +00:00
|
|
|
import * as utils from "https://unpkg.com/@waku/byte-utils@0.0.2/bundle/index.js";
|
|
|
|
import * as wakuCreate from "https://unpkg.com/@waku/create@0.0.4/bundle/index.js";
|
|
|
|
import { waitForRemotePeer } from "https://unpkg.com/@waku/core@0.0.6/bundle/lib/wait_for_remote_peer.js";
|
|
|
|
import * as wakuMessage from "https://unpkg.com/@waku/core@0.0.6/bundle/lib/waku_message/version_0.js";
|
2022-12-07 22:46:22 +00:00
|
|
|
|
|
|
|
const MULTI_ADDR = "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm";
|
2022-12-08 20:33:06 +00:00
|
|
|
const CONTENT_TOPIC = "/toy-chat/2/huilong/proto";
|
2022-12-07 22:46:22 +00:00
|
|
|
const PROTOCOLS = ["filter", "lightpush"];
|
|
|
|
|
2022-12-12 11:41:04 +00:00
|
|
|
const ui = initUI();
|
|
|
|
runApp(ui).catch((err) => {
|
2022-12-08 23:21:08 +00:00
|
|
|
console.error(err);
|
2022-12-12 11:41:04 +00:00
|
|
|
ui.setStatus(`error: ${err.message}`, "error");
|
2022-12-07 22:46:22 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:41:04 +00:00
|
|
|
async function runApp(ui) {
|
|
|
|
ui.setStatus("connecting...", "progress");
|
2022-12-08 23:21:08 +00:00
|
|
|
|
|
|
|
const { info, sendMessage, unsubscribeFromMessages } = await initWakuContext({
|
|
|
|
protocols: PROTOCOLS,
|
|
|
|
multiAddr: MULTI_ADDR,
|
|
|
|
contentTopic: CONTENT_TOPIC,
|
|
|
|
onMessageReceived: ui.renderMessage,
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:41:04 +00:00
|
|
|
ui.setStatus("connected", "success");
|
2022-12-08 23:21:08 +00:00
|
|
|
|
|
|
|
ui.setLocalPeer(info.localPeerId);
|
2022-12-12 11:46:51 +00:00
|
|
|
ui.setRemotePeer(info.remotePeerIds);
|
2022-12-08 23:21:08 +00:00
|
|
|
ui.setRemoteMultiAddr(info.multiAddr);
|
|
|
|
ui.setContentTopic(info.contentTopic);
|
|
|
|
|
|
|
|
ui.onSendMessage(sendMessage);
|
|
|
|
|
2022-12-09 18:42:31 +00:00
|
|
|
ui.onExit(async () => {
|
2022-12-12 11:41:04 +00:00
|
|
|
ui.setStatus("disconnecting...", "progress");
|
2022-12-09 18:42:31 +00:00
|
|
|
await unsubscribeFromMessages();
|
2022-12-12 11:41:04 +00:00
|
|
|
ui.setStatus("disconnected", "terminated");
|
2022-12-09 18:42:31 +00:00
|
|
|
ui.resetMessages();
|
2022-12-08 23:21:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function initWakuContext({
|
2022-12-07 22:46:22 +00:00
|
|
|
multiAddr,
|
|
|
|
protocols,
|
|
|
|
contentTopic,
|
|
|
|
onMessageReceived,
|
|
|
|
}) {
|
|
|
|
const Decoder = new wakuMessage.DecoderV0(contentTopic);
|
|
|
|
const Encoder = new wakuMessage.EncoderV0(contentTopic);
|
|
|
|
|
2022-12-08 20:33:06 +00:00
|
|
|
const ChatMessage = new protobuf.Type("ChatMessage")
|
|
|
|
.add(new protobuf.Field("timestamp", 1, "uint64"))
|
|
|
|
.add(new protobuf.Field("nick", 2, "string"))
|
|
|
|
.add(new protobuf.Field("text", 3, "bytes"));
|
|
|
|
|
2022-12-12 11:26:34 +00:00
|
|
|
const node = await wakuCreate.createLightNode({ defaultBootstrap: true });
|
2022-12-07 22:46:22 +00:00
|
|
|
|
|
|
|
await node.start();
|
|
|
|
await waitForRemotePeer(node, protocols);
|
|
|
|
|
2022-12-08 20:33:06 +00:00
|
|
|
// Set a filter by using Decoder for a given ContentTopic
|
2022-12-07 22:46:22 +00:00
|
|
|
const unsubscribeFromMessages = await node.filter.subscribe([Decoder], (wakuMessage) => {
|
2022-12-08 20:33:06 +00:00
|
|
|
const messageObj = ChatMessage.decode(wakuMessage.payload);
|
|
|
|
onMessageReceived({
|
|
|
|
...messageObj,
|
|
|
|
text: utils.bytesToUtf8(messageObj.text),
|
|
|
|
});
|
2022-12-07 22:46:22 +00:00
|
|
|
});
|
|
|
|
|
2022-12-08 23:21:08 +00:00
|
|
|
const localPeerId = node.libp2p.peerId.toString();
|
|
|
|
|
|
|
|
const remotePeers = await node.libp2p.peerStore.all();
|
2022-12-12 11:46:51 +00:00
|
|
|
const remotePeerIds = remotePeers.map(peer => peer.id.toString());
|
2022-12-08 23:21:08 +00:00
|
|
|
|
2022-12-07 22:46:22 +00:00
|
|
|
return {
|
|
|
|
unsubscribeFromMessages,
|
2022-12-08 23:21:08 +00:00
|
|
|
info: {
|
|
|
|
multiAddr,
|
|
|
|
contentTopic,
|
|
|
|
localPeerId,
|
2022-12-12 11:46:51 +00:00
|
|
|
remotePeerIds,
|
2022-12-08 23:21:08 +00:00
|
|
|
},
|
2022-12-08 20:33:06 +00:00
|
|
|
sendMessage: async ({ text, nick }) => {
|
2022-12-08 23:21:08 +00:00
|
|
|
if (!text || !nick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-08 20:33:06 +00:00
|
|
|
const protoMessage = ChatMessage.create({
|
|
|
|
nick,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
text: utils.utf8ToBytes(text),
|
|
|
|
});
|
|
|
|
|
2022-12-07 22:46:22 +00:00
|
|
|
await node.lightPush.push(Encoder, {
|
2022-12-08 20:33:06 +00:00
|
|
|
payload: ChatMessage.encode(protoMessage).finish(),
|
2022-12-07 22:46:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-12-08 23:21:08 +00:00
|
|
|
|
|
|
|
// UI adapter
|
|
|
|
function initUI() {
|
|
|
|
const exitButton = document.getElementById("exit");
|
|
|
|
const sendButton = document.getElementById("send");
|
|
|
|
|
|
|
|
const statusBlock = document.getElementById("status");
|
|
|
|
const localPeerBlock = document.getElementById("localPeerId");
|
|
|
|
const remotePeerId = document.getElementById("remotePeerId");
|
|
|
|
const remoteMultiAddr = document.getElementById("remoteMultiAddr");
|
|
|
|
const contentTopicBlock = document.getElementById("contentTopic");
|
|
|
|
|
|
|
|
const messagesBlock = document.getElementById("messages");
|
|
|
|
|
|
|
|
const nickText = document.getElementById("nickText");
|
|
|
|
const messageText = document.getElementById("messageText");
|
|
|
|
|
|
|
|
return {
|
|
|
|
// UI events
|
|
|
|
onExit: (cb) => {
|
|
|
|
exitButton.addEventListener("click", cb);
|
|
|
|
},
|
|
|
|
onSendMessage: (cb) => {
|
2022-12-09 18:42:31 +00:00
|
|
|
sendButton.addEventListener("click", async () => {
|
|
|
|
await cb({
|
2022-12-08 23:21:08 +00:00
|
|
|
nick: nickText.value,
|
|
|
|
text: messageText.value,
|
|
|
|
});
|
2022-12-09 18:42:31 +00:00
|
|
|
messageText.value = "";
|
2022-12-08 23:21:08 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
// UI renderers
|
2022-12-12 11:41:04 +00:00
|
|
|
setStatus: (value, className) => {
|
|
|
|
statusBlock.innerHTML = `<span class=${className || ""}>${value}</span>`;
|
2022-12-08 23:21:08 +00:00
|
|
|
},
|
|
|
|
setLocalPeer: (id) => {
|
|
|
|
localPeerBlock.innerText = id.toString();
|
|
|
|
},
|
2022-12-12 11:46:51 +00:00
|
|
|
setRemotePeer: (ids) => {
|
|
|
|
remotePeerId.innerText = ids.join("\n");
|
2022-12-08 23:21:08 +00:00
|
|
|
},
|
|
|
|
setRemoteMultiAddr: (multiAddr) => {
|
|
|
|
remoteMultiAddr.innerText = multiAddr.toString();
|
|
|
|
},
|
|
|
|
setContentTopic: (topic) => {
|
|
|
|
contentTopicBlock.innerText = topic.toString();
|
|
|
|
},
|
|
|
|
renderMessage: (messageObj) => {
|
|
|
|
const { nick, text, timestamp } = messageObj;
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
|
|
|
// WARNING: XSS vulnerable
|
|
|
|
messagesBlock.innerHTML += `
|
|
|
|
<div class="message">
|
|
|
|
<p>${nick} <span>(${date.toDateString()})</span>:</p>
|
|
|
|
<p>${text}</p>
|
|
|
|
<div>
|
|
|
|
`;
|
|
|
|
},
|
|
|
|
resetMessages: () => {
|
|
|
|
messagesBlock.innerHTML = "";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|