如何处理 XMPP XEP-0363:客户端使用 SMACK 库的 HTTP 文件上传功能?
Posted
技术标签:
【中文标题】如何处理 XMPP XEP-0363:客户端使用 SMACK 库的 HTTP 文件上传功能?【英文标题】:How to handle XMPP XEP-0363: HTTP File Upload feature at client side using SMACK library? 【发布时间】:2016-01-26 04:40:37 【问题描述】:SMACK 是否有用于处理“XEP-0363:HTTP 文件上传”功能的客户端界面? 如果没有,是否有任何其他相同的标准客户端接口?
我可以使用自定义 IQ 来做到这一点,
IQ iq = new IQ("request", "urn:xmpp:http:upload")
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml)
xml.rightAngleBracket();
xml.element("filename", "text.png");
xml.element("size", "1645");
return xml;
iq.setType(IQ.Type.get);
iq.setFrom("testa@localhost/Smack");
iq.setTo("upload.localhost");
还有,
connection.sendStanza(iq);
【问题讨论】:
【参考方案1】:我使用简单的类来实现这个功能:
package mypkg;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.provider.IntrospectionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
public class XEP0363 implements StanzaListener
private XMPPTCPConnection connection;
private String id;
private SlotGrantedListener listener;
static
ProviderManager.addIQProvider("slot", "urn:xmpp:http:upload", new SlotIQProvider());
public static class SlotIQProvider extends IntrospectionProvider.IQIntrospectionProvider
public SlotIQProvider()
super(SlotIQ.class);
public static class SlotIQ extends IQ
private String put;
private String get;
public SlotIQ()
super("request", "urn:xmpp:http:upload");
public String getPut()
return put;
public void setPut(String put)
this.put = put;
public String getGet()
return get;
public void setGet(String get)
this.get = get;
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml)
return null;
public interface SlotGrantedListener
void slotGranted(String put, String get);
void slotDenied(String error);
public XEP0363(XMPPTCPConnection connection)
this.connection = connection;
public void requestSlot(String to, final String fileName, final long fileSize, SlotGrantedListener listener)
throws SmackException.NotConnectedException
this.listener = listener;
final IQ iq = new IQ("request", "urn:xmpp:http:upload")
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml)
xml.rightAngleBracket();
xml.element("filename", fileName);
xml.element("size", String.valueOf(fileSize));
return xml;
;
iq.setType(IQ.Type.get);
iq.setFrom(connection.getUser());
iq.setTo(to);
id = iq.getStanzaId();
connection.addAsyncStanzaListener(this, new StanzaFilter()
@Override
public boolean accept(Stanza stanza)
return id != null && id.equals(stanza.getStanzaId());
);
connection.sendStanza(iq);
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException
connection.removeAsyncStanzaListener(this);
if (((IQ)packet).getType() != IQ.Type.error)
SlotIQ slotIQ = (SlotIQ) packet;
listener.slotGranted(slotIQ.getPut(), slotIQ.getGet());
else
XMPPError error = packet.getError();
String errDesc = error.getDescriptiveText(null);
String errText = errDesc != null ? String.format("%s (%s)", errDesc, error.toString()) : error.toString();
listener.slotDenied(errText);
然后,创建实现SlotGrantedListener
的监听器并调用requestSlot()
:
XEP0363 fileUpload = new XEP0363(connection);
fileUpload.requestSlot("upload.myxmppdomain", "mypic.jpg", 12345, listener)
注意,这个类不是线程安全的。
【讨论】:
你能提供这个代码的基本概念吗?我是ios开发者所以请解释一下任何东西以上是关于如何处理 XMPP XEP-0363:客户端使用 SMACK 库的 HTTP 文件上传功能?的主要内容,如果未能解决你的问题,请参考以下文章