forked from sagi/seepur
call auto join - PoC
This commit is contained in:
parent
0247eb118d
commit
d73ad6491c
2 changed files with 27 additions and 20 deletions
|
@ -1,7 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
const User = use('App/Models/User');
|
const User = use('App/Models/User');
|
||||||
const UserChildUtils = use('App/Utils/UserChildUtils');
|
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 IceServer = use('App/Models/IceServer');
|
||||||
const UserChannel = use('App/Controllers/Ws/UserChannelController')
|
const UserChannel = use('App/Controllers/Ws/UserChannelController')
|
||||||
const calls = {};
|
const calls = {};
|
||||||
|
@ -12,24 +12,24 @@ class SignalingController {
|
||||||
this.user = auth.user;
|
this.user = auth.user;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
this.register(call);
|
this.register(call)
|
||||||
|
.then(_ => {
|
||||||
console.log(`User #${this.user.id} connected to call ${this.callId}`);
|
console.log(`User #${this.user.id} connected to call ${this.callId}`);
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
console.error(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
async register(callModel) {
|
async register(callModel) {
|
||||||
if (!calls[this.callId]) {
|
if (!calls[this.callId])
|
||||||
calls[this.callId] =
|
calls[this.callId] =
|
||||||
new CallSession(callModel, this.onCallEnded.bind(this));
|
new CallSession(callModel, this.onCallEnded.bind(this));
|
||||||
} else
|
else
|
||||||
console.log(`Call #${this.callId} Already Found`);
|
console.log(`Call #${this.callId} Already Found`);
|
||||||
const callSession = calls[this.callId];
|
const callSession = calls[this.callId];
|
||||||
callSession.registerUser(this.user, this.socket)
|
success = await callSession.registerUser(this.user, this.socket)
|
||||||
.then(success => {
|
|
||||||
if (!success) throw new Error('Invalid User');
|
if (!success) throw new Error('Invalid User');
|
||||||
})
|
return true;
|
||||||
.catch(
|
|
||||||
error => {
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
onCallEnded(callId) {
|
onCallEnded(callId) {
|
||||||
console.log(`Call ${callId} Ended`);
|
console.log(`Call ${callId} Ended`);
|
||||||
|
@ -59,8 +59,8 @@ class CallSession {
|
||||||
const elapsed = ((now - this.startTime) / 60000).toPrecision(1);
|
const elapsed = ((now - this.startTime) / 60000).toPrecision(1);
|
||||||
const newStateTimeout = 5; // 5 min
|
const newStateTimeout = 5; // 5 min
|
||||||
const startedStateTimeout = 10; // 10 min
|
const startedStateTimeout = 10; // 10 min
|
||||||
console.log(`Heartbeat for call #${this.callId} State: ${
|
// console.log(`Heartbeat for call #${this.callId} State: ${
|
||||||
this.state}, time-elapsed: ${(elapsed)}min`);
|
// this.state}, time-elapsed: ${(elapsed)}min`);
|
||||||
if (this.state === 'ENDED') return this.endCall();
|
if (this.state === 'ENDED') return this.endCall();
|
||||||
if (this.state === 'NEW' && elapsed >= newStateTimeout)
|
if (this.state === 'NEW' && elapsed >= newStateTimeout)
|
||||||
return this.endCall();
|
return this.endCall();
|
||||||
|
@ -80,6 +80,7 @@ class CallSession {
|
||||||
if (this.state === 'ENDED') this.endCall();
|
if (this.state === 'ENDED') this.endCall();
|
||||||
}
|
}
|
||||||
async registerUser(user, socket) {
|
async registerUser(user, socket) {
|
||||||
|
if (!this.child) this.child = await Child.find(this.callModel.child_id);
|
||||||
let userIndex = -1;
|
let userIndex = -1;
|
||||||
if (this.user_1.id === user.id)
|
if (this.user_1.id === user.id)
|
||||||
userIndex = 1;
|
userIndex = 1;
|
||||||
|
@ -96,13 +97,17 @@ class CallSession {
|
||||||
if (this.state === 'STARTED') {
|
if (this.state === 'STARTED') {
|
||||||
await this.sendStandby(socket, userIndex);
|
await this.sendStandby(socket, userIndex);
|
||||||
// Send event to other user about the call
|
// Send event to other user about the call
|
||||||
|
console.log(
|
||||||
|
`trying to find user ${this[`user_${otherUser}`].id} channel...`);
|
||||||
const otherUserChannel =
|
const otherUserChannel =
|
||||||
UserChannel.getUserChannel(this[`user_${otherUser}`].id);
|
UserChannel.getUserChannel(this[`user_${otherUser}`].id);
|
||||||
if (otherUserChannel)
|
if (otherUserChannel) {
|
||||||
otherUserChannel.emit('call:incoming', {
|
// console.log(otherUserChannel);
|
||||||
callId: this.callId,
|
console.log('Sending notification to other user');
|
||||||
child: (await Child.find(this.callModel.child_id)).toJSON()
|
const payload = {callId: this.callId, child: this.child.toJSON()};
|
||||||
});
|
console.dir(payload);
|
||||||
|
otherUserChannel.emit('call:incoming', payload);
|
||||||
|
}
|
||||||
} else if (this.state === 'IN_PROGRESS')
|
} else if (this.state === 'IN_PROGRESS')
|
||||||
await this.sendStart(socket, userIndex);
|
await this.sendStart(socket, userIndex);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -22,10 +22,12 @@ class UserChannelController {
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
}
|
}
|
||||||
emit(event, data) {
|
emit(event, data) {
|
||||||
|
console.log(`sending ${event} to user #${this.user.id}`);
|
||||||
this.socket.emit(event, data);
|
this.socket.emit(event, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static getUserChannel(userId) {
|
static getUserChannel(userId) {
|
||||||
|
console.log(`user ${userId} has socket? ${!!connectedUsers[userId]}`)
|
||||||
return connectedUsers[userId];
|
return connectedUsers[userId];
|
||||||
}
|
}
|
||||||
static isUserOnline(userId) {
|
static isUserOnline(userId) {
|
||||||
|
|
Loading…
Reference in a new issue