tigase客戶端之smack用法【草稿】
專案介紹
sureim
專案地址:
sureim專案地址
git clone https://git.tigase.tech/sureim.git
demo網站:
tigase im
獲取原始碼
git clone https://git.tigase.tech/sureim.git ~/git-projects/sureim
配置
spark原始碼
git clone https://github.com/igniterealtime/Spark.git ~/git-projects/spark
一個客戶端竟然有300mb。。
然後,mvn先編譯一下:
$ cd Spark/core
$ mvn exec:java
經過漫長的下載依賴以後就出現這個畫面了額。。報錯:
額,沒有一個專案是什麼都不用管就能直接執行的。
下面來構建一個idea+gradle的spark原始碼專案。
idea專案
smack的專案地址
請下載相關jar包然後新增依賴,暫且不表。
程式碼編寫測試
Openfire/XMPP學習之——一個簡單的Smack樣例
【openfire,smack使用總結】–Smack庫的使用
編寫程式碼編譯:
注意,org.jxmpp整個包都是沒有的,這裡有:
https://mvnrepository.com/artifact/org.jxmpp/jxmpp-core/0.6.3
匯入gradle然後試試:
jid不見,這裡有:
https://mvnrepository.com/artifact/org.jxmpp/jxmpp-jid/0.6.3
再試試:
根據各種錯誤,至少,gradle中的依賴需要有:
plugins { id 'java' } group 'net.w2p' version '1.0-SNAPSHOT' /***所有專案共通***/ allprojects { sourceCompatibility = 1.8 targetCompatibility = 1.8 apply plugin: 'java' apply plugin: 'idea' apply plugin: 'groovy' idea { module { inheritOutputDirs = true } } tasks.withType(JavaCompile) { options.encoding = "UTF-8" } tasks.withType(GroovyCompile) { groovyOptions.encoding = "MacRoman" } repositories { maven{ //更換為阿里的倉庫 url 'http://maven.aliyun.com/nexus/content/groups/public' } //有些jar包在中央倉庫是沒有的,需要手動新增上去 // flatDir { dirs 'local_jars' } // mavenCentral() } } dependencies { compile fileTree(dir:'libs/smack_4_3_1/libs',includes:['*jar']) compile 'org.jxmpp:jxmpp-core:0.6.3' compile 'org.jxmpp:jxmpp-jid:0.6.3' compile 'xmlpull:xmlpull:1.1.3.4a' compile 'net.sf.kxml:kxml2:2.3.0' compile 'org.minidns:minidns-hla:0.3.3' compile 'org.minidns:minidns-core:0.3.3' // compile 'org.xbill:dns:2.0.8' compile 'dnsjava:dnsjava:2.1.8' compile 'org.apache.httpcomponents:httpclient:4.5.6' testCompile fileTree(dir:'libs/smack_4_3_1/libs',includes:['*jar']) testCompile group: 'junit', name: 'junit', version: '4.12' }
然後執行:
連結不成功,好了,啟動自己搭建的tigase server:
好了,改改地址埠,然後執行:
報錯如下:
Exception in thread "main" org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1069)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:993)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1009)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:803)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$1200(XMPPTCPConnection.java:151)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1064)
... 3 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)
... 13 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)
... 19 more
十二月 19, 2018 4:13:31 下午 org.jivesoftware.smack.AbstractXMPPConnection callConnectionClosedOnErrorListener
警告: Connection XMPPTCPConnection[not-authenticated] (0) closed with error
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:803)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$1200(XMPPTCPConnection.java:151)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1064)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:993)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1009)
at java.lang.Thread.run(Thread.java:748)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)
... 13 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)
... 19 more
Process finished with exit code 1
而服務端有:
2018-12-19 16:13:30.784 [ConnectionOpenThread] SocketThread.<clinit>() WARNING: 33 socketReadThreads started.
2018-12-19 16:13:30.802 [ConnectionOpenThread] SocketThread.<clinit>() WARNING: 33 socketWriteThreads started.
2018-12-19 16:13:30.969 [in_23-c2s] CertificateContainer.createCertificate() WARNING: Auto-generated certificate for domain: localhost
查詢解決方案:
解決Java呼叫Azure SDK證書錯誤javax.net.ssl.SSLHandshakeException
解決 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path buildin
不對,經過資料核查,發現:
Smack 4 throws “SSLHandshakeException: ValidatorException: SunCertPathBuilderException” on connect
資料太久了,不適用於smack4.。。
下面有一些新一點的資料:
或者直接點:
smack4中文文件
也有另一位兄弟也寫了:
package main.im_tester;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import java.net.InetAddress;
public class FirstTest {
public static void main(String[] args) throws Exception {
main3();
}
static void main1() throws Exception {
// Create a connection to the jabber.org server on a specific port.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("username", "password")
.setXmppDomain("localhost")
.setHost("localhost")
.setPort(5222)
.build();
// org.xmlpull.v1.
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
conn2.connect().login();
}
static void main2() throws Exception {
// Create a connection to the jabber.org server.
// AbstractXMPPConnection conn1 = new XMPPTCPConnection(“username”, “password” ,“localhost”);
// conn1.connect();
//
//// Create a connection to the jabber.org server on a specific port.
// XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
// .setUsernameAndPassword(“username”, “password”)
// .setServiceName(“localhost”)
// .setHost(“localhost”)
// .setPort(5222)
// .build();
//
// AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
// conn2.connect();
// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
// configBuilder.setUsernameAndPassword(“username”, “password”);
configBuilder.setResource(“SomeResource”);
configBuilder.setXmppDomain(“localhost”);
//–這個比較重要,假如不設定的話是預設開啟加密傳輸的,到時候會有錯誤:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path buildin
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
connection.connect();
// Log into the server
// connection.login();
// Disconnect from the server
connection.disconnect();
}
public static void main3(){
XMPPConnection connection=createConnection();
((XMPPTCPConnection) connection).disconnect();
}
public static XMPPTCPConnection createConnection() {
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
XMPPTCPConnection connection=null;
try {
//設定openfire主機IP
config.setHostAddress(InetAddress.getByName("localhost"));
//設定openfire伺服器名稱
config.setXmppDomain("localhost");
//設定埠號:預設5222
config.setPort(5222);
//禁用SSL連線
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).setCompressionEnabled(false);
// config.setDebuggerEnabled(true);
//設定離線狀態
config.setSendPresence(false);
//需要經過同意才可以新增好友
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
// config.setUsernameAndPassword(“t1”,“t2”);
connection = new XMPPTCPConnection(config.build());
connection.connect();
// connection.login();
System.out.println("成功連結上tigase 伺服器");
} catch (Exception e) {
// Logger.d(“openfire連線失敗,請檢查是否開啟伺服器!”);
e.printStackTrace();
}
return connection;
}
這一位兄弟也是:
Smack4.20實現連線登陸(1)
好了,參考上面的來寫程式碼:
package main.im_tester;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import java.net.InetAddress;
public class FirstTest {
public static void main(String[] args) throws Exception {
main3();
}
static void main1() throws Exception {
// Create a connection to the jabber.org server on a specific port.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("username", "password")
.setXmppDomain("localhost")
.setHost("localhost")
.setPort(5222)
.build();
// org.xmlpull.v1.
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
conn2.connect().login();
}
static void main2() throws Exception {
// Create a connection to the jabber.org server.
// AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password" ,"localhost");
// conn1.connect();
//
//// Create a connection to the jabber.org server on a specific port.
// XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
// .setUsernameAndPassword("username", "password")
// .setServiceName("localhost")
// .setHost("localhost")
// .setPort(5222)
// .build();
//
// AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
// conn2.connect();
// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
// configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setResource("SomeResource");
configBuilder.setXmppDomain("localhost");
//--這個比較重要,假如不設定的話是預設開啟加密傳輸的,到時候會有錯誤:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path buildin
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
connection.connect();
// Log into the server
// connection.login();
// Disconnect from the server
connection.disconnect();
}
public static void main3(){
XMPPConnection connection=createConnection();
((XMPPTCPConnection) connection).disconnect();
}
public static XMPPTCPConnection createConnection() {
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
XMPPTCPConnection connection=null;
try {
//設定openfire主機IP
config.setHostAddress(InetAddress.getByName("localhost"));
//設定openfire伺服器名稱
config.setXmppDomain("localhost");
//設定埠號:預設5222
config.setPort(5222);
//禁用SSL連線
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).setCompressionEnabled(false);
// config.setDebuggerEnabled(true);
//設定離線狀態
config.setSendPresence(false);
//需要經過同意才可以新增好友
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
// config.setUsernameAndPassword("t1","t2");
connection = new XMPPTCPConnection(config.build());
connection.connect();
// connection.login();
System.out.println("成功連結上tigase 伺服器");
} catch (Exception e) {
// Logger.d("openfire連線失敗,請檢查是否開啟伺服器!");
e.printStackTrace();
}
return connection;
}
}
測試:
終於可以連線上了。
順便,gradle也變更一下:
plugins {
id 'java'
}
group 'net.w2p'
version '1.0-SNAPSHOT'
/***所有專案共通***/
allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
idea {
module {
inheritOutputDirs = true
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
repositories {
maven{
//更換為阿里的倉庫
url 'http://maven.aliyun.com/nexus/content/groups/public'
}
//有些jar包在中央倉庫是沒有的,需要手動新增上去
// flatDir { dirs 'local_jars' }
// mavenCentral()
}
}
dependencies {
/*
--- 注意,這裡不要直接下載jar來依賴了。用gradle上面的依賴。
compile fileTree(dir:'libs/smack_4_3_1/libs',includes:['*jar'])
compile 'org.jxmpp:jxmpp-core:0.6.3'
compile 'org.jxmpp:jxmpp-jid:0.6.3'
compile 'xmlpull:xmlpull:1.1.3.4a'
compile 'net.sf.kxml:kxml2:2.3.0'
compile 'org.minidns:minidns-hla:0.3.3'
compile 'org.minidns:minidns-core:0.3.3'
// compile 'org.xbill:dns:2.0.8'
compile 'dnsjava:dnsjava:2.1.8'
compile 'org.apache.httpcomponents:httpclient:4.5.6'
testCompile fileTree(dir:'libs/smack_4_3_1/libs',includes:['*jar'])
*/
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "org.igniterealtime.smack:smack-core:4.3.1"
compile "org.igniterealtime.smack:smack-tcp:4.3.1"
compile "org.igniterealtime.smack:smack-im:4.3.1"
}