TSignaling

TSignaling

new TSignaling(options) → {Object}

腾讯云 Web 信令 SDK 入口类。
接入前,您需要在 云通信控制台 中创建一个云通信应用,并取得 SDKAppID

Example
let options = {
  SDKAppID: 0 // 接入时需要将0替换为您的云通信应用的 SDKAppID
};
let tsignaling = new TSignaling(options);
Parameters:
Name Type Description
options Object

配置

Properties
Name Type Description
SDKAppID Number

云通信应用的 SDKAppID

offlineSupport Boolean

是否拉取历史未读,默认为 true(选填)

Returns:

SDK 实例

Type
Object

Methods

setLogLevel(level)

设置日志级别,低于 level 的日志将不会输出。

Example
tsignaling.setLogLevel(0);
Parameters:
Name Type Description
level Number

日志级别

  • 0 普通级别,日志量较多,接入时建议使用
  • 1 release级别,SDK 输出关键信息,生产环境时建议使用
  • 2 告警级别,SDK 只输出告警和错误级别的日志
  • 3 错误级别,SDK 只输出错误级别的日志
  • 4 无日志级别,SDK 将不打印任何日志

login(options) → {Promise}

登录

Example
let promise = tsignaling.login({userID: 'your userID', userSig: 'your userSig'});
promise.then(function(imResponse) {
  console.log(imResponse.data); // 登录成功
  if (imResponse.data.repeatLogin === true) {
    // 标识账号已登录,本次登录操作为重复登录
    console.log(imResponse.data.errorInfo);
  }
}).catch(function(imError) {
  console.warn('login error:', imError); // 登录失败的相关信息
});
Parameters:
Name Type Description
options Object

登录配置

Properties
Name Type Description
userID String

用户 ID

userSig String

用户登录即时通信 IM 的密码,其本质是对 UserID 等信息加密后得到的密文。
具体生成方法请参见生成 UserSig

Returns:
Type
Promise

logout() → {Promise}

登出,通常在切换帐号的时候调用,清除登录态以及内存中的所有数据。

Example
let promise = tsignaling.logout();
promise.then(function(imResponse) {
  console.log(imResponse.data); // 登出成功
}).catch(function(imError) {
  console.warn('logout error:', imError);
});
Returns:
Type
Promise

off(eventName, handler)

解除监听事件。

Example
let onTextMessageReceived = function(event) {
  event.data.forEach(function(message) {
    console.log('demo ' + (message.from || message.nick) + ' : ', message.payload.text);
  });
};
tsignaling.off(TSignaling.EVENT.TEXT_MESSAGE_RECEIVED, onTextMessageReceived);
Parameters:
Name Type Description
eventName String

事件名称。所有的事件名称都存放在 TSignaling.EVENT 变量中,如需要查看可以使用 console.log(TSignaling.EVENT) 把所有的事件显示出来。事件列表

handler function

处理事件的方法。

joinGroup(groupID) → {Promise}

加入群组

Example
let promise = tsignaling.joinGroup('group1');
promise.then(function(imResponse) {
  switch (imResponse.data.status) {
    case TSignaling.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
      break;
    case TSignaling.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
      console.log(imResponse.data.group); // 加入的群组资料
      break;
    case TSignaling.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
      break;
    default:
      break;
  }
}).catch(function(imError){
  console.warn('joinGroup error:', imError); // 申请加群失败的相关信息
});
Parameters:
Name Type Description
groupID String

群组 ID

Returns:
Type
Promise

quitGroup(groupID) → {Promise}

退出群组

Example
let promise = tsignaling.quitGroup('group1');
promise.then(function(imResponse) {
  console.log(imResponse.data.groupID); // 退出成功的群 ID
}).catch(function(imError){
  console.warn('quitGroup error:', imError); // 退出群组失败的相关信息
});
Parameters:
Name Type Description
groupID String

群组 ID

Returns:
Type
Promise

sendTextMessage(options) → {Promise}

发送文本消息

Example
let promise = tsignaling.sendTextMessage({
  to: 'AV1',
  groupFlag: true,
  priority: TSignaling.TYPES.MSG_PRIORITY_NORMAL,
  text: 'hello from TSignaling'
});
promise.then(function(imResponse) {
  console.log('demo sendTextMessage OK', imResponse);
}).catch(function(imError) {
  console.log('demo sendTextMessage failed', imError);
});
Parameters:
Name Type Description
options Object

