差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
im:applet:message [2019/11/06 03:09]
zhangdong
im:applet:message [2020/04/28 10:28]
zhangdong [接收消息]
行 35: 行 35:
   ​   ​
   * 附件消息   * 附件消息
 +
 +  * 自定义消息
  
 多样化的消息类型,覆盖多种场景下的消息需求。 多样化的消息类型,覆盖多种场景下的消息需求。
行 342: 行 344:
 <code javascript>​ <code javascript>​
 // 单聊发送文件消息 // 单聊发送文件消息
-var sendPrivateFile = function ​() { +uploadRecord(tempFilePath){ 
-    var id conn.getUniqueId(); ​                  // 生成本地消息id +    var str = WebIM.config.appkey.split("#"​); 
-    var msg = new WebIM.message('​file',​ id);        // 创建文件消息 +    var me this
-    var input document.getElementById('​file'​) // 选择文件的input +    var token = WebIM.conn.context.accessToken 
-    var file = WebIM.utils.getFileUrl(input); ​     // 将文件转化为二进制文件 +    ​wx.uploadFile(
-    ​var allowType = +        ​url: "https://​a1.easemob.com/"​ + str[0] + "/"​ + str[1] + "/​chatfiles"​
-        ​'​jpg'​true+        ​filePathtempFilePath
-        ​'​gif'​true+        ​name"​file"​
-        ​'​png'​true+        ​header{ 
-        ​'​bmp'​true, +            "​Content-Type"​"​multipart/​form-data"​
-        '​zip'​true+            ​Authorization"​Bearer " + token 
-        '​txt'​true, +        ​}
-        ​'​doc'​:true+        ​success(res){ 
-        ​'​pdf':​ true +            var id = WebIM.conn.getUniqueId()
-    }+            var msg = new WebIM.message(msgType.AUDIO, id); 
-    ​if ​(file.filetype.toLowerCase(in allowType) { +            var dataObj ​JSON.parse(res.data);​ 
-        var option ​{ +            ​msg.set({ 
-            apiUrl: WebIM.config.apiURL,​ +                ​apiUrl: WebIM.config.apiURL,​ 
-            filefile+                ​accessTokentoken
-            to'​username'​                      /接收消息对象 +                body
-            ​roomTypefalse+                    type: msgType.FILE, 
-            ext{},                              // 扩展消息 +                    url: dataObj.uri + "/" + dataObj.entities[0].uuid,​ 
-            ​onFileUploadErrorfunction () {      // 消息上传失败 +                    ​filetype""​
-                ​console.log('​onFileUploadError'​); +                    ​filenametempFilePath,​ 
-            }+                    accessToken:​ token, 
-            ​onFileUploadComplete: function () {   // 消息上传成功 +                    length: ""​ 
-                ​console.log('onFileUploadComplete'); +                ​}, 
-            }, +                fromme.data.username.myName,​ 
-            ​success:​ function ​() {                // 消息发送成功 +                ​to: me.getSendToParam(), 
-                ​console.log('​Success'​); +                ​roomType:​ false
-            }, +                ​chatType:​ me.data.chatType,​ 
-            ​flashUpload:​ WebIM.flashUpload +                success: function (argument) { 
-        }+                    disp.fire('em.chat.sendSuccess', id); 
-        msg.set(option);​ +                
-        ​conn.send(msg.body);​ +           }); 
-    }+           ​if(me.isGroupChat()){ 
 +               msg.setGroup("​groupchat"​); 
 +           ​
 +           msg.body.length = ""​
 +           WebIM.conn.send(msg.body);​ 
 +       ​} 
 +   });
 }; };
 </​code>​ </​code>​
行 430: 行 438:
 </​code>​ </​code>​
  
