下面主要說一下smack 4.1.2 的開發使用,在網上看了好多文章包括stackoverflow的都沒有4.1以上的使用說明,同時我發現官方的一些說明好像和這個版本不對應,例如UseConnectionConfiguration#setReconnectionAllowed(boolean) to turn on/off this feature, 我查了根本沒有這個方法,所以以就自己寫了一些筆記,供大家參考,同時共同討論促進大家使用smack的最新版。
以下是我參考的鏈接
http://my.oschina.net/yuanxulong/blog/348572
https://github.com/igniterealtime/Smack/blob/master/documentation/gettingstarted.md
http://blog.csdn.net/chszs/article/details/41620843
http://blog.csdn.net/chszs/article/details/41620853
securty問題
https://issues.jenkins-ci.org/browse/JENKINS-25505
認證方式
http://blog.csdn.net/coding_me/article/details/39524137
0認證的解決辦法
I have solved this problem, the Service Name should be the same as openfire settings:
下面是我寫的工具類,聊天也就主要兩件事情,一個就是發送信息,一個是接受信息,發送就是smackTools.getChat();.sendMessage("detail") 獲取內容就是 chatManager.addChatListener(new ChatManagerListener()
那一部分,總體講比較簡單,以下說明需要注意的幾點
(1)以下是securtyMode關閉之後的代碼
(2)ServerName不是域名是openfire服務器的名稱
(3)host可以在host文件中更改
(4)connect.login()完必須循環有操作否則會在別人那裏顯示爲脫機狀態
(5) newChat = chatManager.createChat("why2@why-pc",null); @後面是serverName
(6)使用完必須關閉連接否則會出現發送一條多條顯示的現象。因爲每個對話都是單獨一條線程
(7)採用循環讓獲取對方說話內容一方面獲取內容了而且保證一直在線,像網上說的那個設置重連的方法在這版本中不存在
public class SmackTools {
public static SmackTools smackTools;
private AbstractXMPPConnection connection;
private String content;
private Chat newChat;
private SmackTools() throws InterruptedException{
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setUsernameAndPassword("why1", "why134134");
builder.setServiceName("why-pc");
builder.setHost("csdn.shimiso.com");
builder.setConnectTimeout(10000000);
builder.setResource("why");
ConnectionConfiguration config = builder.build();
try {
connection = new XMPPTCPConnection(builder.build());
connection.connect();
connection.login();
//獲取的談話
//chat
final ChatManager chatManager = ChatManager.getInstanceFor(connection);
newChat = chatManager.createChat("why2@why-pc",null);
new Thread(){
public void run() {
while(true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
chatManager.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
if (!createdLocally) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
if(message.getBody() == null || message.getBody().equals(null)){
}
else{
// System.out
//
.println("run...");
System.out
.println("from " + message.getFrom() + " " + message.getBody());
content = content + "\n" + message.getFrom() + ": " + message.getBody();
Consts.content = Consts.content + "\n" + message.getFrom() + ": " + message.getBody();
}
}
});
}
}
});
}
};
}.start();
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Chat getChat(){
return newChat;
}
public String getContent(){
return content;
}
public AbstractXMPPConnection getConn(){
return connection;
}
public void disConn(){
connection.disconnect();
}
public static SmackTools getInstance(){
if(smackTools == null){
try {
smackTools = new SmackTools();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return smackTools;
}
}