Message Events 研究!
Description
In order to receive event notifications for a given message you first have to specify
which events are you interested in. Each message that you send has to request its own event
notifications. Therefore, every message that you send as part of a chat should request its own event
notifications.
Usage
The class MessageEventManager
provides an easy way for requesting event notifications. All you have to do is specify
the message that requires the event notifications and the events that you are interested in.
Use the static method MessageEventManager.addNotificationsRequests(Message message, boolean offline, boolean
delivered, boolean displayed, boolean composing)
for requesting event notifications.
Example
Below you can find an example that logs in a user to the server, creates a message, adds the requests
for notifications and sends the message.
// Connect to the server and log in
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat with user2
Chat chat1 = conn1.createChat(user2);
// Create a message to send
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"
);
msg.setBody("An interesting body comes here..."
);
// Add to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true
, true
, true
, true
);
// Send the message that contains the notifications request
chat1.sendMessage(msg);
Description
You can receive notification requests for the following events: delivered, displayed, composing and offline. You
must
listen for these requests and react accordingly.
Usage
The general idea is to create a new DefaultMessageEventRequestListener
that will listen to the event notifications
requests and react with custom logic. Then you will have to add the listener to the
MessageEventManager
that works on
the desired XMPPConnection
.
Note that DefaultMessageEventRequestListener
is a default implementation of the
MessageEventRequestListener
interface.
The class DefaultMessageEventRequestListener
automatically sends a delivered notification to the sender of the message
if the sender has requested to be notified when the message is delivered. If you decide to create a new class that
implements the MessageEventRequestListener
interface, please remember to send the delivered notification.
- To create a new MessageEventManager
use the MessageEventManager(XMPPConnection)
constructor.
- To create an event notification requests listener create a subclass of DefaultMessageEventRequestListener
or
create a class that implements the MessageEventRequestListener
interface.
- To add a listener to the messageEventManager use the MessageEventManager's message
addMessageEventRequestListener(MessageEventRequestListener)
.
Example
Below you can find an example that connects two users to the server. One user will create a message, add the requests
for notifications and will send the message to the other user. The other user will add a
DefaultMessageEventRequestListener
to a MessageEventManager
that will listen and react to the event notification requested by the other user.
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// User2 creates a MessageEventManager
MessageEventManager messageEventManager = new MessageEventManager(conn2);
// User2 adds the listener that will react to the event notifications requests
messageEventManager.addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
public void deliveredNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.deliveredNotificationRequested(from, packetID, messageEventManager);
// DefaultMessageEventRequestListener automatically responds that the message was delivered when receives this request
System.out.println("Delivered Notification Requested (" + from + ", " + packetID + ")"
);
}
public void displayedNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.displayedNotificationRequested(from, packetID, messageEventManager);
// Send to the message's sender that the message was displayed
messageEventManager.sendDisplayedNotification(from, packetID);
}
public void composingNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.composingNotificationRequested(from, packetID, messageEventManager);
// Send to the message's sender that the message's receiver is composing a reply
messageEventManager.sendComposingNotification(from, packetID);
}
public void offlineNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.offlineNotificationRequested(from, packetID, messageEventManager);
// The XMPP server should take care of this request. Do nothing.
System.out.println("Offline Notification Requested (" + from + ", " + packetID + ")"
);
}
});
// User1 creates a chat with user2
Chat chat1 = conn1.createChat(user2);
// User1 creates a message to send to user2
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"
);
msg.setBody("An interesting body comes here..."
);
// User1 adds to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true
, true
, true
, true
);
// User1 sends the message that contains the notifications request
chat1.sendMessage(msg);
Thread.sleep(500);
// User2 sends to the message's sender that the message's receiver cancelled composing a reply
messageEventManager.sendCancelledNotification(user1, msg.getPacketID());
Description
Once you have requested for event notifications you will start to receive notifications of events. You can
receive notifications of the following events: delivered, displayed, composing, offline and cancelled. You
will probably want to react to some or all of these events.
Usage
The general idea is to create a new MessageEventNotificationListener
that will listen to the event notifications
and react with custom logic. Then you will have to add the listener to the MessageEventManager
that works on
the desired XMPPConnection
.
- To create a new MessageEventManager
use the MessageEventManager(XMPPConnection)
constructor.
- To create an event notifications listener create a class that implements the MessageEventNotificationListener
interface.
- To add a listener to the messageEventManager use the MessageEventManager's message
addMessageEventNotificationListener(MessageEventNotificationListener)
.
Example
Below you can find an example that logs in a user to the server, adds a MessageEventNotificationListener
to a MessageEventManager
that will listen and react to the event notifications, creates a message, adds
the requests for notifications and sends the message.
// Connect to the server and log in
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a MessageEventManager
MessageEventManager messageEventManager = new MessageEventManager(conn1);
// Add the listener that will react to the event notifications
messageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
System.out.println("The message has been delivered (" + from + ", " + packetID + ")"
);
}
public void displayedNotification(String from, String packetID) {
System.out.println("The message has been displayed (" + from + ", " + packetID + ")"
);
}
public void composingNotification(String from, String packetID) {
System.out.println("The message's receiver is composing a reply (" + from + ", " + packetID + ")"
);
}
public void offlineNotification(String from, String packetID) {
System.out.println("The message's receiver is offline (" + from + ", " + packetID + ")"
);
}
public void cancelledNotification(String from, String packetID) {
System.out.println("The message's receiver cancelled composing a reply (" + from + ", " + packetID + ")"
);
}
});
// Create a chat with user2
Chat chat1 = conn1.createChat(user2);
// Create a message to send
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"
);
msg.setBody("An interesting body comes here..."
);
// Add to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true
, true
, true
, true
);
// Send the message that contains the notifications request
chat1.sendMessage(msg);
分享到:
相关推荐
Smack 是一个开源的即时通讯库,主要用于实现XMPP(Extensible Messaging and Presence Protocol)协议。...开发者可以通过解压并研究这些文件,学习如何在自己的Android项目中集成和使用Smack4.1.4。
"javadoc"目录下则是Smack的Java API文档,对于开发者来说,这是一个不可或缺的参考工具,可以帮助快速查找类、方法和接口,理解其功能和使用方法。 **库文件** "libs"目录包含了Smack的库文件,开发者可以将这些...
《Smack文档中文版》是针对Smack开源项目的详尽指南,主要面向开发者和IT专业人士。Smack是一个用Java编写的开源库,用于在XMPP...同时,对于源码的探索也能帮助我们深入了解其内部机制,为二次开发或优化提供可能。
Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档
这对于跟踪API的发展和决定是否升级到新版本非常有帮助。开发者可以通过阅读release notes了解哪些新特性可能适合自己的项目,以及如何适应可能的API变化。 3. **Libs**:这个目录下的文件是Smack API的实际库文件...
通过对Smack源码的深入研究,开发者可以更好地理解XMPP协议的工作原理,定制化需求,优化性能,并解决实际项目中遇到的问题。同时,源码学习也有助于开发者提升对网络通信、多线程、事件驱动编程等技术的理解。
通过研究Smack的源码,我们可以深入了解XMPP的工作原理,以及如何在Java应用程序中实现实时通信功能。 首先,Smack源码包含了众多类和接口,它们是构建XMPP客户端和服务器端的基础。例如,`XMPPTCPConnection`是...
**二、连接ejabberd服务器** 连接ejabberd服务器需要创建XMPPTCPConnection对象。以下代码展示了如何建立连接: ```java XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() .set...
"javadoc"是Smack的API文档,对于开发者来说至关重要,它提供了所有类、方法和接口的详细说明,方便查阅和学习。"libs"文件夹则很可能包含了Smack 4.1的库文件,供开发者在Android项目中集成使用。 在实际开发中,...
1)smack api,基于smack官方javadoc制作而成,格式chm,语言english; 2)smack documentation,内容包括Overview,Getting Started Guide等等,格式chm,语言为中文(感谢fhqdddddd的奉献,本文档基于...
Smack 3.2.2 是一个专门针对Linux平台的XMPP(Extensible Messaging and Presence Protocol)库。XMPP是一种开放标准的即时通讯协议,它允许用户进行实时、双向通信,广泛应用于聊天应用、协作工具以及物联网设备...
3. **smackx-debug.jar**:包含了用于调试和日志记录的类,帮助开发者追踪和分析应用在运行时的行为,对于定位问题和优化性能非常有帮助。 4. **smackx-jingle.jar**:Jingle是XMPP的一个子集,用于处理多媒体通信...
### Smack类库最佳学习资料概述 #### 一、Smack类库介绍及应用场景 Smack是一个用于开发基于XMPP(可扩展消息处理协议)的应用程序的...对于更复杂的应用场景,如群聊、文件传输等功能,Smack也提供了相应的API支持。
在Smack_4_1_6的javadoc中,包含了详细的类、接口、方法和常量描述,这对于开发者了解如何与Smack库进行交互至关重要。通过阅读javadoc,开发者可以了解到每个类的功能,以及如何正确地调用库中的方法来实现XMPP功能...
对于调试,Smack 提供了 `DEBUG_ENABLED` 静态变量,当设置为 `true` 时,将启用详细的日志输出,这对于追踪问题和理解底层通信流程非常有帮助。 除了基本的聊天功能,Smack 还支持更复杂的特性,如多用户聊天(MUC...
由于原始的Smack文档主要为英文,对于中文使用者来说,可能存在一定的理解障碍。然而,尽管这里的中文文档可能较旧,但对于初学者而言,仍然是一个宝贵的资源,可以辅助理解XMPP协议的基础概念和Smack库的使用方法。...
Smack中文API文档
对于开发者而言,可以利用Smack库在自己的应用中实现与Openfire服务器的连接,从而提供IM功能。 在实际应用中,Openfire通常用于企业内部沟通或者在线客服系统,因为它支持大规模用户并发,且提供了丰富的API和插件...