call auto join - PoC

This commit is contained in:
Sagi Dayan 2020-04-12 21:52:08 -04:00
parent 0247eb118d
commit d73ad6491c
2 changed files with 27 additions and 20 deletions

View file

@ -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;

View file

@ -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) {