From d73ad6491c7b3312258c4712033d521f534077d7 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sun, 12 Apr 2020 21:52:08 -0400 Subject: [PATCH] call auto join - PoC --- app/Controllers/Ws/SignalingController.js | 45 ++++++++++++--------- app/Controllers/Ws/UserChannelController.js | 2 + 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/app/Controllers/Ws/SignalingController.js b/app/Controllers/Ws/SignalingController.js index 46c6d18..f4931b8 100644 --- a/app/Controllers/Ws/SignalingController.js +++ b/app/Controllers/Ws/SignalingController.js @@ -1,7 +1,7 @@ 'use strict' const User = use('App/Models/User'); const UserChildUtils = use('App/Utils/UserChildUtils'); -const Call = use('App/Models/Call'); +const Child = use('App/Models/Child'); const IceServer = use('App/Models/IceServer'); const UserChannel = use('App/Controllers/Ws/UserChannelController') const calls = {}; @@ -12,24 +12,24 @@ class SignalingController { this.user = auth.user; this.socket = socket; this.request = request; - this.register(call); - console.log(`User #${this.user.id} connected to call ${this.callId}`); + this.register(call) + .then(_ => { + console.log(`User #${this.user.id} connected to call ${this.callId}`); + }) + .catch(e => { + console.error(e); + }); } async register(callModel) { - if (!calls[this.callId]) { + if (!calls[this.callId]) calls[this.callId] = new CallSession(callModel, this.onCallEnded.bind(this)); - } else + else console.log(`Call #${this.callId} Already Found`); const callSession = calls[this.callId]; - callSession.registerUser(this.user, this.socket) - .then(success => { - if (!success) throw new Error('Invalid User'); - }) - .catch( - error => { - - }); + success = await callSession.registerUser(this.user, this.socket) + if (!success) throw new Error('Invalid User'); + return true; } onCallEnded(callId) { console.log(`Call ${callId} Ended`); @@ -59,8 +59,8 @@ class CallSession { const elapsed = ((now - this.startTime) / 60000).toPrecision(1); const newStateTimeout = 5; // 5 min const startedStateTimeout = 10; // 10 min - console.log(`Heartbeat for call #${this.callId} State: ${ - this.state}, time-elapsed: ${(elapsed)}min`); + // console.log(`Heartbeat for call #${this.callId} State: ${ + // this.state}, time-elapsed: ${(elapsed)}min`); if (this.state === 'ENDED') return this.endCall(); if (this.state === 'NEW' && elapsed >= newStateTimeout) return this.endCall(); @@ -80,6 +80,7 @@ class CallSession { if (this.state === 'ENDED') this.endCall(); } async registerUser(user, socket) { + if (!this.child) this.child = await Child.find(this.callModel.child_id); let userIndex = -1; if (this.user_1.id === user.id) userIndex = 1; @@ -96,13 +97,17 @@ class CallSession { if (this.state === 'STARTED') { await this.sendStandby(socket, userIndex); // Send event to other user about the call + console.log( + `trying to find user ${this[`user_${otherUser}`].id} channel...`); const otherUserChannel = UserChannel.getUserChannel(this[`user_${otherUser}`].id); - if (otherUserChannel) - otherUserChannel.emit('call:incoming', { - callId: this.callId, - child: (await Child.find(this.callModel.child_id)).toJSON() - }); + if (otherUserChannel) { + // console.log(otherUserChannel); + console.log('Sending notification to other user'); + const payload = {callId: this.callId, child: this.child.toJSON()}; + console.dir(payload); + otherUserChannel.emit('call:incoming', payload); + } } else if (this.state === 'IN_PROGRESS') await this.sendStart(socket, userIndex); return true; diff --git a/app/Controllers/Ws/UserChannelController.js b/app/Controllers/Ws/UserChannelController.js index dffab09..4216ff5 100644 --- a/app/Controllers/Ws/UserChannelController.js +++ b/app/Controllers/Ws/UserChannelController.js @@ -22,10 +22,12 @@ class UserChannelController { .catch(console.error); } emit(event, data) { + console.log(`sending ${event} to user #${this.user.id}`); this.socket.emit(event, data); } static getUserChannel(userId) { + console.log(`user ${userId} has socket? ${!!connectedUsers[userId]}`) return connectedUsers[userId]; } static isUserOnline(userId) {