Add multi SimplePear connection
authorgparant <g.parant@thecodingmachine.com>
Sat, 25 Apr 2020 15:14:05 +0000 (17:14 +0200)
committergparant <g.parant@thecodingmachine.com>
Sat, 25 Apr 2020 15:14:05 +0000 (17:14 +0200)
back/src/Controller/IoSocketController.ts
front/src/WebRtc/SimplePeer.ts

index 57adf92d3bbc50058cf6fd3c5eaa1195228b1e25..cd715bb8dadd83538007bee4306e54c8bd026321 100644 (file)
@@ -92,9 +92,17 @@ export class IoSocketController{
                 let clients : Array<any> = Object.values(this.Io.sockets.sockets);
 
                 //send start at one client to initialise offer webrtc
+                //send all users in room to create PeerConnection in front
+                let clientsId = clients.reduce((tabs : Array<any>, client: ExtWebSocket) => {
+                    if(!client.userId){
+                        return tabs;
+                    }
+                    tabs.push(client.userId);
+                    return tabs;
+                }, []);
                 clients.forEach((client: ExtWebSocket, index : number) => {
                     client.emit('webrtc-start', JSON.stringify({
-                        userId: client.userId,
+                        usersId: clientsId.filter((userId : any) => userId !== client.userId),
                         initiator : index === 0
                     }));
                 });
index 9d199ccc09cff93e3f840722d7a1193718a43a56..966254be61e6eb7403a0eddda6551b74460e9387 100644 (file)
@@ -8,6 +8,7 @@ export class SimplePeer {
     RoomId: string;
 
     PeerConnexion: any;
+    PeerConnexionArray: Array<any> = new Array<any>();
 
     constructor(Connexion: ConnexionInterface, roomId: string = "test-webrtc") {
         this.Connexion = Connexion;
@@ -46,7 +47,7 @@ export class SimplePeer {
         let data = JSON.parse(message);
 
         //create pear connexion of user stared
-        this.createPeerConnexion(data.initiator);
+        this.createPeerConnexion(data.usersId, data.initiator);
     }
 
     /**
@@ -54,16 +55,23 @@ export class SimplePeer {
      * @param userId
      * @param initiator
      */
-    createPeerConnexion(initiator : boolean = false){
-        this.PeerConnexion = new Peer({initiator: initiator});
-        this.addMedia();
+    createPeerConnexion(usersId : Array<string>, initiator : boolean = false) {
+        usersId.forEach((userId: any) => {
+            if(this.PeerConnexionArray[userId]){
+                return;
+            }
+            this.PeerConnexion = new Peer({initiator: initiator});
+
+            this.PeerConnexion.on('signal', (data: any) => {
+                this.sendWebrtcSignal(data);
+            });
 
-        this.PeerConnexion.on('signal', (data: any) => {
-            this.sendWebrtcSignal(data);
-        });
+            this.PeerConnexion.on('stream', (stream: MediaStream) => {
+                this.stream(stream);
+            });
 
-        this.PeerConnexion.on('stream', (stream: MediaStream) => {
-            this.stream(stream)
+            this.PeerConnexionArray[userId] = this.PeerConnexion;
+            this.addMedia(userId);
         });
     }
 
@@ -81,10 +89,10 @@ export class SimplePeer {
      */
     receiveWebrtcSignal(message: string) {
         let data = JSON.parse(message);
-        if(!this.PeerConnexion){
+        if(!this.PeerConnexionArray[data.userId]){
             return;
         }
-        this.PeerConnexion.signal(data.signal);
+        this.PeerConnexionArray[data.userId].signal(data.signal);
     }
 
     /**
@@ -100,7 +108,7 @@ export class SimplePeer {
      * Permit to update stream
      * @param stream
      */
-     addMedia () {
-         this.PeerConnexion.addStream(this.MediaManager.localStream) // <- add streams to peer dynamically
+     addMedia (userId : any) {
+         this.PeerConnexionArray[userId].addStream(this.MediaManager.localStream) // <- add streams to peer dynamically
     }
 }
\ No newline at end of file