private void SendImage() { if (conn.ConnectionState != agsXMPP.XmppConnectionState.Connected && conn.ConnectionState != agsXMPP.XmppConnectionState.SessionStarted) { MessageBox.Show("请连接后再上传文件"); return; } if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } // Path : /{org_name}/{app_name}/chatfiles string path = conn.GetUrl("chatfiles"); PostedFileResp resp = PostedFileManager.PostFile(path, conn.TokenData.access_token, openFileDialog1.FileName); string title = BuildMsgTitle(conn.UserName); //edChat.AppendHtml(title, Color.Blue); //edChat.AppendNewLine(); byte[] bytes = SdkUtils.DownloadThumbnail(resp.uri + "/" + resp.entities[0].uuid, resp.entities[0].share_secret, conn.TokenData.access_token); //edChat.AppendImageBytes(bytes); //edChat.AppendNewLine(2); conn.SendFile(toUserName, resp); }
private void SendAudio(string audioFileName) { string path = conn.GetUrl("chatfiles"); PostedFileResp resp = PostedFileManager.PostFile(path, conn.TokenData.access_token, audioFileName); string title = BuildMsgTitle(conn.UserName); //edChat.AppendHtml(title, Color.Blue); //edChat.AppendNewLine(); byte[] bytes = SdkUtils.DownloadThumbnail(resp.uri + "/" + resp.entities[0].uuid, resp.entities[0].share_secret, conn.TokenData.access_token); AppendAudioTag("我的语音"); conn.SendFile(toUserName, resp); }
/// <summary> /// 发送文件 /// </summary> /// <param name="toUser">发给谁</param> /// <param name="postedfile">已经上传的文件返回信息</param> public void SendFile(string toUser, PostedFileResp postedfile) { Jid to = new Jid(BuildJid(toUser)); Jid from = new Jid(BuildJid(UserName)); BodyBase[] bodies = new BodyBase[postedfile.entities.Length]; // 构建发送文件的 message 消息 for (int i = 0; i < postedfile.entities.Length; i++) { PostFileEntity entity = postedfile.entities[i]; // 文件类型 img audio string otype = SdkUtils.GetFileType(entity.filename); // 文件的url string ourl = postedfile.uri + "/" + entity.uuid; string osecret = entity.share_secret; string ofilename = entity.filename; /* * 传图片 * ReceivedData: * <message xmlns='jabber:client' from='easemob-demo#[email protected]/webim' * to='easemob-demo#[email protected]' id='124420481838219668' type='chat'> * <body> * {"from":"weigang75","to":"march3","bodies": * [{"type":"img","url":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/cd6f8050-81f7-11e5-a16a-05187e341cb0", * "secret":"zW-AWoH3EeWmJevV5n4Fpkxnnu3e5okMLIhENE0QHaZbvqg5", * "filename":"原生+自定义.jpg", * "thumb":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/cd6f8050-81f7-11e5-a16a-05187e341cb0", * "thumb_secret":"","size":{"width":952,"height":671}}],"ext":{}}</body></message> * * 传语音 * ReceivedData: * <message xmlns='jabber:client' from='easemob-demo#[email protected]/webim' * to='easemob-demo#[email protected]' id='124421298246910356' type='chat'> * <body> * {"from":"weigang75","to":"march3","bodies": * [{"type":"audio", * "url":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/3ec2bb50-81f8-11e5-8e7c-1fa6315dec2d", * "secret":"PsK7WoH4EeWwmIkeyVsexnkK-Rmqu2X_N2qqK9FQYmUkko8W", * "filename":"环信通讯 - 副本.mp3", * "length":3}],"ext":{}}</body></message> * */ // 如果是文件,需要多一些字段 thumb、thumb_secret、size if ("img".Equals(otype)) { bodies[i] = new ImageBody { type = otype, url = ourl, secret = osecret, filename = ofilename, thumb = ourl, thumb_secret = "", size = new ImageSize { width = entity.imageSize.Width, height = entity.imageSize.Height } }; } else if ("audio".Equals(otype)) { bodies[i] = new AudioBody { type = otype, url = ourl, secret = osecret, filename = ofilename }; } else { bodies[i] = new FileBody { type = otype, url = ourl, secret = osecret, filename = ofilename }; } } MsgData data = new MsgData() { from = UserName, to = toUser, bodies = bodies, ext = new { } }; WSMessage msgNode = new WSMessage(to, from); msgNode.Type = MessageType.chat; msgNode.SetBodyData(data); Send(msgNode); }