From dba4654ee5e8bba2fc3c4e0b8004c11c75b423b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E6=99=93=E6=B5=A9?= <1923911906@qq.com> Date: Fri, 24 May 2024 17:41:47 +0800 Subject: [PATCH] test: mdns --- package.json | 1 + .../src/classes/DatabaseManager.ts | 2 +- .../src/classes/Libp2pManager.ts | 31 ++++++++++++++----- .../src/views/StartChatting.vue | 3 +- pnpm-lock.yaml | 3 ++ 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 86d87a7..6507cfc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/web-chat-x-vue/src/classes/DatabaseManager.ts b/packages/web-chat-x-vue/src/classes/DatabaseManager.ts index d9ded98..90881c2 100644 --- a/packages/web-chat-x-vue/src/classes/DatabaseManager.ts +++ b/packages/web-chat-x-vue/src/classes/DatabaseManager.ts @@ -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(); diff --git a/packages/web-chat-x-vue/src/classes/Libp2pManager.ts b/packages/web-chat-x-vue/src/classes/Libp2pManager.ts index 390ac6b..24fd35d 100644 --- a/packages/web-chat-x-vue/src/classes/Libp2pManager.ts +++ b/packages/web-chat-x-vue/src/classes/Libp2pManager.ts @@ -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 @@ -247,6 +248,7 @@ export class Libp2pManager { topics: topics, // defaults to ['_peer-discovery._p2p._pubsub'] listenOnly: false, }), + mdns(), ], services: { identify: identify(), @@ -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, @@ -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 }) => { diff --git a/packages/web-chat-x-vue/src/views/StartChatting.vue b/packages/web-chat-x-vue/src/views/StartChatting.vue index d7771db..214e173 100644 --- a/packages/web-chat-x-vue/src/views/StartChatting.vue +++ b/packages/web-chat-x-vue/src/views/StartChatting.vue @@ -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 || @@ -139,7 +138,7 @@ async function friendRequest() { router.push({ name: "PrivateChat", params: { - user_id: privateChatFormData.userId, + user_id: remotePeerIdString, }, }); } catch (error) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2b21f4c..839a366 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: '@libp2p/identify': specifier: ^1.0.16 version: 1.0.21 + '@libp2p/mdns': + specifier: ^10.0.24 + version: 10.0.24 '@libp2p/mplex': specifier: ^10.0.17 version: 10.0.24