import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
/**
* Chat prototype using smack as client library. Server side are supported by
* openfire.
*
* @author promise
* @date 2009-3-30
*
*/
public class FireChat {
public static XMPPConnection con;
public static Chat newChat;
public static ChatManager chatmanager;
public static void main(String[] args) throws InterruptedException {
// XMPPConnection.DEBUG_ENABLED = true;
try {
// Create a connection to server
ConnectionConfiguration config = new ConnectionConfiguration(
"localhost", 5222);
con = new XMPPConnection(config);
// connect and login with the username and pwd on server
con.connect();
con.login("admin", "123");
System.out.println("Authenticated = " + con.isAuthenticated());
// add a listener to receive all messages
addListener();
chatmanager = con.getChatManager();
newChat = chatmanager.createChat("zkp@10.25.101.159/spark",
new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("I'm sending: "
+ message.getBody());
}
});
newChat.sendMessage("hi");
} catch (XMPPException e) {
e.printStackTrace();
} finally {
//让线程休眠 然后再关闭连接
Thread.sleep(3600000);
con.disconnect();
}
}
private static void addListener() {
// just need Messages
PacketFilter filterMessage = new PacketTypeFilter(Message.class);
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
System.out.println("From: " + packet.getFrom() + "\n");
System.out.println("Body: " + ((Message) packet).getBody());
// when receiving prc's Message, just say something else again
// and again, robot
try {
newChat.sendMessage("hi again");
} catch (XMPPException e) {
e.printStackTrace();
}
}
};
// register the listener to the connection
con.addPacketListener(myListener, filterMessage);
}
}
要提到的是,运行此程序必须先配置好openfire的服务器,然后把用户名等写对!
分享到:
相关推荐
这个是Smack API的中文版,详细介绍了smack原理,并且有几个小例子在里面
Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档Smack中文文档
smack源码
Smack中文API文档
smack用于android移动端的开发,可以结合openfire使用,进行im通信
1)smack api,基于smack官方javadoc制作而成,格式chm,语言english; 2)smack documentation,内容包括Overview,Getting Started Guide等等,格式chm,语言为中文(感谢fhqdddddd的奉献,本文档基于...
Smack是一个开源,易于使用的XMPP(jabber)客户端类库。 附件是目前最新版本的smack库,含源码。
smack api帮助文档官方提供的帮助工具
smack xmpp 3.2.2 for linux
Smack帮助文档中文版:XMPP协议包括一个基本协议和许多可选扩充,像具有代表性的"JEP's".Smack 为核心XMPP协议提供了org.jivesoftware.smack包,为许多协议扩充提供了org.jivesoftware.smackx包.
最新的 smack4.1.4 android开发demo 可以登录 已验证
smack3.3.1源码 open fire开源的聊天服务器。客户端的核心xmpp协议的操作。
smack源码下载,java版下载,smack源码下载
smack xmpp 即时通讯,使用smack库结合openfire实现即时通讯
Openfire+spark+smack xmpp
基于openfire服务器利用Xmpp协议在Android上实现即时通讯 内含:smack.jar, smackx.jar, smackx-debug.jar, smackx-jingle.jar
Android端smack4.2.2 IM开发全部jar包包含jxmpp和smack。
Android 链接openfire的smack库
SMACK文件传输的介绍及代码,官网源文件
Smack类库最好的学习资料