Java Smack SDKはOpenfireサーバーを結合し、IM通信を確立し、チャットメッセージを送信する
Smackって何?Smackはオープンソースで、使いやすいXMPP(jabber)クライアントクラスライブラリです.Smack APIは、JavaのXMPP Client Libraryであり、Jive Softwareによって開発されています.利点:プログラミングが簡単です.欠点:APIは大量の同時ユーザーのために設計するのではなくて、取引先ごとに1つのスレッドを要して、資源を占有して大きくて、1台の機械は限られた(数千人)取引先をシミュレートすることしかできません.smackはjavaで書くXMPPクライアントコードライブラリで、sparkの核心です.
文章の初めに、openfireに関する知識を理解して熟知してください.そうすれば、次のコードと次のコードの用途を理解するのに役立ちます.同時に、openfireサーバ上でセッションリンクを確立し、オンラインユーザーにチャットメッセージを送信するのに成功したかどうかをテストするために、簡単なCSチャットツールをインストールする必要があります.
次のことを理解する必要があります.http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html
http://www.cnblogs.com/hoojo/archive/2012/05/13/2498151.html (Windows以外のシステム)
オプション:http://www.cnblogs.com/hoojo/archive/2012/05/17/2506845.html
http://www.cnblogs.com/hoojo/archive/2012/06/18/2553975.html
チャットソフトSparkは、チャットメッセージの送信が成功したかどうかをテストし、アドレスをダウンロードするために使用されます.http://www.igniterealtime.org/downloads/download-landing.jsp?file=spark/spark_2_6_3.exe
smack関連jarパッケージを追加する必要があります
smack.jar smackx.jar
JArパッケージのダウンロードアドレス:http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_3_2_2.zip
コードにはjunit、junit jarダウンロードアドレスも使用されます.http://ebr.springsource.com/repository/app/bundle/version/download?name=com.springsource.org.junit&version=4.8.2&type=binary
コードセクションを開始します
はい、これらはすべてsmackの基本的な機能で、もっと多くのものが研究する必要があります.次の機会にまた共有します.
文章の初めに、openfireに関する知識を理解して熟知してください.そうすれば、次のコードと次のコードの用途を理解するのに役立ちます.同時に、openfireサーバ上でセッションリンクを確立し、オンラインユーザーにチャットメッセージを送信するのに成功したかどうかをテストするために、簡単なCSチャットツールをインストールする必要があります.
次のことを理解する必要があります.http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html
http://www.cnblogs.com/hoojo/archive/2012/05/13/2498151.html (Windows以外のシステム)
オプション:http://www.cnblogs.com/hoojo/archive/2012/05/17/2506845.html
http://www.cnblogs.com/hoojo/archive/2012/06/18/2553975.html
チャットソフトSparkは、チャットメッセージの送信が成功したかどうかをテストし、アドレスをダウンロードするために使用されます.http://www.igniterealtime.org/downloads/download-landing.jsp?file=spark/spark_2_6_3.exe
smack関連jarパッケージを追加する必要があります
smack.jar smackx.jar
JArパッケージのダウンロードアドレス:http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_3_2_2.zip
コードにはjunit、junit jarダウンロードアドレスも使用されます.http://ebr.springsource.com/repository/app/bundle/version/download?name=com.springsource.org.junit&version=4.8.2&type=binary
コードセクションを開始します
package com.hoo.smack;
import java.util.Collection;
import java.util.Iterator;
import javax.net.SocketFactory;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Session;
import org.jivesoftware.smack.packet.Message.Type;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* <b>function:</b> Smack XMPP
* @author hoojo
* @createDate 2012-5-22 10:28:18
* @file ConnectionServerTest.java
* @package com.hoo.smack.conn
* @project jwchat
* @blog http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
public class SmackXMPPTest {
private Connection connection;
private ConnectionConfiguration config;
/** openfire address */
private final static String server = "192.168.8.32";
private final void fail(Object o) {
if (o != null) {
System.out.println(o);
}
}
private final void fail(Object o, Object... args) {
if (o != null && args != null && args.length > 0) {
String s = o.toString();
for (int i = 0; i < args.length; i++) {
String item = args[i] == null ? "" : args[i].toString();
if (s.contains("{" + i + "}")) {
s = s.replace("{" + i + "}", item);
} else {
s += " " + item;
}
}
System.out.println(s);
}
}
/**
* <b>function:</b> Smack openfire
* @author hoojo
* @createDate 2012-6-25 04:06:42
*/
@Before
public void init() {
try {
//connection = new XMPPConnection(server);
//connection.connect();
/** 5222 openfire , http://192.168.8.32:9090/ */
config = new ConnectionConfiguration(server, 5222);
/** */
config.setCompressionEnabled(true);
/** */
config.setSASLAuthenticationEnabled(true);
/** */
config.setDebuggerEnabled(false);
//config.setReconnectionAllowed(true);
//config.setRosterLoadedAtLogin(true);
/** connection */
connection = new XMPPConnection(config);
/** */
connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
}
fail(connection);
fail(connection.getConnectionID());
}
@After
public void destory() {
if (connection != null) {
connection.disconnect();
connection = null;
}
}
/**
* <b>function:</b> ConnectionConfiguration
* @author hoojo
* @createDate 2012-6-25 04:11:25
*/
@Test
public void testConfig() {
fail("PKCS11Library: " + config.getPKCS11Library());
fail("ServiceName: {0}", config.getServiceName());
// ssl
fail("TruststorePassword: {0}", config.getTruststorePassword());
fail("TruststorePath: {0}", config.getTruststorePath());
fail("TruststoreType: {0}", config.getTruststoreType());
SocketFactory socketFactory = config.getSocketFactory();
fail("SocketFactory: {0}", socketFactory);
/*try {
fail("createSocket: {0}", socketFactory.createSocket("localhost", 3333));
} catch (IOException e) {
e.printStackTrace();
}*/
}
/**
* <b>function:</b> Connection
* @author hoojo
* @createDate 2012-6-25 04:12:04
*/
@Test
public void testConnection() {
/** */
AccountManager accountManager = connection.getAccountManager();
for (String attr : accountManager.getAccountAttributes()) {
fail("AccountAttribute: {0}", attr);
}
fail("AccountInstructions: {0}", accountManager.getAccountInstructions());
/** */
fail("isConnected:", connection.isConnected());
fail("isAnonymous:", connection.isAnonymous());
/** */
fail("isAuthenticated:", connection.isAuthenticated());
fail("isSecureConnection:", connection.isSecureConnection());
/** */
fail("isUsingCompression:", connection.isUsingCompression());
}
/**
* <b>function:</b>
* @author hoojo
* @createDate 2012-6-25 04:22:31
*/
@Test
public void testAccountManager() {
AccountManager accountManager = connection.getAccountManager();
for (String attr : accountManager.getAccountAttributes()) {
fail("AccountAttribute: {0}", attr);
}
fail("AccountInstructions: {0}", accountManager.getAccountInstructions());
fail("supportsAccountCreation: {0}", accountManager.supportsAccountCreation());
try {
/** boy, boy; http://192.168.8.32:9090/user-summary.jsp / , */
accountManager.createAccount("boy", "boy");
/** */
accountManager.changePassword("abc");
} catch (XMPPException e) {
e.printStackTrace();
}
}
@Test
public void testUser() {
try {
/** , 、 */
connection.login("hoojo", "hoojo");
} catch (XMPPException e) {
e.printStackTrace();
}
/** */
fail("User:", connection.getUser());
/** */
Roster roster = connection.getRoster();
/** , Spark , */
Collection<RosterEntry> rosterEntiry = roster.getEntries();
Iterator<RosterEntry> iter = rosterEntiry.iterator();
while (iter.hasNext()) {
RosterEntry entry = iter.next();
fail("Groups: {0}, Name: {1}, Status: {2}, Type: {3}, User: {4}", entry.getGroups(), entry.getName(), entry.getStatus(), entry.getType(), entry);
}
fail("-------------------------------");
/** 、 , , */
Collection<RosterEntry> unfiledEntries = roster.getUnfiledEntries();
iter = unfiledEntries.iterator();
while (iter.hasNext()) {
RosterEntry entry = iter.next();
fail("Groups: {0}, Name: {1}, Status: {2}, Type: {3}, User: {4}", entry.getGroups(), entry.getName(), entry.getStatus(), entry.getType(), entry);
}
}
@Test
@SuppressWarnings("static-access")
public void testPacket() {
try {
connection.login("hoojo", "hoojo");
} catch (XMPPException e) {
e.printStackTrace();
}
//Packet packet = new Data(new DataPacketExtension("jojo@" + server, 2, "this is a message"));
//connection.sendPacket(packet);
/** ,available=true ,false ,status ; , Spark */
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Q ");
connection.sendPacket(presence);
Session session = new Session();
String sessid = session.nextID();
connection.sendPacket(session);
/** [email protected] , Spark jojo ,
* jojo ,Spark jojo
**/
/** Type.chat ,groupchat ,error ,headline ; */
Message message = new Message("jojo@" + server, Type.chat);
//Message message = new Message(sessid, Type.chat);
message.setBody("h!~ jojo, I'am is hoojo!");
connection.sendPacket(message);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* <b>function:</b>
* @author hoojo
* @createDate 2012-6-25 05:03:23
*/
@Test
public void testChatManager() {
/** */
try {
connection.login("hoojo", "hoojo");
} catch (XMPPException e) {
e.printStackTrace();
}
/** */
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Q ");
connection.sendPacket(presence);
/** */
ChatManager chatManager = connection.getChatManager();
/** chat,MyMessageListeners */
Chat chat = chatManager.createChat("jojo@" + server, new MyMessageListeners());
try {
/** */
chat.sendMessage("h!~ jojo……");
/** message */
Message message = new Message();
message.setBody("message");
message.setProperty("color", "red");
chat.sendMessage(message);
} catch (XMPPException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* <b>function:</b> , ,
* @author hoojo
* @createDate 2012-6-25 05:05:31
* @file SmackXMPPTest.java
* @package com.hoo.smack
* @project jwchat
* @blog http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
class MyMessageListeners implements MessageListener {
public void processMessage(Chat chat, Message message) {
try {
/** */
chat.sendMessage("dingding……" + message.getBody());
} catch (XMPPException e) {
e.printStackTrace();
}
/** */
fail("From: {0}, To: {1}, Type: {2}, Sub: {3}", message.getFrom(), message.getTo(), message.getType(), message.toXML());
/*Collection<Body> bodys = message.getBodies();
for (Body body : bodys) {
fail("bodies[{0}]", body.getMessage());
}
//fail(message.getLanguage());
//fail(message.getThread());
//fail(message.getXmlns());*/
fail("body: ", message.getBody());
}
}
}
はい、これらはすべてsmackの基本的な機能で、もっと多くのものが研究する必要があります.次の機会にまた共有します.