From 9a1207a9398db9cf3b750b9ae9edb7e193a85463 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 14 May 2022 11:58:24 -0500 Subject: [PATCH] getting calling system all good I thinki --- packages/api/sockets/index.ts | 4 +- packages/core/sockets/channels.ts | 8 +- .../desktop/src/providers/StreamProvider.tsx | 99 ++++++++++++++++++- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/packages/api/sockets/index.ts b/packages/api/sockets/index.ts index 9d92537..7cc88b5 100644 --- a/packages/api/sockets/index.ts +++ b/packages/api/sockets/index.ts @@ -158,7 +158,7 @@ export default function InitializeWs(io: any) { const userSocketId = userIdsToSocketIds[req.userIdToCall]; io.to(userSocketId).emit( - `${ServerResponseChannels.RTC_NEW_USER_JOINED_RESPONSE_PREFIX}:${req.lineId}`, + ServerResponseChannels.RTC_NEW_USER_JOINED_RESPONSE, new RtcNewUserResponse(userInfo.userId, req.simplePeerSignal), ); }); @@ -167,7 +167,7 @@ export default function InitializeWs(io: any) { const userSocketId = userIdsToSocketIds[req.userIdToCall]; io.to(userSocketId).emit( - `${ServerResponseChannels.RTC_RECEIVING_ANSWER_RESPONSE_PREFIX}:${req.lineId}`, + ServerResponseChannels.RTC_RECEIVING_ANSWER_RESPONSE, new RtcReceiveAnswerResponse(userInfo.userId, req.simplePeerSignal), ); }); diff --git a/packages/core/sockets/channels.ts b/packages/core/sockets/channels.ts index ead5980..1d1a3e7 100644 --- a/packages/core/sockets/channels.ts +++ b/packages/core/sockets/channels.ts @@ -59,8 +59,8 @@ export enum ServerResponseChannels { SOMEONE_STOPPED_BROADCASTING = 'SOMEONE_STOPPED_BROADCASTING', // stop showing their stream tracks // sending to the correct room of tunedin folks AND also making sure it's the right event handler in the right handler for this component - RTC_NEW_USER_JOINED_RESPONSE_PREFIX = 'RTC_NEW_USER_JOINED_RESPONSE_PREFIX', - RTC_RECEIVING_ANSWER_RESPONSE_PREFIX = 'RTC_RECEIVING_ANSWER_RESPONSE_PREFIX', + RTC_NEW_USER_JOINED_RESPONSE = 'RTC_NEW_USER_JOINED_RESPONSE', + RTC_RECEIVING_ANSWER_RESPONSE = 'RTC_RECEIVING_ANSWER_RESPONSE', SOMEONE_GOING_INTO_FLOW_STATE = 'SOMEONE_GOING_INTO_FLOW_STATE', } @@ -110,7 +110,7 @@ export class SocketEmitter { } export class RtcCallRequest { - constructor(public lineId: string, public userIdToCall: string, public simplePeerSignal: any) {} + constructor(public userIdToCall: string, public simplePeerSignal: any) {} } export class RtcNewUserResponse { @@ -118,7 +118,7 @@ export class RtcNewUserResponse { } export class RtcAnswerRequest { - constructor(public lineId: string, public userIdToCall: string, public simplePeerSignal: any) {} + constructor(public userIdToCall: string, public simplePeerSignal: any) {} } export class RtcReceiveAnswerResponse { diff --git a/packages/desktop/src/providers/StreamProvider.tsx b/packages/desktop/src/providers/StreamProvider.tsx index c08e374..056a7eb 100644 --- a/packages/desktop/src/providers/StreamProvider.tsx +++ b/packages/desktop/src/providers/StreamProvider.tsx @@ -24,6 +24,16 @@ import Peer from 'simple-peer'; import useRealTimeRooms from './RealTimeRoomProvider'; import useAuth from './AuthProvider'; import { useImmer } from 'use-immer'; +import useSockets from './SocketProvider'; +import { + RtcAnswerRequest, + RtcCallRequest, + RtcNewUserResponse, + RtcReceiveAnswerResponse, + ServerRequestChannels, +} from '@nirvana/core/sockets/channels'; +import { ServerResponseChannels } from '../../../core/sockets/channels'; +import toast from 'react-hot-toast'; type PeerMap = { [userId: string]: Peer; @@ -40,7 +50,60 @@ export function StreamProvider({ children }: { children: React.ReactChild }) { const { roomsMap } = useRealTimeRooms(); const { user } = useAuth(); - const [peerMap, setPeerMap] = useImmer({}); + const { $ws } = useSockets(); + + const [peerMap, updatePeerMap] = useImmer({}); + + useEffect(() => { + $ws.on( + ServerResponseChannels.RTC_RECEIVING_ANSWER_RESPONSE, + (res: RtcReceiveAnswerResponse) => { + console.log(`oooo some master received my call and accepted it ${JSON.stringify(res)}`); + + // find the peer we created earlier for this master + // ?is this okay? using the setter to get the current state? + updatePeerMap((draft) => { + const peerForAnswerer = draft[res.answererUserId]; + + if (peerForAnswerer) { + peerForAnswerer.signal(res.simplePeerSignal); + } else { + toast.error('could not find the peer we created before for this master'); + } + }); + }, + ); + + $ws.on(ServerResponseChannels.RTC_NEW_USER_JOINED_RESPONSE, (res: RtcNewUserResponse) => { + console.log('ooo newbie joined room, I guess I will accept it and send him my signal'); + + const peerForMeAndNewbie = new Peer({ + initiator: false, + trickle: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times + }); + + peerForMeAndNewbie.on('signal', (signal) => { + console.log( + 'as the answerer, I am going to send back my signal so that the newbie can update his local peer for me', + ); + $ws.emit( + ServerRequestChannels.RTC_ANSWER_REQUEST, + new RtcAnswerRequest(res.newUserId, signal), + ); + }); + + peerForMeAndNewbie.signal(res.simplePeerSignal); + + updatePeerMap((draft) => { + draft[res.newUserId] = peerForMeAndNewbie; + }); + }); + + return () => { + $ws.removeListener(ServerResponseChannels.RTC_RECEIVING_ANSWER_RESPONSE); + $ws.removeListener(ServerResponseChannels.RTC_NEW_USER_JOINED_RESPONSE); + }; + }, [$ws, updatePeerMap, peerMap]); useEffect(() => { // get distinct peers that we need to build a connection with @@ -51,13 +114,39 @@ export function StreamProvider({ children }: { children: React.ReactChild }) { return; } - line.tunedInMemberIds.forEach((tunedUserId) => userIdsSet.add(tunedUserId)); + line.tunedInMemberIds.forEach((tunedUserId) => { + if (tunedUserId === user._id.toString()) return; + + userIdsSet.add(tunedUserId); + }); }); - console.log(userIdsSet); + console.log(`new distinct list of tuned in users`, userIdsSet); - // go call all of them - }, [user, roomsMap]); + updatePeerMap((draft) => { + // go call all of them, if I'm not already connected + userIdsSet.forEach((userIdToCall) => { + // ?don't call if we already have a connection? + if (draft[userIdToCall]) return; + + const localPeerConnection = new Peer({ + initiator: true, + trickle: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times + }); + + localPeerConnection.on('signal', (signal) => { + $ws.emit( + ServerRequestChannels.RTC_CALL_REQUEST, + new RtcCallRequest(userIdToCall, signal), + ); + }); + + draft[userIdToCall] = localPeerConnection; + }); + }); + }, [user, roomsMap, $ws, updatePeerMap]); //TODO: make this not run on EVERY update to roomsMap? only tuned in lists? so the separate map for that? + + console.log(peerMap); return ( {children}