-单聊发送视频消息示例如下:+ 
 +=== API === 
 +示例中使用到的 API 
 +  * [[http://​webim-h5.easemob.com/​jsdoc/​out/​connection.html#​getUniqueId|getUniqueId]] 
 +   
 +  * [[http://​webim-h5.easemob.com/​jsdoc/​out/​connection.html#​send|send]] 
 + 
 +---- 
 + 
 +==== 发送自定义消息 ==== 
 +单聊发送自定义消息示例如下:
 <code javascript>​ <code javascript>​
-// 单聊发送视频消息 +var sendCustomMsg ​= function () { 
-var sendPrivateVideo ​= function () { +    var id = conn.getUniqueId(); ​                ​// 生成本地消息id 
-    var id = conn.getUniqueId(); ​                  ​// 生成本地消息id +    var msg = new WebIM.message('​custom', id);   ​// 创建自定义消息 
-    var msg = new WebIM.message('​video', id);      // 创建视频消息 +    var customEvent ​"​customEvent"​            ​// 创建自定义事件 
-    var input document.getElementById('​video'​) // 选择视频的input +    var params ​{}                            ​// 消息内容 
-    var file WebIM.utils.getFileUrl(input)     // 将视频转化为二进制文件 +    var customExts ​= {};                         // 消息扩展 
-    var allowType ​= { +    ​msg.set({ 
-        '​mp4':​ true, +        to: '​username', ​                         // 接收消息对象(用户id) 
-        '​wmv':​ true, +        ​customEvent
-        '​avi':​ true, +        ​customExts
-        '​rmvb':​true,​ +        ​params,​ 
-        '​mkv':​true +        roomTypefalse
-    ​}; +        ​success: function (id, serverMsgId) {}, 
-    ​if (file.filetype.toLowerCase() in allowType) ​+        fail: function(e){} 
-        ​var option = { +    }); 
-            apiUrl: WebIM.config.apiURL,​ +    conn.send(msg.body);​
-            file: file, +
-            ​to: '​username', ​                      ​// 接收消息对象 +
-            ​roomType:​ false+
-            ext: {}                             // 扩展消息 +
-            ​onFileUploadErrorfunction () {      // 消息上传失败 +
-                console.log('​onFileUploadError'​);​ +
-            }+
-            ​onFileUploadComplete: function () {   // 消息上传成功 +
-                console.log('​onFileUploadComplete'​);​ +
-            ​}, +
-            ​success: function () {                // 消息发送成功 +
-                console.log('​Success'​);​ +
-            ​}, +
-            ​flashUpload:​ WebIM.flashUpload +
-        ​}+
-        msg.set(option); +
-        conn.send(msg.body);​ +
-    }+
 }; };
 </​code>​ </​code>​
- 
 === API === === API ===
 示例中使用到的 API 示例中使用到的 API
行 475: 行 474:
   * [[http://​webim-h5.easemob.com/​jsdoc/​out/​connection.html#​send|send]]   * [[http://​webim-h5.easemob.com/​jsdoc/​out/​connection.html#​send|send]]
  
 +----
 +
 +===== 消息撤回 =====
 +SDK增值服务。
 +<code javascript>​
 +/**
 + * 发送撤回消息
 +  * @param {Object} option - 
 + * @param {Object} option.mid -   ​回撤消息id
 + * @param {Object} option.to -   ​消息的接收方
 + * @param {Object} option.type -  chat(单聊) groupchat(群组) chatroom(聊天室)
 + */
 +WebIM.conn.recallMessage(option)
 +</​code>​
 ---- ----
 ===== 接收消息 ===== ===== 接收消息 =====
行 490: 行 503:
     onLocationMessage:​ function ( message ) {},//​收到位置消息     onLocationMessage:​ function ( message ) {},//​收到位置消息
     onFileMessage:​ function ( message ) {},    //​收到文件消息     onFileMessage:​ function ( message ) {},    //​收到文件消息
 +    onCustomMessage:​ function ( message ) {},  //​收到自定义消息
     onVideoMessage:​ function (message) {     onVideoMessage:​ function (message) {
         var node = document.getElementById('​privateVideo'​);​         var node = document.getElementById('​privateVideo'​);​
行 524: 行 538:
 }); });
 </​code>​ </​code>​
- 
-**注意:** 
-  * 如果 ''​isAutoLogin''​ 设置为 false ,那么必须手动设置上线,否则无法收消息。手动上线指的是调用 ''​conn.setPresence()''​ ; 
-  ​ 
-  * 如果 conn 初始化时已将 ''​isAutoLogin''​ 设置为 true ,则无需调用 ''​conn.setPresence()''​。 
  
 === API === === API ===