Skip to content

Commit

Permalink
send iceCandidates between clients added
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitinari committed Feb 22, 2022
1 parent 6dc76df commit 1cb4803
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions server/src/gateways/message.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export class MessageGateway implements OnGatewayInit, OnGatewayDisconnect {
});
}

@SubscribeMessage('candidate')
public iceCandidate(client: Socket, data: any): void {
console.log(data);

client.to(data.user).emit('candidate', {
user: client.id,
candidate: data.candidate,
});
}

@SubscribeMessage('reject-call')
public rejectCall(client: Socket, data: any): void {
client.to(data.from).emit('call-rejected', {
Expand Down
2 changes: 2 additions & 0 deletions web/src/hooks/useStartPeerSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const useStartPeerSession = (room, userMediaStream, localVideoRef) => {
}
});

peerVideoConnection.onNewIceCandidate((user,candidate) => peerVideoConnection.newIceCandidate(user,candidate));

peerVideoConnection.onAnswerMade((socket) => peerVideoConnection.callUser(socket));
}

Expand Down
22 changes: 21 additions & 1 deletion web/src/utils/PeerConnectionSession.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as io from 'socket.io-client';

const { RTCPeerConnection, RTCSessionDescription } = window;
const { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } = window;

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
Expand Down Expand Up @@ -40,6 +40,18 @@ class PeerConnectionSession {
callback(stream);
};

const emitIceCandidate = (id, candidate) => {
this.socket.emit('candidate', {user: id, candidate: candidate});
}

this.peerConnections[id].onicecandidate = function (event) {
// console.log({ id, _stream });
console.log("new ice candidate");
if(event.candidate){
emitIceCandidate(id,event.candidate)
}
};

console.log(this.peerConnections);
}

Expand Down Expand Up @@ -116,6 +128,14 @@ class PeerConnectionSession {
this.senders = [];
Object.keys(this.peerConnections).forEach(this.removePeerConnection.bind(this));
}

onNewIceCandidate(callback) {
this.socket.on('candidate', (response) => callback(response.user, response.candidate))
}

newIceCandidate(user, candidate) {
this.peerConnections[user].addIceCandidate(new RTCIceCandidate(candidate))
}
}

export const createPeerConnectionContext = () => {
Expand Down

0 comments on commit 1cb4803

Please sign in to comment.