2023-04-03 11:51:55 +00:00
|
|
|
import * as utils from "https://unpkg.com/@waku/utils@0.0.4/bundle/bytes.js";
|
|
|
|
import * as wakuCreate from "https://unpkg.com/@waku/create@0.0.12/bundle/index.js";
|
2022-12-21 20:22:23 +00:00
|
|
|
import {
|
|
|
|
waitForRemotePeer,
|
|
|
|
createDecoder,
|
|
|
|
createEncoder,
|
2023-04-03 11:51:55 +00:00
|
|
|
} from "https://unpkg.com/@waku/core@0.0.16/bundle/index.js";
|
2022-12-21 20:22:23 +00:00
|
|
|
|
2022-12-08 20:33:06 +00:00
|
|
|
const CONTENT_TOPIC = "/toy-chat/2/huilong/proto";
|
2022-12-07 22:46:22 +00:00
|
|
|
|
2022-12-12 11:41:04 +00:00
|
|
|
const ui = initUI();
|
|
|
|
runApp(ui).catch((err) => {
|
2022-12-21 20:22:23 +00:00
|
|
|
console.error(err);
|
|
|
|
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) {
|
2022-12-21 20:22:23 +00:00
|
|
|
ui.setStatus("connecting...", "progress");
|
|
|
|
|
|
|
|
const { info, sendMessage, unsubscribeFromMessages } = await initWakuContext({
|
|
|
|
contentTopic: CONTENT_TOPIC,
|
|
|
|
onMessageReceived: ui.renderMessage,
|
|
|
|
});
|
|
|
|
|
|
|
|
ui.setStatus("connected", "success");
|
|
|
|
|
|
|
|
ui.setLocalPeer(info.localPeerId);
|
|
|
|
ui.setRemotePeer(info.remotePeerIds);
|
|
|
|
ui.setContentTopic(info.contentTopic);
|
|
|
|
|
|
|
|
ui.onSendMessage(sendMessage);
|
|
|
|
|
|
|
|
ui.onExit(async () => {
|
|
|
|
ui.setStatus("disconnecting...", "progress");
|
|
|
|
await unsubscribeFromMessages();
|
|
|
|
ui.setStatus("disconnected", "terminated");
|
|
|
|
ui.resetMessages();
|
|
|
|
});
|
2022-12-08 23:21:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:51:55 +00:00
|
|
|
async function initWakuContext({ contentTopic, onMessageReceived }) {
|
2022-12-21 20:22:23 +00:00
|
|
|
const Decoder = createDecoder(contentTopic);
|
2023-04-03 11:51:55 +00:00
|
|
|
const Encoder = createEncoder({ contentTopic });
|
2022-12-21 20:22:23 +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"));
|
|
|
|
|
|
|
|
const node = await wakuCreate.createLightNode({ defaultBootstrap: true });
|
|
|
|
|
|
|
|
await node.start();
|
2023-04-03 11:51:55 +00:00
|
|
|
await waitForRemotePeer(node);
|
2022-12-21 20:22:23 +00:00
|
|
|
|
|
|
|
// Set a filter by using Decoder for a given ContentTopic
|
|
|
|
const unsubscribeFromMessages = await node.filter.subscribe(
|
|
|
|
[Decoder],
|
|
|
|
(wakuMessage) => {
|
|
|
|
const messageObj = ChatMessage.decode(wakuMessage.payload);
|
|
|
|
onMessageReceived({
|
|
|
|
...messageObj,
|
|
|
|
text: utils.bytesToUtf8(messageObj.text),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const localPeerId = node.libp2p.peerId.toString();
|
|
|
|
|
|
|
|
const remotePeers = await node.libp2p.peerStore.all();
|
|
|
|
const remotePeerIds = remotePeers.map((peer) => peer.id.toString());
|
|
|
|
|
|
|
|
return {
|
|
|
|
unsubscribeFromMessages,
|
|
|
|
info: {
|
|
|
|
contentTopic,
|
|
|
|
localPeerId,
|
|
|
|
remotePeerIds,
|
|
|
|
},
|
|
|
|
sendMessage: async ({ text, nick }) => {
|
|
|
|
if (!text || !nick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const protoMessage = ChatMessage.create({
|
|
|
|
nick,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
text: utils.utf8ToBytes(text),
|
|
|
|
});
|
|
|
|
|
2023-04-03 11:51:55 +00:00
|
|
|
await node.lightPush.send(Encoder, {
|
2022-12-21 20:22:23 +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() {
|
2022-12-21 20:22:23 +00:00
|
|
|
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 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) => {
|
|
|
|
sendButton.addEventListener("click", async () => {
|
|
|
|
await cb({
|
|
|
|
nick: nickText.value,
|
|
|
|
text: messageText.value,
|
|
|
|
});
|
|
|
|
messageText.value = "";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// UI renderers
|
|
|
|
setStatus: (value, className) => {
|
|
|
|
statusBlock.innerHTML = `<span class=${className || ""}>${value}</span>`;
|
|
|
|
},
|
|
|
|
setLocalPeer: (id) => {
|
|
|
|
localPeerBlock.innerText = id.toString();
|
|
|
|
},
|
|
|
|
setRemotePeer: (ids) => {
|
|
|
|
remotePeerId.innerText = ids.join("\n");
|
|
|
|
},
|
|
|
|
setContentTopic: (topic) => {
|
|
|
|
contentTopicBlock.innerText = topic.toString();
|
|
|
|
},
|
|
|
|
renderMessage: (messageObj) => {
|
|
|
|
const { nick, text, timestamp } = messageObj;
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
|
|
|
// WARNING: XSS vulnerable
|
|
|
|
messagesBlock.innerHTML += `
|
2022-12-08 23:21:08 +00:00
|
|
|
<div class="message">
|
|
|
|
<p>${nick} <span>(${date.toDateString()})</span>:</p>
|
|
|
|
<p>${text}</p>
|
|
|
|
<div>
|
|
|
|
`;
|
2022-12-21 20:22:23 +00:00
|
|
|
},
|
|
|
|
resetMessages: () => {
|
|
|
|
messagesBlock.innerHTML = "";
|
|
|
|
},
|
|
|
|
};
|
2022-12-08 23:21:08 +00:00
|
|
|
}
|