1.0.2 • Published 3 years ago

webim-lib v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

初始化及连接

  1. 引入

    通过标签方式直接引入

    <script src="/dist/bundle.js"></script>
    <!-- 
    #自动引入全局变量 webim
    #可自由选择 webim.WebIM/webim.WebRTC
    -->

    通过npm安装 npm i webim-lib

    import { WebIM , WebRTC } from 'webim-lib'
    • 其中 WebRTC 继承 WebIM ,包含 WebIM 完整实例功能
    • 为提高webrtc的兼容性,你可以自行引入 https://webrtc.github.io/adapter/adapter-latest.js
  1. 初始化

    const conn = new WebRTC( config )
    // const conn = new WebRTC( config )
    // config 可配置选项
    {
    	apiURL: "https://imapitest.wecloud.cn", //接口请求地址 | 必须
    	wsURL: 'wss://imapitest.wecloud.cn/ws',	//websocket URL | 必须
    	heartBeatWait: 15000,					//websocket心跳间隔时间
    	reConnectNumMax: '2',					//websocket 最大重连次数
    	reConnectTimeWait: 2000,				//websocket 重新连接等待时间
       sendMessageTimeout: 8000,			  	//websocket 发送消息超时时间
       videoConstraints:true,				   //video预设,默认为true
       audioConstraints:true					//audio预设,默认为true
       ...
    }
  2. 绑定回调事件

    conn.onClosed = function () { }
    conn.onTextMessage = function () { } 	//接收到文本消息时
    conn.onError = function () { }
    
    conn.onLocalStream = function (stream) { }	//可以用本地流时
    conn.onRemoteStream = function (stream) { }	//可以使用远端流时
    conn.onCall = function () { }			// 接收到被人呼叫时
    conn.onClientJoined = function () { }	//对方加入通话时
    conn.onRefuseAnswer = function () { } 	//对方拒绝通话邀请时
    conn.onClientLeave = function () { } 	//对方离开通话时
    ...
  3. 连接

    const option = {
        timestamp: 1635410291094,
        clientId: "44ds55224d4d",
        appKey: "ldZ3oal2LkthGhQp",
        sign:"95b885d82e4ab37e62bcdf3eee",
    } 
    conn.open(option)
  4. 关闭连接

    conn.close()

消息

  1. 文本消息

    const reqId = conn.createRequestId() //消息id,发送成功后会返回,可用于更新本地数据
    
    const msgBody = conn.creatMessage({
       reqId,
       type: 'text',
       msg:'hello!!!',
       toConversation:'44233d5s336ds333z'
       ext:{} //自定义扩展数据 
    })
    conn.send(msgBody).then( () => {} )

会话

  1. 创建会话

    const options = {
        name:'name', //会话名称
        clientIds:['clientA','clientB'] //会话中包含的用户,
        attributes:{}	//自定义属性
    }
    conn.createConversation(options).then((res)=>{ console.log(res) })
  2. 获取会话列表

    conn.getConversationList().then((res)=>{ console.log(res) })
  3. 设置会话状态

    const conversationIds = ['clientA']
    const state = false   //state 状态  true 显示, false隐藏
    conn.setConversationState(conversationIds,state).then(()=>{})
  4. 获取离线消息

    conn.getOfflineList().then((res)=>{ console.log(res) })
  5. 获取历史消息

    conn.getHistoryMsg(options).then((res)=>{ console.log(res) })
  6. 消息设为已读状态

    conn.setMessageRead(msgIds).then((res)=>{ console.log(res) })
  7. 消息设为已接收状态

    conn.setMessageReceived(msgIds).then((res)=>{ console.log(res) })
  8. 消息撤回

    conn.withdrawMessage(msgIds).then((res)=>{ console.log(res) })

RTC视频通话(1x1)

  1. 呼叫

    const option = {
        toClient:'5assd2s4w' //呼叫clientId
    }
    
    conn.call(option).catch((err)=>{ console.log(err) })
  2. 接听

    const option = {	//onCall回调会返回
        channelId:'4s53s2wews',	
        type:'video' // voice,video(默认)
    }
    conn.answer(option).catch((err)=>{ console.log(err) })
  3. 挂断

    conn.hangup().then(()=>{})
  1. 设置本地录像状态

    conn.setVideoMute(true)	//true:关闭录视频,false:开启录视频
  1. 设置本地录音状态

    conn.setAudioMute(true) //true:关闭录音,false:开启录音		
  2. 切换摄像头

    // 'environment' 后置摄像头,user 前置
    conn.switchCamera('environment')  //( video constrains)
  3. 设置视频参数

    const options = {
        height: 400,
        width: 600
    }
    conn.applyVideoConstraints(options) //  (video constrains)

    ...

即时通讯官网

https://www.wecloud.cn/product/message.html

体验简易demohttps://imwebtest.wecloud.cn