- 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/
使用 Python 的 rtmplib 绑定(bind)并遇到一些问题。 首先, 我有这样的东西: import librtmp conn = librtmp.RTMP(...) conn.con
基本上,我是在查看 Motorstorm 排行榜时在 PS3 上窃听数据包。排行榜以 XML 格式发送到我的 ps3,但只有在我获得授权后。那么有人可以告诉我这三个数据包之间发生了什么,以及我如何在浏
我正在努力了解 TCP,但解析大量 RFC 并没有帮助。我相信我了解连接和关闭握手,但我似乎无法找到任何总结实际数据流的内容。 在连接和关闭握手之间 TCP 数据包看起来像什么? (特别是标题) 最佳
我正在尝试通过 RCON 端口与我的 Minecraft 服务器通信。 虽然我不知道如何使用套接字和流的东西。四处寻找,我发现他们都有一些共同点。套接字、输入流和输出流。 我在我的代码中试过了,但返回
我正在 UDP 之上设计一个简单的协议(protocol),现在我意识到其他人可以将数据包发送到我正在监听的端口。这样的数据包对于我的应用程序来说显然是不正确的(我现在不担心安全问题) 是否有过滤这些
我目前有一个具有可自定义滴答率的游戏服务器,但在本示例中,我们建议服务器每秒仅滴答一次或 1hz。我想知道如果客户端发送速率比服务器快,因为我当前的设置似乎不起作用,那么处理传入数据包的最佳方法是什么
我无法理解网络字节顺序以及通过 UDP 发送和接收数据的顺序。我正在使用 C#。 我有一个结构保持: message.start_id = 0x7777CCCC; message.me
我正在为 USB 设备编写代码。假设 USB 主机开始控制读取传输以从设备读取一些数据,并且请求的数据量(设置数据包中的 wLength)是端点 0 最大数据包大小的倍数。那么在主机接收到所有数据后(
我有一台 Windows PC、Marvell 交换机、Netgear 交换机和一台 Ubuntu 机器连接在一起(通过 Netgear 交换机)。 我最近从 Windows PC 向 Marvell
在查看数据包字节码时,您将如何识别 dns 数据包。 IP header 的协议(protocol)字段会告诉后面有一个 UDP 帧,但是在 UDP 帧内没有协议(protocol)字段来指定接下来会
我有一个通过 udf 的 802.11 (wifi) 上各种类型的流量的 pcap。由于 MTU,udp(或更准确地说是 IP)对 wifi 数据包进行分段。我目前正在使用 SharpPcap 读取并
我正在开发的 Core Audio 应用程序上有此崩溃日志。我目前正在调试它,所以我的问题不是关于崩溃本身,而是关于 的含义“k”包 . 这是什么意思 ? 我已阅读 this , 和 this (关于
我在一台 VM Ubuntu 16.04 机器上的 100 个多播组上生成 UDP 数据包,并在另一台 VM Ubuntu 16.04 机器上订阅这些组。两者都在由 Hyper-V 管理器运行的 HP
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个回答) 6年前关闭。 我正在尝试创建一个简单的
我正在寻找使用 Java 来欺骗 UDP 数据包。是否有任何好的 Java 库可以让您创建自己的 RAW SOCKETS? 最佳答案 我会使用包装 libpcap 的 Java API . libpc
我在基于 Tyrus 的客户端和 tomcat Web 服务器之间使用没有压缩的 websocket。我在 tomcat 端看到消息传入和传出我的套接字,但如果我设置一个wireshark来观察它们传
我的应用程序在模拟器中运行时无法接收 UDP 数据包。 UDP 数据包由“localhost”上的以下 java 程序通过端口 49999 发送。 DatagramSocket clien
我正在开发一个 Google Glass 应用程序,它需要在工作线程中监听 UDP 数据包(与发送 UDP 数据包的现有系统集成)。我之前发布了一个问题(请参阅 here )并收到了一个答案,其中提供
我正在从客户端向服务器发送两个数据包。我遇到的问题是,在服务器上读取的数据使两个字符串对于发送的最长字符串具有相同的长度。例如: 如果字符串 1 为:1234 字符串 2 为:abcdefghi 服务
我知道这是不好的做法,但是可以执行以下操作吗? Send packet1 to UDP port 1 port 1 receives packet1 and sends it to port 2 po
我是一名优秀的程序员,十分优秀!