Matrix Riot 介紹
我的理解是一個類似 Email 分佈式的開源聊天工具:https://matrix.org/ ,可以用免費服務、線上付費服務(可以綁定私有域名),也可以自己在家搭建。
跟 XMPP 類似,不過 XMPP 不爭氣,真正好用的客戶端很少,而且除文字圖片之外的高級功能相互兼容不好。
matrix 目前基於 json + HTTP (RESTful),支持 羣組端到端加密。自建服務器也很簡單。
android, ios 和 瀏覽器 都用叫做 riot 的軟件(也存在其他客戶端)。線上試用 https://riot.im https://matrixim.cc/ 等等(我找的這兩個不FQ可能比較慢。)
matrix 的協議轉換插件也比較全,可以跟別家的 IRC XMPP Email 等等互通。
matrix 目前比較火,有機會取代商業閉源軟件,希望不要跟當年 xmpp 一樣。
其實綜上所述,簡單的介紹了下我對Matrix的理解,而我們所關心的,則是Matrix SDK 的使用, ok,進入正題
Matrix Riot 服務端
目前外網有很多類似的教程文檔, 但國內目前比較少, 就如同介紹Android Matrix SDK 一般。
但仍有熱心同學來書寫教程, 向開源開放精神點贊!
https://www.jianshu.com/p/5c445d8698a4
因爲本次只着重介紹Android SDK ,
所以有需要幫助搭建服務端的同學可以加我微信 chason_win
Matrix Riot 的重要提醒!
Android Matrix Riot 示例程序編譯
因 Android Riot 示例程序編譯時, 很容易會因爲依賴包下不來 導致編譯失敗,如:
org.jitsi.react:jitsi-meet-sdk:2.2.2
即使該包僥倖下成功,但仍有問題
com.facebook.react.modules.core.PermissionListener;
會爆出該類找不到, 經檢查發現需要一個
react-native 的依賴,而且版本不能太低,後續我會放出來讓大家下載,着急可以加我微信,在下面有寫。
Android Matrix SDK Module 編譯
因 Android Matrix SDK 身在外網,所以在下載依賴的時候, 有一個包實測是下不下來的!
react-native-webrtc-1.69.2-jitsi-2062090@aar
所以我們可以手動下載jar包,集成到項目中去, 後續我會放出來,急用可以聯繫我微信,下面有寫。
Android Matrix Riot 登錄
如果你已經看到這裏, 說明準備工作已經完成,挺複雜的吧? 我當時搞了一天, 各種辦法都嘗試過,你們算幸運的咯!
個人感覺這個應該是放在註冊後面講的, 但是因爲他相對於註冊來說極其簡單, 所以讓大家先學習這個,有點成就感, 不然一上來就摸索註冊半天,有點難受。
先上代碼:
/*初始化*/
hsConfig = new HomeServerConnectionConfig.Builder()
.withHomeServerUri(Uri.parse(url))
.build();
loginRestClient = new LoginRestClient(hsConfig);
/**
* 使用賬號密碼進行登錄
* @param userName
* @param password
*/
private static void login(String userName,String password){
loginRestClient.loginWithUser(userName, password, new ApiCallback<Credentials>() {
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
@Override
public void onSuccess(Credentials info) {
}
});
利用你事先註冊好的賬號,登錄即可,成功後會回調onSuccess接口
重頭戲來咯,我歇會先…
Android Matrix Riot 註冊
高能預警
此次註冊需要調用四次接口,三個不同接口!
直接看代碼吧!
public static void register() {
RegistrationParams params = new RegistrationParams();
params.username = "dazhaung01";
loginRestClient.register(params, new ApiCallback<Credentials>() {
@Override
public void onNetworkError(Exception e) {
Log.e(TAG, "onNetworkError: " + e.getMessage());
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(TAG, "onMatrixError: " + e.toString());
Log.e(TAG, "onMatrixError errcode: " + e.errcode);
Log.e(TAG, "onMatrixError mStatus: " + e.mStatus);
if (TextUtils.equals(e.errcode, MatrixError.USER_IN_USE)) {
Log.d(TAG, "User name is used");
} else if (TextUtils.equals(e.errcode, MatrixError.UNAUTHORIZED)) {
Log.d(TAG, "UNAUTHORIZED");
} else if (null != e.mStatus && e.mStatus == 401) {
try {
if (registrationFlowResponse != null) {
Log.d(TAG, "registrationFlowResponse !=null ");
return;
}
registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
//檢查郵箱
doesServerRequireIdentityServerParam();
Log.d(TAG, "registrationFlowResponse ");
} catch (Exception castExcept) {
Log.e(TAG, "JsonUtils.toRegistrationFlowResponse " + castExcept.getLocalizedMessage(), castExcept);
}
// listener.onRegistrationFailed(ERROR_MISSING_STAGE);
// handler.sendEmptyMessage(1);
Log.d(TAG, "ERROR_MISSING_STAGE ");
} else if (TextUtils.equals(e.errcode, MatrixError.RESOURCE_LIMIT_EXCEEDED)) {
// listener.onResourceLimitExceeded(e);
Log.d(TAG, "onResourceLimitExceeded ");
} else {
// listener.onRegistrationFailed("");
Log.d(TAG, "onRegistrationFailed ");
}
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(TAG, "onUnexpectedError: " + e.getMessage());
}
@Override
public void onSuccess(Credentials info) {
Log.e(TAG, "onSuccess: " + info.toString());
}
});
}
private static void doesServerRequireIdentityServerParam() {
loginRestClient.doesServerRequireIdentityServerParam(new ApiCallback<Boolean>() {
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
@Override
public void onSuccess(Boolean info) {
Log.e(TAG, "onSuccess info : " + info);
requestEmailValidationToken();
}
});
}
private static void requestEmailValidationToken(){
profileRestClient.requestEmailValidationToken(
"null",
"你的郵箱地址@qq.com",
"72916032-3204-4f9b-9602-72e136c7c3c6",
0,
/*nextLink*/null,
true,
new ApiCallback<RequestEmailValidationResponse>() {
@Override
public void onSuccess(RequestEmailValidationResponse info) {
clientSecret=info.clientSecret;
email=info.email;
sid=info.sid;
sendAttempt=info.sendAttempt;
Log.e(TAG, "onSuccess: " );
}
@Override
public void onNetworkError(final Exception e) {
Log.e(TAG, "onNetworkError: " );
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(TAG, "onUnexpectedError: " );
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(TAG, "onMatrixError: " );
// String errorMessage = null;
// if (TextUtils.equals(MatrixError.THREEPID_IN_USE, e.errcode)) {
// errorMessage = build3PidErrorMessage(context, R.string.account_email_already_used_error, null);
// } else {
// errorMessage = build3PidErrorMessage(context, R.string.account_email_error, e.getLocalizedMessage());
// }
//
// listener.onThreePidRequestFailed(errorMessage);
}
});
}
public static void registerRight() {
//--data $'
// {"auth":{"threepid_creds":
// {"client_secret":"359cef9b-f9c8-4359-b074-f84a7ed0b2e6",
// "sid":"1824741622"},
// "session":"KXoarSJvqbkvEtVCjEXorIPf",
// "type":"m.login.email.identity"},
// "initial_device_display_name":"移動設備",
// "password":"111111","username":"dazhuang8"}
// ' -H "User-Agent: Riot.im/0.9.13-dev(Linux; U; Android 7.0; SM-G9508 Build/NRD90; Flavour GooglePlay; MatrixAndroidSDK 0.9.36-dev)" '
// http://synapse-dev.gidepay.com/_matrix/client/r0/register' | python -m json.tool
RegistrationParams params = new RegistrationParams();
params.username = "dazhaung01";
params.password = "111111";
AuthParams authParams = getThreePidAuthParams(clientSecret, "",
sid, LoginRestClient.LOGIN_FLOW_TYPE_EMAIL_IDENTITY);
authParams.session=registrationFlowResponse.session;
params.auth=authParams;
loginRestClient.register(params, new ApiCallback<Credentials>() {
@Override
public void onNetworkError(Exception e) {
Log.e(TAG, "onNetworkError: " + e.getMessage());
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(TAG, "onMatrixError: " + e.toString());
Log.e(TAG, "onMatrixError errcode: " + e.errcode);
Log.e(TAG, "onMatrixError mStatus: " + e.mStatus);
if (TextUtils.equals(e.errcode, MatrixError.USER_IN_USE)) {
Log.d(TAG, "User name is used");
} else if (TextUtils.equals(e.errcode, MatrixError.UNAUTHORIZED)) {
Log.d(TAG, "電子郵件還沒有進行確認");
} else if (null != e.mStatus && e.mStatus == 401) {
Log.d(TAG, "mStatus");
try {
if (registrationFlowResponse != null) {
Log.d(TAG, "registrationFlowResponse !=null ");
return;
}
registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
//檢查郵箱
// doesServerRequireIdentityServerParam();
Log.d(TAG, "registrationFlowResponse ");
} catch (Exception castExcept) {
Log.e(TAG, "JsonUtils.toRegistrationFlowResponse " + castExcept.getLocalizedMessage(), castExcept);
}
// listener.onRegistrationFailed(ERROR_MISSING_STAGE);
// handler.sendEmptyMessage(1);
Log.d(TAG, "ERROR_MISSING_STAGE ");
} else if (TextUtils.equals(e.errcode, MatrixError.RESOURCE_LIMIT_EXCEEDED)) {
// listener.onResourceLimitExceeded(e);
Log.d(TAG, "onResourceLimitExceeded ");
} else {
// listener.onRegistrationFailed("");
Log.d(TAG, "onRegistrationFailed ");
}
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(TAG, "onUnexpectedError: " + e.getMessage());
}
@Override
public void onSuccess(Credentials info) {
Log.e(TAG, " 註冊成功 onSuccess: " + info.toString());
}
});
}
/**
* Format three pid params for registration request
*
* @param clientSecret
* @param host
* @param sid received by requestToken request
* @param medium type of three pid
* @return map of params
*/
private static AuthParams getThreePidAuthParams(final String clientSecret,
final String host,
final String sid,
final String medium) {
AuthParamsThreePid authParams = new AuthParamsThreePid(medium);
authParams.threePidCredentials.clientSecret = clientSecret;
authParams.threePidCredentials.idServer = host;
authParams.threePidCredentials.sid = sid;
return authParams;
}
啊~舒服
你不舒服加我微信,我來幫你舒服
chason_win