- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我要接收的消息包。
<message to="39@domainname.com/smack" chat_id="73392"
custom_packet="true" user_id="44" manager_id="39" time_stamp="0" website_id="0"
visitor_name="John" end_time="False" xml:lang="en-us" type="groupchat"
from="room73392@conference.domainname.com/39">
<body>Hello</body>
<x xmlns="http://jabber.org/protocol/muc#user">
<status xmlns="" code="0"/>
</x></message>
我收到的消息包。
<message to="44@domainname.com/Smack"
from="room73407@conference.domainname.com/Visitor1171" type="groupchat">
<body>Hello</body>
<delay xmlns="urn:xmpp:delay"></delay>
<x xmlns="jabber:x:delay" stamp="20120917T05:57:19"
from="4732abb5@domainname.com/4732abb5">
</x></message>
我有一个 smack 的源代码,这是数据包类。谁能帮我制作我的定制包。任何帮助修改源代码表示赞赏。
代码:
public abstract class Packet {
protected static final String DEFAULT_LANGUAGE =
java.util.Locale.getDefault().getLanguage().toLowerCase();
private static String DEFAULT_XML_NS = null;
/**
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
* has no id set this constant as the packet's id. When the packet is asked for its id the
* answer will be <tt>null</tt>.
*/
public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE";
/**
* Date format as defined in XEP-0082 - XMPP Date and Time Profiles.
* The time zone is set to UTC.
* <p>
* Date formats are not synchronized. Since multiple threads access the format concurrently,
* it must be synchronized externally.
*/
public static final DateFormat XEP_0082_UTC_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
static {
XEP_0082_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(5) + "-";
/**
* Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID.
*/
private static long id = 0;
private String xmlns = DEFAULT_XML_NS;
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
public static synchronized String nextID() {
return prefix + Long.toString(id++);
}
public static void setDefaultXmlns(String defaultXmlns) {
DEFAULT_XML_NS = defaultXmlns;
}
private String packetID = null;
private String to = null;
private String from = null;
private final List<PacketExtension> packetExtensions
= new CopyOnWriteArrayList<PacketExtension>();
private final Map<String,Object> properties = new HashMap<String, Object>();
private XMPPError error = null;
/**
* Returns the unique ID of the packet. The returned value could be <tt>null</tt> when
* ID_NOT_AVAILABLE was set as the packet's id.
*
* @return the packet's unique ID or <tt>null</tt> if the packet's id is not available.
*/
public String getPacketID() {
if (ID_NOT_AVAILABLE.equals(packetID)) {
return null;
}
if (packetID == null) {
packetID = nextID();
}
return packetID;
}
/**
* Sets the unique ID of the packet. To indicate that a packet has no id
* pass the constant ID_NOT_AVAILABLE as the packet's id value.
*
* @param packetID the unique ID for the packet.
*/
public void setPacketID(String packetID) {
this.packetID = packetID;
}
/**
* Returns who the packet is being sent "to", or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "to"
* attribute optional, so it does not always need to be set.<p>
*
* The StringUtils class provides several useful methods for dealing with
* XMPP addresses such as parsing the
* {@link StringUtils#parseBareAddress(String) bare address},
* {@link StringUtils#parseName(String) user name},
* {@link StringUtils#parseServer(String) server}, and
* {@link StringUtils#parseResource(String) resource}.
*
* @return who the packet is being sent to, or <tt>null</tt> if the
* value has not been set.
*/
public String getTo() {
return to;
}
/**
* Sets who the packet is being sent "to". The XMPP protocol often makes
* the "to" attribute optional, so it does not always need to be set.
*
* @param to who the packet is being sent to.
*/
public void setTo(String to) {
this.to = to;
}
/**
* Returns who the packet is being sent "from" or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "from"
* attribute optional, so it does not always need to be set.<p>
*
* The StringUtils class provides several useful methods for dealing with
* XMPP addresses such as parsing the
* {@link StringUtils#parseBareAddress(String) bare address},
* {@link StringUtils#parseName(String) user name},
* {@link StringUtils#parseServer(String) server}, and
* {@link StringUtils#parseResource(String) resource}.
*
* @return who the packet is being sent from, or <tt>null</tt> if the
* value has not been set.
*/
public String getFrom() {
return from;
}
/**
* Sets who the packet is being sent "from". The XMPP protocol often
* makes the "from" attribute optional, so it does not always need to
* be set.
*
* @param from who the packet is being sent to.
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Returns the error associated with this packet, or <tt>null</tt> if there are
* no errors.
*
* @return the error sub-packet or <tt>null</tt> if there isn't an error.
*/
public XMPPError getError() {
return error;
}
/**
* Sets the error for this packet.
*
* @param error the error to associate with this packet.
*/
public void setError(XMPPError error) {
this.error = error;
}
/**
* Returns an unmodifiable collection of the packet extensions attached to the packet.
*
* @return the packet extensions.
*/
public synchronized Collection<PacketExtension> getExtensions() {
if (packetExtensions == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(new ArrayList<PacketExtension>(packetExtensions));
}
/**
* Returns the first extension of this packet that has the given namespace.
*
* @param namespace the namespace of the extension that is desired.
* @return the packet extension with the given namespace.
*/
public PacketExtension getExtension(String namespace) {
return getExtension(null, namespace);
}
/**
* Returns the first packet extension that matches the specified element name and
* namespace, or <tt>null</tt> if it doesn't exist. If the provided elementName is null
* than only the provided namespace is attempted to be matched. Packet extensions are
* are arbitrary XML sub-documents in standard XMPP packets. By default, a
* DefaultPacketExtension instance will be returned for each extension. However,
* PacketExtensionProvider instances can be registered with the
* {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager}
* class to handle custom parsing. In that case, the type of the Object
* will be determined by the provider.
*
* @param elementName the XML element name of the packet extension. (May be null)
* @param namespace the XML element namespace of the packet extension.
* @return the extension, or <tt>null</tt> if it doesn't exist.
*/
public PacketExtension getExtension(String elementName, String namespace) {
if (namespace == null) {
return null;
}
for (PacketExtension ext : packetExtensions) {
if ((elementName == null || elementName.equals(ext.getElementName()))
&& namespace.equals(ext.getNamespace()))
{
return ext;
}
}
return null;
}
/**
* Adds a packet extension to the packet.
*
* @param extension a packet extension.
*/
public void addExtension(PacketExtension extension) {
packetExtensions.add(extension);
}
/**
* Removes a packet extension from the packet.
*
* @param extension the packet extension to remove.
*/
public void removeExtension(PacketExtension extension) {
packetExtensions.remove(extension);
}
/**
* Returns the packet property with the specified name or <tt>null</tt> if the
* property doesn't exist. Property values that were orginally primitives will
* be returned as their object equivalent. For example, an int property will be
* returned as an Integer, a double as a Double, etc.
*
* @param name the name of the property.
* @return the property, or <tt>null</tt> if the property doesn't exist.
*/
public synchronized Object getProperty(String name) {
if (properties == null) {
return null;
}
return properties.get(name);
}
/**
* Sets a property with an Object as the value. The value must be Serializable
* or an IllegalArgumentException will be thrown.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public synchronized void setProperty(String name, Object value) {
if (!(value instanceof Serializable)) {
throw new IllegalArgumentException("Value must be serialiazble");
}
properties.put(name, value);
}
/**
* Deletes a property.
*
* @param name the name of the property to delete.
*/
public synchronized void deleteProperty(String name) {
if (properties == null) {
return;
}
properties.remove(name);
}
/**
* Returns an unmodifiable collection of all the property names that are set.
*
* @return all property names.
*/
public synchronized Collection<String> getPropertyNames() {
if (properties == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(new HashSet<String>(properties.keySet()));
}
/**
* Returns the packet as XML. Every concrete extension of Packet must implement
* this method. In addition to writing out packet-specific data, every sub-class
* should also write out the error and the extensions data if they are defined.
*
* @return the XML format of the packet as a String.
*/
public abstract String toXML();
/**
* Returns the extension sub-packets (including properties data) as an XML
* String, or the Empty String if there are no packet extensions.
*
* @return the extension sub-packets as XML or the Empty String if there
* are no packet extensions.
*/
protected synchronized String getExtensionsXML() {
StringBuilder buf = new StringBuilder();
// Add in all standard extension sub-packets.
for (PacketExtension extension : getExtensions()) {
buf.append(extension.toXML());
}
// Add in packet properties.
if (properties != null && !properties.isEmpty()) {
buf.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
// Loop through all properties and write them out.
for (String name : getPropertyNames()) {
Object value = getProperty(name);
buf.append("<property>");
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
buf.append("<value type=\"");
if (value instanceof Integer) {
buf.append("integer\">").append(value).append("</value>");
}
else if (value instanceof Long) {
buf.append("long\">").append(value).append("</value>");
}
else if (value instanceof Float) {
buf.append("float\">").append(value).append("</value>");
}
else if (value instanceof Double) {
buf.append("double\">").append(value).append("</value>");
}
else if (value instanceof Boolean) {
buf.append("boolean\">").append(value).append("</value>");
}
else if (value instanceof String) {
buf.append("string\">");
buf.append(StringUtils.escapeForXML((String)value));
buf.append("</value>");
}
// Otherwise, it's a generic Serializable object. Serialized objects are in
// a binary format, which won't work well inside of XML. Therefore, we base-64
// encode the binary data before adding it.
else {
ByteArrayOutputStream byteStream = null;
ObjectOutputStream out = null;
try {
byteStream = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
buf.append("java-object\">");
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append(encodedVal).append("</value>");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (out != null) {
try {
out.close();
}
catch (Exception e) {
// Ignore.
}
}
if (byteStream != null) {
try {
byteStream.close();
}
catch (Exception e) {
// Ignore.
}
}
}
}
buf.append("</property>");
}
buf.append("</properties>");
}
return buf.toString();
}
public String getXmlns() {
return this.xmlns;
}
public static String getDefaultLanguage() {
return DEFAULT_LANGUAGE;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Packet packet = (Packet) o;
if (error != null ? !error.equals(packet.error) : packet.error != null) { return false; }
if (from != null ? !from.equals(packet.from) : packet.from != null) { return false; }
if (!packetExtensions.equals(packet.packetExtensions)) { return false; }
if (packetID != null ? !packetID.equals(packet.packetID) : packet.packetID != null) {
return false;
}
if (properties != null ? !properties.equals(packet.properties)
: packet.properties != null) {
return false;
}
if (to != null ? !to.equals(packet.to) : packet.to != null) { return false; }
return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
}
@Override
public int hashCode() {
int result;
result = (xmlns != null ? xmlns.hashCode() : 0);
result = 31 * result + (packetID != null ? packetID.hashCode() : 0);
result = 31 * result + (to != null ? to.hashCode() : 0);
result = 31 * result + (from != null ? from.hashCode() : 0);
result = 31 * result + packetExtensions.hashCode();
result = 31 * result + properties.hashCode();
result = 31 * result + (error != null ? error.hashCode() : 0);
return result;
}
}
最佳答案
尽管我认为这是错误的方法,正如我在您的其他问题中所说的那样。这些是您尝试执行的操作的一些指南。
不需要修改Packet,只需要扩展Message,放入自己的自定义属性即可。这将完成发送所需的操作。要接收,您必须修改 PacketParserUtil.parseMessage()能够确定您的自定义消息或标准消息是否正在发送,并根据需要构建和填充。我不知道您使用的服务器是否存在此问题,因为它不是标准的,但它可能会工作。
另一个更简单的选择是通过 Packet.setProperty() 将您的属性作为属性简单地添加到标准消息中当然还有通过 getProperty() 读取。这将创建和扩展数据包,但不需要自定义工作,因为 Smack 已经读取/写入扩展。
关于java - 使用 asmack for android 在 XMPP 数据包中的消息标记中添加自定义属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12475122/
在 Smack API 中,有一个用于连接的配置类,在此页面中描述 ConnectionConfiguration 我对服务名称和服务器名称之间的区别感到困惑。 假设我有一台名为“mybox.mydo
我想阻止常见的 xmpp 客户端(例如 Pidgin、PSI+...)登录我们的 xmpp 服务器(ejabberd)。我怎样才能做到这一点? 提前致谢。 最佳答案 我认为这不可能真正做到。当然,您可
我想阻止常见的 xmpp 客户端(例如 Pidgin、PSI+...)登录我们的 xmpp 服务器(ejabberd)。我怎样才能做到这一点? 提前致谢。 最佳答案 我不认为这真的可以做到。当然,您可
我正在尝试使用 XEP-0048 - 书签 (http://xmpp.org/extensions/xep-0048.html) 自动加入房间。 我正在使用 RobbieHanson XMPPFram
我正在使用 XMPP(通过 ejabberd)。据此XEP standard ,我发现XMPP服务器可以存储离线消息,并且可以在离线用户上线时传递它们(分享他的存在)。 我的问题是: 1> 这些离线消
我试图通过用低级 Python 编码来学习 XMPP 规范( RFC 3920 )。但是我在 6.5 的第 4 步被挂断了一个多小时。 ,选择身份验证机制。我发送: ,并得到:而不是 base64 编
我搜索过但没有找到 XMPP 使用哪些端口。我需要实现XMPP服务器和客户端并使用XML传输、文件传输和流媒体。他们使用不同的端口吗?有没有办法让它们都一样使用,这样我就不需要打扰网络管理员?谢谢 最
我正在编写一个通过 XMPP 与客户端通信的应用程序。我希望能够使用用户现有的 xmpp 帐户(他们都有 google ID),但我不希望我的消息出现在他们的常规 IM 流中。 我在想,当我的客户端与
如果他们有 jabber 帐户,我想高效地检查所有本地地址簿联系人。用户使用手机号在XMPP服务器上注册。我目前将以下 XEP-0055 节发送到 ejabberd 服务器并评估结果。
是否可以将消息传递到服务器时包含到 XMPP 消息时间? 现在我从 OpenFire 服务器收到如下消息: test 但我需要知道消息何时发送(传递到服务器),例如: test2012-10-12 1
我想知道 xmpp 发布-订阅功能是同时向所有订阅者发送通知(广播)还是在队列中发送通知,即一个一个地发送通知。 我想知道我正在使用的 pub-sub 和 jaxl 类之间的区别,因为 jaxl 在队
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
XMPP 允许用户使用同一个帐户同时从多个客户端连接到服务器。我构建了一个执行此操作的应用程序,但如果启用了桌面客户端,我不希望用户能够使用移动客户端进行连接。这是一个游戏,连接到这两个会导致问题。
我正在为我的 XMPP 连接使用 libjingle。我可以在没有服务器的情况下连接两个 XMPP 客户端吗?如果是,我该怎么做,如果不是,那为什么不可能呢? XMPP client1 XMPP c
我目前正在研究 XMPP,我想知道是否有办法创建动态 XMPP 花名册。我希望服务器/组件/插件自动生成任何用户的联系人列表。 组件可以访问和修改名册吗? 我知道有些服务器(如 OpenFire)使用
我的网络上有两台机器。我在其中一台机器(机器 A)上安装了 ejabberd,在那里注册了两个用户。我在两台机器上都运行了 Pidgin。我在机器 A 上登录到 Pidgin,并且能够登录。当我尝试在
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
默认情况下,XMPP 状态会发布给所有订阅该人的人。是否可以发送诸如 iq 调用之类的内容来获取不在我的名册中的 ID 的存在标签? 最佳答案 如果您想知道 XMPP 实体是否已连接,可以使用 XMP
我希望使用 xmpp 在我的应用程序中包含两个功能。第一个是几乎完成的一对一聊天(使用 strophe),第二个是实时通知,就像它在 facebook 中的工作方式一样。我试图了解 xmpp 中的 p
引用this question ,XMPP 被认为是 IM 互操作性的开放标准。 对于我的应用程序来说,如果我使用 XMPP 进行内部客户端-服务器通信,或者开发自己的内部协议(protocol)但在
我是一名优秀的程序员,十分优秀!