Skip to content

Instantly share code, notes, and snippets.

@dirkmoors
Created April 3, 2014 09:05
Show Gist options
  • Save dirkmoors/9951034 to your computer and use it in GitHub Desktop.
Save dirkmoors/9951034 to your computer and use it in GitHub Desktop.
Smack: Simple MUC test
package messaging;
import java.io.IOException;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freely.messaging.XMPPService;
public class XMPPTest {
private static final Logger logger = LoggerFactory.getLogger(XMPPTest.class.getName());
public static void main(String[] args){
//Added setDefaultPacketReplyTimeout call
SmackConfiguration.setDefaultPacketReplyTimeout(10*1000);
XMPPService xmpp = new XMPPService("USER@HOST", "PASSWORD");
XMPPConnection connection;
try {
connection = xmpp.connect();
} catch (XMPPException e) {
logger.error("Could not connect: XMPPException", e);
return;
} catch (SmackException e) {
logger.error("Could not connect: SmackException", e);
return;
} catch (IOException e) {
logger.error("Could not connect: IOException", e);
return;
}
assert connection.isSecureConnection();
logger.debug("connection established and authentication successfull!");
MultiUserChat muc = new MultiUserChat(connection, "ROOM@MUCSERVICE");
muc.addMessageListener(new ConsumerMUCMessageListener());
try {
muc.join("NICKNAME");
} catch (NoResponseException e) {
logger.error("Could not join conversation: IOException", e);
} catch (XMPPErrorException e) {
logger.error("Could not join conversation: IOException", e);
} catch (NotConnectedException e) {
logger.error("Could not join conversation: IOException", e);
}
logger.debug("joined conversation!");
try {
muc.sendMessage("Hello world!");
} catch (NotConnectedException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
private static class ConsumerMUCMessageListener implements PacketListener {
private int messageCount=0;
public void processPacket(Packet packet) {
if (packet instanceof Message) {
Message msg = (Message)packet;
logger.info("Received message number : " + (messageCount++)+": "+msg.getBody());
}
}
public int getMessageCount() {
return messageCount;
}
}
}
@lokeshsuryan
Copy link

lokeshsuryan commented Jan 31, 2017

multiUserChat.join method return org.jivesoftware.smack.SmackException$NoResponseException

can you half resolving this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment