======消息集成====== 消息是客户端集成中最重要的功能之一,webim消息的使用方法主要发送消息、接受消息、处理消息、历史消息、新消息提醒、消息回执等6个主要的类型。通过对消息的集成,您可以最快速的集成体验 IM 收发消息的流畅体验。 ---- ======发送消息====== [[https://webim.easemob.com/sdk/jsdoc/out/connection.html#send|查看 [send]方法使用详情]]。 =====消息id以及消息类型===== 常用消息包含文字、语音、图片、多媒体文件、文件附件、透传、扩展消息等。每一种消息类型需要填写对应的内容设置消息id以及消息类型如下。 var sendPrivateText = function () { var id = conn.getUniqueId(); var msg = new WebIM.message('txt', id); }; ^参数^说明^ |id|消息id,使用到的方法是getUniqueId[[https://webim.easemob.com/sdk/jsdoc/out/connection.html#getUniqueId|查看 [getUniqueId]方法使用详情]]。| |msg|消息的类型,txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息。| =====单聊和群聊===== webim sdk有单聊和群聊的概念,收件人是单个用户,需要定义为单聊; msg.body.chatType = 'singleChat'; 收件人是群组或者聊天室,需要定义为群聊。 msg.body.chatType = 'groupChat'; =====集成代码示例===== 因为不同的消息类型需要填写的内容不同,后面会对每种类型进行详细说明。 ====文本消息==== 代码示例 var sendPrivateText = function () { var id = conn.getUniqueId(); var msg = new WebIM.message('txt', id); var option = { msg: 'message content', to: 'username', roomType: false, success: function (id, serverMsgId) { console.log('send private text Success'); }, fail: function(e){ console.log("Send private text error"); } }); msg.set(option); msg.body.chatType = 'singleChat'; conn.send(msg.body); }; ^参数^说明^ |msg|消息的文本内容| |to|填写用户名,群组id,聊天室id| |roomType|??? | |success|对成功的相关定义,sdk会将消息id登记到日志进行备份处理| |fail|对失败的相关定义,sdk会将消息id登记到日志进行备份处理| ==== 图片消息 ==== 代码示例 document.addEventListener('paste', function (e) { if (e.clipboardData && e.clipboardData.types) { if (e.clipboardData.items.length > 0) { if (/^image\/\w+$/.test(e.clipboardData.items[0].type)) { var blob = e.clipboardData.items[0].getAsFile(); var url = window.URL.createObjectURL(blob); var id = conn.getUniqueId(); var msg = new WebIM.message('img', id); var option = { apiUrl: WebIM.config.apiURL, file: {data: blob, url: url}, to: 'username', roomType: false, onFileUploadError: function (error) { console.log('Error'); }, onFileUploadComplete: function (data) { console.log('Complete'); }, success: function (id) { console.log('Success'); } }); msg.set(option); msg.body.chatType = 'singleChat'; conn.send(msg.body); } } } }); ^参数^说明^ |apiUrl|消息的文本内容| |file|???| |to|填写用户名,群组id,聊天室id| |roomType|??? | |onFileUploadError|对图片上传失败的相关定义,sdk会进行日志错误备份处理| |onFileUploadComplete|对图片上传完成的相关定义,sdk会进行日志完成备份处理| |success|对成功的相关定义,sdk会将消息id登记到日志进行备份处理| ----