add protobuf, change topic
Signed-off-by: weboko <anon@mail.com>
This commit is contained in:
parent
0227dfe592
commit
4fddc02896
|
@ -8,7 +8,7 @@
|
|||
|
||||
<body>
|
||||
<h2>Status</h2>
|
||||
<div id='status'></div>
|
||||
<div id='status'>Connecting... | Connected | Disconnected</div>
|
||||
|
||||
<h2>Local Peer Id</h2>
|
||||
<div id='peer-id'></div>
|
||||
|
@ -30,6 +30,7 @@
|
|||
<button>Exit chat</button>
|
||||
</div>
|
||||
|
||||
<script src="//cdn.jsdelivr.net/npm/protobufjs@7.X.X/dist/protobuf.min.js"></script>
|
||||
<script type='module' src="./index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
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'
|
||||
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";
|
||||
|
||||
const MULTI_ADDR = "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm";
|
||||
const CONTENT_TOPIC = "/js-waku-examples/1/chat/utf8";
|
||||
const CONTENT_TOPIC = "/toy-chat/2/huilong/proto";
|
||||
const PROTOCOLS = ["filter", "lightpush"];
|
||||
|
||||
const { sendMessage, unsubscribeFromMessages } = await initializeWakuContext({
|
||||
multiAddr: MULTI_ADDR,
|
||||
protocols: PROTOCOLS,
|
||||
multiAddr: MULTI_ADDR,
|
||||
contentTopic: CONTENT_TOPIC,
|
||||
onMessageReceived: (message) => {
|
||||
console.log(message);
|
||||
onMessageReceived: ({ nick, timestamp, text }) => {
|
||||
console.log(timestamp, '\t', nick, ": ", text);
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -25,6 +25,11 @@ async function initializeWakuContext({
|
|||
const Decoder = new wakuMessage.DecoderV0(contentTopic);
|
||||
const Encoder = new wakuMessage.EncoderV0(contentTopic);
|
||||
|
||||
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();
|
||||
|
||||
await node.start();
|
||||
|
@ -32,16 +37,26 @@ async function initializeWakuContext({
|
|||
await node.dial(multiAddr, protocols);
|
||||
await waitForRemotePeer(node, protocols);
|
||||
|
||||
// Set a filter by using Decoder for a given ContentTopic
|
||||
const unsubscribeFromMessages = await node.filter.subscribe([Decoder], (wakuMessage) => {
|
||||
const messageText = utils.bytesToUtf8(wakuMessage.payload);
|
||||
onMessageReceived(messageText);
|
||||
const messageObj = ChatMessage.decode(wakuMessage.payload);
|
||||
onMessageReceived({
|
||||
...messageObj,
|
||||
text: utils.bytesToUtf8(messageObj.text),
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
unsubscribeFromMessages,
|
||||
sendMessage: async (value) => {
|
||||
sendMessage: async ({ text, nick }) => {
|
||||
const protoMessage = ChatMessage.create({
|
||||
nick,
|
||||
timestamp: Date.now(),
|
||||
text: utils.utf8ToBytes(text),
|
||||
});
|
||||
|
||||
await node.lightPush.push(Encoder, {
|
||||
payload: utils.utf8ToBytes(value)
|
||||
payload: ChatMessage.encode(protoMessage).finish(),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue