From 713d9e48c8f069f61c6df35f1095958b12df59c0 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Thu, 1 Aug 2024 16:54:08 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(y-webrtc)=20fix=20prob=20connectio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes the connection was not established correctly, because multiple connections were created at the same time. This commit fixes this issue by ensuring that only one connection is created at a time. --- .../apps/y-webrtc-signaling/src/server.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/frontend/apps/y-webrtc-signaling/src/server.ts b/src/frontend/apps/y-webrtc-signaling/src/server.ts index 704c7add..f8ec0167 100644 --- a/src/frontend/apps/y-webrtc-signaling/src/server.ts +++ b/src/frontend/apps/y-webrtc-signaling/src/server.ts @@ -68,7 +68,7 @@ const onconnection = (conn: WebSocket, url: string) => { subscribedTopics.forEach((topicName) => { const subs = topics.get(topicName) || new Set(); subs.forEach((sub) => { - if (sub.conn === conn) { + if (sub.url === url && sub.conn === conn) { subs.delete(sub); } }); @@ -96,9 +96,18 @@ const onconnection = (conn: WebSocket, url: string) => { topicName, () => new Set(), ); - topic.add({ url, conn }); - // add topic to conn - subscribedTopics.add(topicName); + + let isAlreadyAdded = false; + topic.forEach((sub) => { + if (sub.url === url && sub.conn === conn) { + isAlreadyAdded = true; + } + }); + + if (!isAlreadyAdded) { + topic.add({ url, conn }); + subscribedTopics.add(topicName); + } } }); break;