Search in sources :

Example 1 with MessageListener

use of org.jivesoftware.smack.MessageListener in project Smack by igniterealtime.

the class MultiUserChatIntegrationTest method mucTest.

@SmackIntegrationTest
public void mucTest() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-message");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);
    final String mucMessage = "Smack Integration Test MUC Test Message " + randomString;
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    mucAsSeenByTwo.addMessageListener(new MessageListener() {

        @Override
        public void processMessage(Message message) {
            String body = message.getBody();
            if (mucMessage.equals(body)) {
                resultSyncPoint.signal(body);
            }
        }
    });
    createMuc(mucAsSeenByOne, "one-" + randomString);
    mucAsSeenByTwo.join(Resourcepart.from("two-" + randomString));
    mucAsSeenByOne.sendMessage(mucMessage);
    try {
        resultSyncPoint.waitForResult(timeout);
    } catch (TimeoutException e) {
        throw new AssertionError("Failed to receive presence", e);
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) MessageListener(org.jivesoftware.smack.MessageListener) EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackException(org.jivesoftware.smack.SmackException) TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) TimeoutException(java.util.concurrent.TimeoutException) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 2 with MessageListener

use of org.jivesoftware.smack.MessageListener in project ecf by eclipse.

the class ECFConnection method sendMessage.

protected void sendMessage(ID receiver, Message aMsg) throws IOException {
    synchronized (this) {
        if (!isConnected())
            throw new IOException("not connected");
        try {
            if (receiver == null)
                throw new IOException("receiver cannot be null for xmpp instant messaging");
            else if (receiver instanceof XMPPID) {
                final XMPPID rcvr = (XMPPID) receiver;
                aMsg.setType(Message.Type.chat);
                final String receiverName = rcvr.getFQName();
                final Chat localChat = connection.getChatManager().createChat(receiverName, new MessageListener() {

                    public void processMessage(Chat chat, Message message) {
                    }
                });
                localChat.sendMessage(aMsg);
            } else if (receiver instanceof XMPPRoomID) {
                final XMPPRoomID roomID = (XMPPRoomID) receiver;
                aMsg.setType(Message.Type.groupchat);
                final String to = roomID.getMucString();
                aMsg.setTo(to);
                connection.sendPacket(aMsg);
            } else
                throw new IOException("receiver must be of type XMPPID or XMPPRoomID");
        } catch (final XMPPException e) {
            final IOException result = new IOException("XMPPException in sendMessage: " + e.getMessage());
            result.setStackTrace(e.getStackTrace());
            throw result;
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) MessageListener(org.jivesoftware.smack.MessageListener) XMPPRoomID(org.eclipse.ecf.provider.xmpp.identity.XMPPRoomID) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID)

Aggregations

MessageListener (org.jivesoftware.smack.MessageListener)2 XMPPException (org.jivesoftware.smack.XMPPException)2 Message (org.jivesoftware.smack.packet.Message)2 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 XMPPID (org.eclipse.ecf.provider.xmpp.identity.XMPPID)1 XMPPRoomID (org.eclipse.ecf.provider.xmpp.identity.XMPPRoomID)1 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)1 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)1 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)1 Chat (org.jivesoftware.smack.Chat)1 SmackException (org.jivesoftware.smack.SmackException)1 EntityBareJid (org.jxmpp.jid.EntityBareJid)1