Java源码示例:org.jivesoftware.smackx.receipts.DeliveryReceipt
示例1
@Override
public void processStanza(Stanza p) {
// NOTE: the packet is not the acknowledgement itself but the packet
// that is acknowledged
if (!(p instanceof Message)) {
// we are only interested in acks for messages
return;
}
Message m = (Message) p;
LOGGER.config("for message: "+m);
if (DeliveryReceipt.from(m) != null) {
// this is an ack for a 'received' message (XEP-0184) send by
// KonMessageListener, ignore
return;
}
if (m.getBody() == null && m.getExtensions().size() == 1 &&
m.getExtension(ChatStateExtension.NAMESPACE) != null) {
// this is an ack for a chat state notification (XEP-0085), ignore
return;
}
mControl.onMessageSent(MessageIDs.to(m));
}
示例2
private void handleMessage (org.jivesoftware.smack.packet.Message smackMessage, boolean isOmemo, boolean notifyUser) {
String body = smackMessage.getBody();
boolean isGroupMessage = smackMessage.getType() == org.jivesoftware.smack.packet.Message.Type.groupchat;
if (smackMessage.getError() != null) {
// smackMessage.getError().getCode();
String error = "Error " + smackMessage.getError() + " (" + smackMessage.getError().getCondition() + "): " + smackMessage.getError().getConditionText();
debug(TAG, error);
return;
}
if (TextUtils.isEmpty(body)) {
Collection<org.jivesoftware.smack.packet.Message.Body> mColl = smackMessage.getBodies();
for (org.jivesoftware.smack.packet.Message.Body bodyPart : mColl) {
String msg = bodyPart.getMessage();
if (msg != null) {
body = msg;
break;
}
}
}
ChatSession session = findOrCreateSession(smackMessage.getFrom().toString(), isGroupMessage, false);
if (session != null) //not subscribed so don't do anything
{
if ((!TextUtils.isEmpty(body)) && session != null) {
Message rec = new Message(body);
rec.setTo(new XmppAddress(smackMessage.getTo().toString()));
rec.setFrom(new XmppAddress(smackMessage.getFrom().toString()));
rec.setDateTime(new Date());
rec.setID(smackMessage.getStanzaId());
if (isOmemo)
rec.setType(Imps.MessageType.INCOMING_ENCRYPTED_VERIFIED);
else
rec.setType(Imps.MessageType.INCOMING);
// Detect if this was said by us, and mark message as outgoing
if (isGroupMessage) {
if (TextUtils.isEmpty(rec.getFrom().getResource()))
{
return; //do nothing if there is no resource since that is a system message
}
else if (rec.getFrom().getResource().equals(rec.getTo().getUser())) {
try {
//rec.setType(Imps.MessageType.OUTGOING);
Occupant oc = mChatGroupManager.getMultiUserChat(rec.getFrom().getBareAddress()).getOccupant(JidCreate.entityFullFrom(rec.getFrom().getAddress()));
if (oc != null && oc.getJid().equals(mUser.getAddress().getAddress()))
return; //do nothing if it is from us
}
catch (Exception e){
debug(TAG,"error parsing address",e);
}
}
}
else
{
Contact contact = (Contact)session.getParticipant();
if (contact.getPresence() == null || contact.getPresence().getResource() == null)
{
session.updateParticipant(handlePresenceChanged(mRoster.getPresence(smackMessage.getFrom().asBareJid())));
}
}
boolean good = session.onReceiveMessage(rec, notifyUser);
if (smackMessage.getExtension("request", DeliveryReceipt.NAMESPACE) != null) {
if (good) {
debug(TAG, "sending delivery receipt");
// got XEP-0184 request, send receipt
sendReceipt(smackMessage);
session.onReceiptsExpected(true);
} else {
debug(TAG, "not sending delivery receipt due to processing error");
}
} else {
//no request for delivery receipt
session.onReceiptsExpected(false);
}
}
}
}
示例3
private void processChatMessage(Message m) {
LOGGER.config("message: "+m);
// note: thread and subject are null if message comes from the Kontalk
// Android client
MessageIDs ids = MessageIDs.from(m);
Date delayDate = getDelay(m);
// process possible chat state notification (XEP-0085)
ExtensionElement csExt = m.getExtension(ChatStateExtension.NAMESPACE);
ChatState chatState = null;
if (csExt != null) {
chatState = ((ChatStateExtension) csExt).getChatState();
mControl.onChatStateNotification(ids,
Optional.ofNullable(delayDate),
chatState);
}
// must be an incoming message
// get content/text from body and/or encryption/url extension
MessageContent content = ClientUtils.parseMessageContent(m, false);
// make sure not to save a message without content
if (content.isEmpty()) {
if (chatState == null) {
LOGGER.warning("can't find any content in message");
} else if (chatState == ChatState.active) {
LOGGER.info("only active chat state");
}
return;
}
// add message
mControl.onNewInMessage(ids, Optional.ofNullable(delayDate), content);
// send a 'received' for a receipt request (XEP-0184)
DeliveryReceiptRequest request = DeliveryReceiptRequest.from(m);
if (request != null && !ids.xmppID.isEmpty()) {
Message received = new Message(m.getFrom(), Message.Type.chat);
received.addExtension(new DeliveryReceipt(ids.xmppID));
mClient.sendPacket(received);
}
}