vue cli4接入环信webIM
生活随笔
收集整理的這篇文章主要介紹了
vue cli4接入环信webIM
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
vue cli4接入環信webIM
1.安裝
npm install easemob-websdk --save2.配置文件WebIMConfig.js
3.使用
使用組件中引入 import config from '@/utils/webIMConfig' import websdk from 'easemob-websdk' mounted() {this.getLiveInfo()},destroyed() {this.WebIMFun('close') // 根據自己的實際情況,關閉},methods: {getInfo() {getInfoApi({id: this.id}).then(res => {if (res.code === 200) {this.WebIMFun('open', res.data.huanxin_room_id, res.data.huanxin_username, res.data.huanxin_password)} else {this.$message.warning(res.msg)}})},WebIMFun(type, huanxin_room_id, huanxin_username, huanxin_password) {// 初始化IM SDKconst _this = thisvar WebIM = {}WebIM = window.WebIM = websdkWebIM.config = configWebIM.conn = new WebIM.connection({appKey: WebIM.config.appkey,isHttpDNS: WebIM.config.isHttpDNS,isMultiLoginSessions: WebIM.config.isMultiLoginSessions,https: WebIM.config.https,url: WebIM.config.socketServer,apiUrl: WebIM.config.restServer,isAutoLogin: WebIM.config.isAutoLogin,autoReconnectNumMax: WebIM.config.autoReconnectNumMax,autoReconnectInterval: WebIM.config.autoReconnectInterval,delivery: WebIM.config.delivery,useOwnUploadFun: WebIM.config.useOwnUploadFun})if (type == 'close') {WebIM.conn.close()return false}// 鏈接環信WebIM.conn.open({user: huanxin_username,pwd: huanxin_password,appKey: WebIM.config.appkey})WebIM.conn.listen({onOpened: function() {console.log('成功登錄_用戶名:' + huanxin_username)console.log('成功登錄_密碼:' + huanxin_password)console.log('成功登錄_聊天室ID:' + huanxin_room_id)// WebIM.conn.setPresence()WebIM.conn.joinChatRoom({roomId: huanxin_room_id // 聊天室Id}).then(() => {console.log('加入聊天室成功')})},onClosed: function() {// 處理登出事件console.log('退出成功')},onPresence: function(message) {console.log(message)},onTextMessage: function(message) { // 接收消息//獲取到信息做處理//寫自己的邏輯},onError: function(e) {// 異常捕獲console.log('error:' + JSON.stringify(e))}})},}總結
以上是生活随笔為你收集整理的vue cli4接入环信webIM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谈谈企业的持续交付流水线设计
- 下一篇: 改进飞碟游戏