ModuleBase.define("Invite",["Error"],function(Error) {
/**
* @desc 邀请Invite构造函数。
* @constructor
* @alias Invite
* @param {String} fromUserId - 邀请发起方用户id
* @param {String} toUserId - 邀请接受方用户id
* @param {Object} options- 参数
*/
var Invite = function(fromUserId,toUserId,options) {
/**
* Invite级别回调的枚举
* @readonly
* @enum {string}
*/
this.InviteCallbackEnum = {
CALL_ACCEPT_NOTIFY: 'call.accept.notify',
CALL_REJECT_NOTIFY: 'call.reject.notify',
CALL_TIMEOUT_NOTIFY: 'call.timeout.notify',
CALL_RINGING_NOTIFY: 'call.ringing.notify',
CALL_CANCEL_NOTIFY: 'call.cancel.notify',
RESPONSE_CALL_TIMEOUT_NOTIFY: 'response.call.timeout.notify',
CALL_CLOSE_NOTIFY: 'call.close.notify',
};
this.eventEmitter = new EventEmitter();
this.inviteId = ++ Task_Id;
this.fromUserId = fromUserId;
this.toUserId = toUserId;
this.options = options;
this.callTimeout = 60000; //发启邀请超时时长(毫秒),默认设置60秒;如收到振铃Ringing后清零重新开始计时
this.callBaseTime = null; //发启邀请超时逻辑处理的基线时间
this.callTimeoutIntervalId = null; //发启邀请超时逻辑处理的setIntervald值
this.onCallTimeout = 55000; //收到邀请超时时长(毫秒),默认设置60秒;如收到振铃Ringing后清零重新开始计时
this.onCallBaseTime = null; //收到邀请超时逻辑处理的基线时间
this.onCallTimeoutIntervalId = null; //收到邀请超时逻辑处理的setIntervald值
};
/*
* @desc 开启邀请
* @ignore
*/
Invite.prototype.callInner = function (){
console.debug("===invite.callInner()");
var self = this;
if(self.callBaseTime == null) {
self.callBaseTime = new Date();
}
self.callTimeoutIntervalId = setInterval(function(){
var currentTime = new Date();
var duration = currentTime - self.callBaseTime;
if(duration >= self.callTimeout) {
self.clearanceInner();
//通知到应用层
self.eventEmitter.emit(self.InviteCallbackEnum.CALL_TIMEOUT_NOTIFY, self.toUserId);
}
}, 500);
var deferred = when.defer();
signalClient.rtmServer.sendInviteCall(self.inviteId,self.fromUserId,self.toUserId,self.options).then(function() {
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/*
* @desc 收到邀请,不做处理的超时处理
* @ignore
*/
Invite.prototype.onCallTimeOutInner = function (){
console.debug("===invite.onCallTimeOutInner()");
var self = this;
if(self.onCallBaseTime == null) {
self.onCallBaseTime = new Date();
}
self.onCallTimeoutIntervalId = setInterval(function(){
var currentTime = new Date();
var duration = currentTime - self.onCallBaseTime;
if(duration >= self.onCallTimeout) {
self.clearanceInnerOnCall();
//通知到应用层
self.eventEmitter.emit(self.InviteCallbackEnum.RESPONSE_CALL_TIMEOUT_NOTIFY, self.fromUserId);
}
}, 500);
};
/*
* @desc 清场处理
* @ignore
*/
Invite.prototype.clearanceInner = function(){
clearInterval(this.callTimeoutIntervalId);
this.callTimeoutIntervalId = null;
this.callBaseTime = null;
};
/*
* @desc 清场处理
* @ignore
*/
Invite.prototype.clearanceInnerOnCall = function(){
clearInterval(this.onCallTimeoutIntervalId);
this.onCallTimeoutIntervalId = null;
this.onCallBaseTime = null;
};
/**
* @desc 取消邀请
*/
Invite.prototype.cancel = function (){
var deferred = when.defer();
var self = this;
signalClient.rtmServer.sendInviteCancel(self.inviteId,self.fromUserId,self.toUserId).then(function() {
self.clearanceInner();
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/**
* @desc 发送振铃
*/
Invite.prototype.ringing = function (){
var deferred = when.defer();
signalClient.rtmServer.sendInviteRinging(this.inviteId,this.fromUserId,this.toUserId).then(function() {
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/**
* @desc 接受邀请
* @param {Object} options,接受方发送的参数,也可以不填
*/
Invite.prototype.accept = function (options){
var self = this;
var deferred = when.defer();
signalClient.rtmServer.sendInviteAccept(self.inviteId,self.fromUserId,self.toUserId,options).then(function() {
self.clearanceInnerOnCall();
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/**
* @desc 拒绝邀请
* @param {String} reson 拒绝原因
*/
Invite.prototype.reject = function (reson){
var self = this;
var deferred = when.defer();
signalClient.rtmServer.sendInviteReject(self.inviteId,self.fromUserId,self.toUserId,reson).then(function() {
self.clearanceInnerOnCall();
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/**
* @desc 关闭
* @param reson 关闭原因
*/
Invite.prototype.close = function(reson){
var self = this;
var deferred = when.defer();
signalClient.rtmServer.sendInviteClose(self.inviteId,self.fromUserId,self.toUserId,reson).then(function() {
self.clearanceInner();
self.clearanceInnerOnCall();
deferred.resolve();
}).otherwise(function(error) {
deferred.reject(error);
});
return deferred.promise;
};
/**
*@desc 取消邀请回调通知
*@ignore
*/
Invite.prototype.emitCallCancelNotify = function(){
this.eventEmitter.emit(this.InviteCallbackEnum.CALL_CANCEL_NOTIFY,this.toUserId);
};
/**
*@desc 响铃回调通知
*@ignore
*/
Invite.prototype.emitCallRingingNotify = function(){
this.callBaseTime = new Date();
this.eventEmitter.emit(this.InviteCallbackEnum.CALL_RINGING_NOTIFY,this.toUserId);
};
/**
*@desc 邀请接受回调通知
*@ignore
*/
Invite.prototype.emitCallAcceptNotify = function(options){
console.log('options: ', options)
this.clearanceInner();
var optionsObj = null;
if(options){
optionsObj = JSON.parse(JSON.stringify(options));
}
this.eventEmitter.emit(this.InviteCallbackEnum.CALL_ACCEPT_NOTIFY,this.toUserId,optionsObj);
}
/**
*@desc 邀请拒绝回调通知
*@ignore
*/
Invite.prototype.emitCallRejectNotify = function(reson) {
this.clearanceInner();
this.eventEmitter.emit(this.InviteCallbackEnum.CALL_REJECT_NOTIFY,this.toUserId,reson);
}
/**
*@desc 邀请关闭回调通知
*@ignore
*/
Invite.prototype.emitCallCloseNotify = function(reson) {
this.clearanceInner();
this.clearanceInnerOnCall();
this.eventEmitter.emit(this.InviteCallbackEnum.CALL_CLOSE_NOTIFY,this.toUserId,reson);
}
/**
* @desc 邀请级别的回调
* @param {UserCallback} type - 回调枚举标识
* @param {Object} callback -回调方法名,可以自定义
*
* @example
* invite.addCallback(Invite.InviteCallbackEnum.CALL_ACCEPT_NOTIFY, onCallAccept);
* invite.addCallback(Invite.InviteCallbackEnum.CALL_REJECT_NOTIFY, onCallReject);
* invite.addCallback(Invite.InviteCallbackEnum.CALL_TIMEOUT_NOTIFY, onCallTimeout);
* invite.addCallback(Invite.InviteCallbackEnum.CALL_RINGING_NOTIFY, onCallRinging);
*
* invite.addCallback(Invite.InviteCallbackEnum.CALL_CANCEL_NOTIFY, onCallCancel);
* invite.addCallback(Invite.InviteCallbackEnum.RESPONSE_CALL_TIMEOUT_NOTIFY, onResponseCallTimeout);
*
* invite.addCallback(Invite.InviteCallbackEnum.CALL_CLOSE_NOTIFY, onCallClose);
* 邀请请求已被接受的回调
* param:toUserId - 邀请接收者用户ID
* param:options - 邀请参数,json对象
* function onCallAccept(toUserId,options){
* }
*
* 邀请请求已被拒绝的回调
* param:toUserId - 邀请接收者用户ID
* param:reason - 拒绝原因
* function onCallReject(toUserId,reson){
* }
*
* 邀请请求无响应,超时回调
* param:toUserId - 邀请接收者用户ID
* function onCallTimeout(toUserId){
* }
*
* 邀请请求响铃回调
* param:toUserId - 邀请接收者用户ID
* function onCallRinging(toUserId){
* }
*
* 邀请发启者取消邀请的回调
* param:fromUserId - 邀请发启用户ID
* function onCallCancel(fromUserId) {
*
* }
*
* 接收邀请者无响应,超时回调
* param:fromUserId - 邀请发启用户ID
* function onResponseCallTimeout(fromUserId){
*
* }
*
* 邀请关闭
* param:toUserId - 关闭发启用户ID
* param:reason - 关闭原因
* function onCallClose(fromUserId,reson){
*
* }
*/
Invite.prototype.addCallback = function(type, callback){
this.eventEmitter.on(type, callback);
}
return Invite;
});