Skip to content

Commit

Permalink
test: mdns
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohaodu committed May 24, 2024
1 parent 083e0e3 commit dba4654
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@libp2p/echo": "^1.0.1",
"@libp2p/floodsub": "^9.0.14",
"@libp2p/identify": "^1.0.16",
"@libp2p/mdns": "^10.0.24",
"@libp2p/mplex": "^10.0.17",
"@libp2p/peer-id": "^4.0.9",
"@libp2p/peer-id-factory": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/web-chat-x-vue/src/classes/DatabaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class DatabaseManager {
channels: "&id",
subscribers: "&id",
messages:
"&id, postUserId, answerUserId, channelId, &[postUserId+answerUserId], &[postUserId+channelId]",
"&[postUserId+answerUserId], postUserId, answerUserId, channelId, id, [postUserId+channelId]",
});
try {
await db.open();
Expand Down
31 changes: 23 additions & 8 deletions packages/web-chat-x-vue/src/classes/Libp2pManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { peerIdFromPeerId, peerIdFromString } from "@libp2p/peer-id";
import { PeerManager } from "./PeerManager";
import ChatChannel from "./ChatChannel";
import { base64ToFile, fileToBase64 } from "@/utils";
import { mdns } from "@libp2p/mdns";
const topics = [
`webChatX._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space
// "_peer-discovery._p2p._pubsub", // Include if you want to participate in the global space
Expand Down Expand Up @@ -247,6 +248,7 @@ export class Libp2pManager {
topics: topics, // defaults to ['_peer-discovery._p2p._pubsub']
listenOnly: false,
}),
mdns(),
],
services: {
identify: identify(),
Expand Down Expand Up @@ -361,15 +363,22 @@ export class Libp2pManager {
signal: AbortSignal.timeout(5000),
}
);
this.connectFriends();
this.subscribe();
timeIntervalStart();
this.cyclicQuery();
} catch (error) {
reject(error);
}
});
}

cyclicQuery() {
this.connectFriends();
this.subscribe();
setInterval(() => {
console.log(this.libp2p);
}, 1000);
}

async subscribeChannel(
channelName: string,
channelId: string,
Expand Down Expand Up @@ -597,12 +606,18 @@ export class Libp2pManager {
const remoteMessageObject = JSON.parse(
remoteMessageString
) as ProtocolFriendSendMessage;
await this.databaseManager?.putMessage(
false,
this.chatUser?.value.id!,
remoteMessageObject.user.peerId,
remoteMessageObject.message
);
const isFriend = this.chatUser?.value.friendIds.find((friendId) => {
return friendId == remoteMessageObject.user.peerId;
});
if (isFriend) {
await this.databaseManager?.putMessage(
false,
this.chatUser?.value.id!,
remoteMessageObject.user.peerId,
remoteMessageObject.message
);
} else {
}
await stream.close();
});
await this.libp2p.handle("/friend/sendFile", async ({ stream }) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/web-chat-x-vue/src/views/StartChatting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ async function friendRequest() {
privateChatFormData.userId
);
const remotePeerId = peerIdFromString(remotePeerIdString);
// "/dns/webchatx.mayuan.work/tcp/10000/ws/p2p/12D3KooWFzsY7wUBHwbrz6m9nFfLCDwqLD4LS9JykKxSZ4zqG7Pg/p2p-circuit/webrtc/p2p/"
await libp2pManager.requestFriend(
remotePeerId,
privateChatFormData.validationMessage ||
Expand All @@ -139,7 +138,7 @@ async function friendRequest() {
router.push({
name: "PrivateChat",
params: {
user_id: privateChatFormData.userId,
user_id: remotePeerIdString,
},
});
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dba4654

Please sign in to comment.