1.0.6 • Published 9 months ago

best-call v1.0.6

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

best-call

基于 JsSIP 实现的 webrtc 电话条,用于 web 端语音通话

安装

npm install best-call

初始化

初始化加载 sdk 的参数说明:

参数类型说明是否必填
hoststringsip 服务器地址必填项
portnumbersip 服务器端口必填项
fsHoststringfreeswitch 服务器地址必填项
fsPortnumberfreeswitch 服务器地址服务器端口必填项
extNostring分机账号必填项
extPwdstring分机密码必填项
checkMicboolean麦克风检测必填项
stunObjectstun 服务器配置选填项
stateEventListenerFunction状态回调函数方法 参照文档下方 stateEventListener 详细说明必填项,需注入状态回调
import BestCall, { InitConfig } from "best-call";

const bestCall: BestCall;

/**
 * @description: 回调函数,所有状态都会通过这个函数返回
 * @param {string} event 事件状态名称,通过类型文件里的State枚举获取
 * @param {object} data 事件数据,一些报错信息或页面需要用到的数据
 */
const stateEventListener = (event, data) => {
  switch (event) {
    case "ERROR":
      break;
    case "CONNECTED":
      break;
    default:
  }
};
// 初始化配置信息(详情请看InitConfig类型定义)
  enum StunType {
    STUN = "stun",
    TURN = "turn",
  }
const config: InitConfig = {
  host: "127.0.0.1",
  port: "5066",
  fsHost:"127.0.0.1",
  fsPort: "5066",
  extNo: "1001",
  extPwd: "1234",
  stun:{
    type: StunType.STUN,
    host: "stun.l.google.com:19302"
  },
  checkMic: true,
  stateEventListener: stateEventListener,
};
// 初始化
this.bestCall = new BestCall(config);

接听

this.bestCall.answer();

退出

this.bestCall.unregister();
this.bestCall.cleanSdk();

流程说明

1、初始化

1、检查麦克风权限

2、调用初始化方法 this.bestCall = new BestCall(config),建议用户登录业务系统的时候就进行初始化,要求全局唯一,切记不能每次拨打电话的时候都初始化一次。

3、收到回调事件「REGISTERED」表示注册成功。错误处理:监听事件,收到「DISCONNECTED」、「REGISTER_FAILED」做出相应提示

文档说明

提供如下方法:

函数调用方式说明
初始化new BestCall(config)
销毁 SDKcleanSDK()
检查麦克风micCheck()异步接口,若麦克风异常会回调 MIC_ERROR 事件
注册register()
取消注册unregister()
呼叫请求call(phone,extraParam={})phone 为外呼号码,extraParam 为可选的扩展参数(可以不传)
挂断电话hangup()
应答接听answer()
保持hold()
取消保持unhold()
转接通话transfer(phone)
按键sendDtmf(tone)按键或二次拨号

状态回调(stateEventListener)

前端注入状态回调函数,通过状态回调 控制页面按钮显示

stateEventListener 回调参数为 event, data

Event 事件列表返回值状态说明
MIC_ERROR{msg: xxxx}麦克风检测异常
ERROR{msg: xxxx}错误异常
CONNECTED{localAgent: '1001'}连接成功
DISCONNECTED无返回值websocket 连接失败
REGISTERED无返回值注册成功
UNREGISTERED无返回值取消注册
REGISTER_FAILED{msg: xxxx}注册失败
INCOMING_CALL/OUTGOING_CALL{direction: 'inbound', otherLegNumber: '138xxxxxxxx', 'callId': 'xxxxxxx'} 说明:direction 为呼叫方向:inbound 呼入,outbound 呼出;otherLegNumber:第三方呼叫记录呼入振铃/外呼响铃
IN_CALL无返回值通话中
HOLD无返回值保持中
CALL_ENDCallEndEvent通话结束
MUTE无返回值静音
UNMUTE无返回值取消静音
LATENCY_STATLatencyStat网络延迟统计
MESSAGE_INCOMINGnewMessage消息接收

CallEndEvent

属性必须类型说明
answeredboolean是否接通(以后端为准)
originatorstring发起方(挂断方):local 本地(自己),remote 远程(对方)
causestring挂断原因
codenumber当 originator=remote,且 answered=false 时存在
1.0.6

9 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago