分享者:mirrorZyb,來自原文地址
官網(wǎng)demo 下載后把整個(gè)utils目錄下的文件復(fù)制到咱自己工程的目錄下。 在WebIMConfig.js中將AppKey替換成自己應(yīng)用的key 界面簡單的用一個(gè)輸入框和一個(gè)按鈕組成,點(diǎn)擊按鈕發(fā)送消息。 導(dǎo)入WebIM.js
var WebIM = require('../../utils/WebIM.js')
var WebIM = WebIM.default
登陸
hxloign: function () {
var options = {
apiUrl: WebIM.config.apiURL,
user: 'u1',
pwd: 'p1',
grant_type: 'password',
appKey: WebIM.config.appkey //應(yīng)用key
}
WebIM.conn.open(options)
},
發(fā)送文本消息
sendMessage: function () {
var that = this
var id = WebIM.conn.getUniqueId();
var msg = new WebIM.message('txt', id);
msg.set({
msg: this.data.inputValue,//輸入框的文本
to: 'u0',
roomType: false,
success: function (id, serverMsgId) {
}
});
msg.body.chatType = 'singleChat';
WebIM.conn.send(msg.body);
},
接收消息接收消息要先在app.js中添加回調(diào)函數(shù) app.js獲取聊天界面
getRoomPage: function () {
return this.getPage("pages/index/index")//聊天界面
},
getPage: function (pageName) {
var pages = getCurrentPages()
return pages.find(function (page) {
return page.__route__ == pageName
})
在app.jsonlanuch方法中進(jìn)行聲明
var that = this;
WebIM.conn.listen({
onTextMessage: function (message) {
var page = that.getRoomPage()
if (message) {
if (page) {
page.receiveMsg(message, 'txt')//receiveMsg方法就是咱在自己界面定義的方法
}else{
//界面不存在
}
}
}
})
index.js中定義的接收消息的方法
receiveMsg: function (msg, type) {
console.log(msg);
},
到此簡單的收發(fā)消息功能就算實(shí)現(xiàn)了,發(fā)送語音圖片規(guī)則都差不多,只不過參數(shù)不一樣,可以參考消息 環(huán)信開發(fā)文檔 |