Project

General

Profile

jaxmpp how to send message of External links to chat room with my http api through my server application?

w xy
Added 4 months ago

private Jaxmpp jaxmpp(String username) {
        Jaxmpp jaxmpp = new Jaxmpp();
        this.setConfiguration(jaxmpp, username);
        return jaxmpp;
   }

private void setConfiguration(Jaxmpp jaxmpp, String username) {
        ConnectionConfiguration configuration = jaxmpp.getConnectionConfiguration();
        configuration.setDomain(xmppConfig.getServerName());
        configuration.setPort(xmppConfig.getPort());
        configuration.setServer(xmppConfig.getHost());
        configuration.setUserJID(this.userJid(username));
        configuration.setUserPassword(Md5Util.md5Hex(username));
        configuration.setResource(xmppConfig.getResource());
        configuration.setDisableTLS(true);
        configuration.setConnectionType(ConnectionConfiguration.ConnectionType.socket);
    }


public void sendMUCRoom(String username, String password, String jid, String content) {
        Jaxmpp jaxmpp = this.jaxmpp(username);
        try {
            jaxmpp.login(true);
            **Message message = new Message(JID.jidInstance("peek@xmpp.server"), JID.jidInstance("xxxxxxx@muc.xmpp.server"), StanzaType.groupchat, content);**
            if(jaxmpp.isConnected()){
                jaxmpp.send(message.getMessage());
            }
        } catch (JaxmppException e) {
            e.printStackTrace();
        } finally {
            try {
                jaxmpp.disconnect();
            } catch (JaxmppException e) {
                e.printStackTrace();
            }
        }
    }

however, it is failure, but the message would be insert into the room record table.


Replies (4)

(1)

Added by w xy 4 months ago

The second, mucModule = jaxmpp.getModulesManager().register(new MucModule());

Room room = mucModule.getRoom("xxxxxxx@muc.xmpp.server"); room is null.

mucModule.getRooms() the size is zero.

Avatar?id=6098&size=32x32

Added by Bartosz Małkowski TigaseTeam 4 months ago

First of all: you have to join MUC room. Then you're able to send messages to room.
Second: JaXMPP library is asynchronous and is based on events.

Here is simple example code, what show you how to join to room and when (and how) send message:

final Jaxmpp jaxmpp = new Jaxmpp();

jaxmpp.getModulesManager().register(new MucModule());

ConnectionConfiguration configuration = jaxmpp.getConnectionConfiguration();
configuration.setUserJID("XXXXXX@YYYYYYY");
configuration.setUserPassword("***********");
configuration.setResource("bot");
configuration.setDisableTLS(true);

jaxmpp.login(true);

jaxmpp.getEventBus()
        .addHandler(MucModule.MucMessageReceivedHandler.MucMessageReceivedEvent.class,
                    (sessionObject, message, room, nickname, timestamp) -> {
                        try {
                            System.out.println("[" + timestamp + "] " + nickname + ": " + message.getBody());
                        } catch (XMLException e) {
                            e.printStackTrace();
                        }
                    });

jaxmpp.getEventBus()
        .addHandler(MucModule.YouJoinedHandler.YouJoinedEvent.class, (sessionObject, room, asNickname) -> {
            System.out.println("JOINED!");
            try {
                room.sendMessage("O! Hi there!");
            } catch (JaxmppException e) {
                e.printStackTrace();
            }
        });

MucModule mucModule = jaxmpp.getModule(MucModule.class);
mucModule.join("ROOMNAME", "MUCSERVER", "ImABot");

Added by w xy 3 months ago

It work well.

try {
Room room = mucModule.join(jid, "muc." + xmppConfig.getServerName(), jid);
Message message = new Message(JID.jidInstance(this.userJid(username)), JID.jidInstance(this.mucJid(jid)), StanzaType.groupchat, content);
room.sendMessage(message.getMessage());
} catch (JaxmppException e) {
e.printStackTrace();
}

However, what is it different your example?

Avatar?id=6098&size=32x32

Added by Bartosz Małkowski TigaseTeam 3 months ago

In your example client does not wait for room join confirmation.

    (1-4/4)