消息参数

Properties
Name Type Attributes Default Description
to String

接收方 ID

groupFlag Boolean

true 发送到群组;false 发送给个人

priority String <optional>
"TSignaling.TYPES.MSG_PRIORITY_NORMAL"

消息优先级

text String

文本消息内容

Returns:
Type
Promise

sendCustomMessage(options) → {Promise}

发送自定义消息

Example
let promise = tsignaling.sendCustomMessage({
  to: 'AV1',
  groupFlag: true,
  priority:TSignaling.TYPES.MSG_PRIORITY_NORMAL,
  data: 'dianzan', // 用于标识该消息是点赞类型的自定义消息
  description: '',
  extension: ''
});
promise.then(function(imResponse) {
  console.log('demo sendCustomMessage OK', imResponse);
}).catch(function(imError) {
  console.log('demo sendCustomMessage failed', imError);
});
Parameters:
Name Type Description
options Object

消息参数

Properties
Name Type Attributes Default Description
to String

接收方ID

groupFlag Boolean

true 发送到群组;false 发送给个人

priority String <optional>
"TSignaling.TYPES.MSG_PRIORITY_NORMAL"

消息优先级

payload Object

自定义消息

Properties
Name Type Description
data String

自定义消息的数据字段

description String

自定义消息的说明字段

extension String

自定义消息的扩展字段

Returns:
Type
Promise

invite(options) → {Promise}

邀请单个人

Example
tsignaling.invite({
  userID: 'user1',
  data: JSON.stringify({content: 'xxx', platform: 'xxx'})
}).then(function(res) {
  console.log('demo invite OK', res);
}).catch(function(error) {
  console.log('demo invite failed', error.code, error.message);
});
Parameters:
Name Type Description
options Object

邀请配置

Properties
Name Type Attributes Default Description
userID String

用户 ID

data String <optional>
""

自定义数据

timeout Number <optional>
0

超时时间

offlinePushInfo Object <optional>

离线消息推送

Returns:
Type
Promise

inviteInGroup(options) → {Promise}

邀请群内的某些人

Example
tsignaling.inviteInGroup({
  groupID: 'AV1',
  inviteeList: ['user1', 'user2'],
  data: JSON.stringify({content: 'xxx', platform: 'xxx'}),
  timeout: 10
}).then(function(res) {
  console.log('demo inviteInGroup OK', res);
}).catch(function(error) {
  console.log('demo inviteInGroup failed', error.code, error.message);
});
Parameters:
Name Type Description
options Object

邀请配置

Properties
Name Type Attributes Default Description
groupID String

群组 ID

inviteeList Array

被邀请人列表

data String <optional>
""

自定义数据

timeout Number <optional>
0

超时时间

offlinePushInfo Object <optional>

离线消息推送

Returns:
Type
Promise

cancel(options) → {Promise}

邀请发起者取消邀请

Example
tsignaling.cancel({
  inviteID: '38897dbf-ecd4-4b59-a132-bc31529a2b18'
}).then(function(res) {
  console.log('demo cancel OK', res);
}).catch(function(error) {
  console.log('demo cancel failed', error.code, error.message);
});
Parameters:
Name Type Description
options Object

配置

Properties
Name Type Attributes Default Description
inviteID String

邀请信息的ID

data String <optional>
""

自定义数据

Returns:
Type
Promise

accept(options) → {Promise}

被邀请人接受邀请

Example
tsignaling.accept({
  inviteID: '38897dbf-ecd4-4b59-a132-bc31529a2b18'
}).then(function(res) {
  console.log('demo accept OK', res);
}).catch(function(error) {
  console.log('demo accept failed', error.code, error.message);
});
Parameters:
Name Type Description
options Object

配置

Properties
Name Type Attributes Default Description
inviteID String

邀请信息的ID

data String <optional>
""

自定义数据

Returns:
Type
Promise

reject(options) → {Promise}

被邀请人拒绝邀请

Example
tsignaling.reject({
  inviteID: '38897dbf-ecd4-4b59-a132-bc31529a2b18'
}).then(function(res) {
  console.log('demo reject OK', res);
}).catch(function(error) {
  console.log('demo reject failed', error.code, error.message);
});
Parameters:
Name Type Description
options Object

配置

Properties
Name Type Attributes Default Description
inviteID String

邀请信息的ID

data String <optional>
""

自定义数据

Returns:
Type
Promise