• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

vue-cli3.0Typescript项目集成环信WebIM群组聊天

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

项目背景

环信webim 官方没有vue版本的,自己就根据sdk重写了个vue版本的,只实现了基础的 登录 群组功能,其他的可以根据需要参考官方文档,添加相应的功能。

环信webim SDK相关文档:
http://docs-im.easemob.com/im/web/intro/start

先使用vue-cli新建一个typescript项目

集成webim

安装环信sdk和strophe.js 这两个是必须的

cnpm install easemob-websdk --save
cnpm install strophe.js --save
cnpm install crypto-js --save
cnpm install underscore --save

  • 打开node_modules/easemob-websdk/src/connection.js

添加组件
在components中添加 WebIM.ts

// 引入Strophe方法
var Strophejs = require('strophe.js');
var Strophe = (Strophejs as any).Strophe;

// 给Strophe.Connection 添加方法
Strophe.Connection.prototype.setJid = (jid: any) => {
    Strophe.Connection.jid = jid;
    Strophe.Connection.authzid = Strophe.getBareJidFromJid(Strophe.Connection.jid);
    Strophe.Connection.authcid = Strophe.getNodeFromJid(Strophe.Connection.jid);
}

Strophe.Connection.prototype.getJid = () => {
    return Strophe.Connection.jid;
}

(window as any).Strophe = Strophe;

var WebIM = require('easemob-websdk') as any;

//配置文件参考环信demo
var config = {
    xmppURL: 'im-api.easemob.com',
    apiURL: (location.protocol === 'https:' ? 'https:' : 'http:') + '//a1.easemob.com',
    appkey: 'easemob-demo#chatdemoui',
    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'
    }
}

//初始化配置
WebIM.config = config

var conn: any = () => {
    return new WebIM.connection({
        isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
        https: typeof WebIM.config.https === 'boolean' ? WebIM.config.https : location.protocol === 'https:',
        url: WebIM.config.xmppURL,
        isAutoLogin: true,
        heartBeatWait: WebIM.config.heartBeatWait,
        autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
        autoReconnectInterval: WebIM.config.autoReconnectInterval,
        apiUrl: WebIM.config.apiURL
    });
}

export interface CurrentUserInfo {
    access_token: string,
    expires_in: number,
    user: {
        activated: string,
        nickname: string,
        type: string,
        username: string,
        uuid: string
    }
}

export default {
    conn,
    WebIM
};

运行效果

git仓库地址:
https://github.com/sandsli/huanxin-vue


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap