vue-cli项目集成:环信-实时视频

1.npm install easemob-websdk --save

2.npm install strophe.js

3.找到node_modules/easemob-websdk/src/connection.js 在17行加入

var Strophe = require('strophe.js').Strophe;
var meStrophe = require('strophe.js');
$iq = meStrophe.$iq;
$build = meStrophe.$build;
$msg = meStrophe.$msg;
$pres = meStrophe.$pres;

5d9546c5ce717230e4746946173cc5b87f5.jpg

4.在737行 加入

var config = {    
    xmppURL: 'im-api.easemob.com',    
    apiURL: (location.protocol === 'https:' ? 'https:' : 'http:') + '//a1.easemob.com',    
    appkey: '*****************', //去环信自己申请一个appkey就行    
    https: false,    
    isMultiLoginSessions: true,    
    isAutoLogin: true,    
    isWindowSDK: false,    
    isSandBox: false,    
    isDebug: false,    
    autoReconnectNumMax: 2,    
    autoReconnectInterval: 2,    
    isWebRTC: (/Firefox/.test(navigator.userAgent) || /WebKit/.test(navigator.userAgent)) && /^https\:$/.test(window.location.protocol),    
    heartBeatWait: 4500,    
    isHttpDNS: false,    
    msgStatus: true,    
    delivery: true,    
    read: true,    
    saveLocal: false,    
    encrypt: {        
        type: 'none'    
    }
}

8e88f4f5c07f3ede7c5c6d211e5a4762c00.jpg

5.在4654行加入:

WebIM.config = config;

2526d75a18f784d43d3f44feba642c3fac7.jpg

6.找到node_modules/strophe.js/dist/strophe.js 在2892行加入以下代码

  setJid: function (jid) {    
    this.jid = jid;    
    this.authzid = Strophe.getBareJidFromJid(this.jid);    
    this.authcid = Strophe.getNodeFromJid(this.jid);  
  },  
  getJid: function () {    
    return this.jid;  
  },

8c36c086d71d242ad721f50874d370ce9af.jpg

7.接下来就可以在main中使用了

258fc43c835c819dff135829816a239783c.jpg

8.静态文件static走引入EMedia_sdk-2.1.1.de86e59.js文件(可从官网上下载)

9.EMedia_sdk-2.1.1.de86e59.js文件中修改为自己的数据:

13596dad462448f6f2826219032c2c7c149.jpg

10.下面就可以在组件中使用了

<template>
  <div>
    <video ref="test" autoPlay playsInline/>
    <button @click="send">发送</button>
  </div>
</template>

<script>
import emedia from '../../../static/EMedia_sdk-2.1.1.de86e59'

export default {
  data() {
    return {
      $imconn: {},
      $imoption: {},
      localVideo: {},
      videoUrl: {},
      password: ''
    }
  },
  mounted() {
    this.$imconn = this.$imconn
    this.$imoption = this.$imoption
    this.loginWebIM()
  },
  methods: {
    listen() {
      const _this = this
      this.$imconn.listen({
        onClosed: function(message) {}, // 连接关闭回调
        onTextMessage: function(message) {}, // 收到文本消息
        onCmdMessage: function(message) {
          console.log('收到命令消息', message)
          if (message.action === 'ACTIONMONITOR') {
            const confrId = message.ext.ConferenceId
            const password = message.ext.ConferencePWD
            const webIM = emedia.mgr
            emedia.mgr.onStreamAdded = function(member, stream) {
              const video = _this.$refs['test']
              const located = stream.located()
              if (!located) {
                _this.videoUrl = stream
                emedia.mgr.onMediaChanaged(video, function(constaints) {
                  _this.videoUrl = stream
                })
                emedia.mgr.subscribe(member, stream, true, true, video).then((res) => {
                  console.log('订阅成功')
                }).catch((err) => {
                  console.log('订阅失败', err)
                })
              }
            }
            setTimeout(() => {
              console.log('加入会议室')
              emedia.mgr.joinConference(confrId, password, 'user ext field').then(function() {
                console.log('推流')
                emedia.mgr.publish({ audio: true, video: true }, 'user ext field').then((res) => {
                  console.log('推流成功')
                }).catch((err) => {
                  console.log('推流失败', err)
                })
              }).catch(function(e) {
                console.error('失败', e)
              })
            }, 100)
          }
        }, // 收到命令消息
        onAudioMessage: function(message) {}, // 收到音频消息
        onVideoMessage: function(message) {
          var node = document.getElementById('privateVideo')
          var option = {
            url: message.url,
            headers: {
              'Accept': 'audio/mp4'
            },
            onFileDownloadComplete: function(response) {
              var objectURL = WebIM.utils.parseDownloadResponse.call(conn, response)
              node.src = objectURL
            },
            onFileDownloadError: function() {
              console.log('File down load error.')
            }
          }
          WebIM.utils.download.call(conn, option)
        }, // 收到视频消息
        onPresence: function(message) {}, // 处理“广播”或“发布-订阅”消息,如联系人订阅请求、处理群组、聊天室被踢解散等消息
        onRoster: function(message) {}, // 处理好友申请
        onInviteMessage: function(message) {}, // 处理群组邀请
        onError: function(message) {}, // 失败回调
        onReceivedMessage: function(message) {}, // 收到消息送达服务器回执
        onMutedMessage: function(message) {} // 如果用户在A群组被禁言,在A群发消息会走这个回调并且消息不会传递给群其它成员
      })
    },
    send() {
      var id = this.$imconn.getUniqueId() // 生成本地消息id
      var msg = new WebIM.message('cmd', id) // 创建命令消息
      msg.set({
        msg: 'msg',
        to: '*******', // 接收消息对象
        action: 'ACTIONMONITOR', // 用户自定义,cmd消息必填
        ext: { 'extmsg': 'extends messages' }, // 用户自扩展的消息内容(群聊用法相同)
        success: function(id, serverMsgId) {
        }, // 消息发送成功回调
        fail: function(err) {
          console.log(999, err)
        }
      })
      this.$imconn.send(msg.body)
      this.listen()
    },
    // 登录环信
    loginWebIM() { // 这儿是测试用的账号和密码
      this.$imoption.user = '*****'
      this.$imoption.pwd = '*******'
      this.$imconn.open(this.$imoption)
      this.listen()
    }
  }

}
</script>

 

转载于:https://my.oschina.net/zoujinxia/blog/3030128

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值