我的理解是一个类似 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,进入正题
目前外网有很多类似的教程文档, 但国内目前比较少, 就如同介绍Android Matrix SDK 一般。
但仍有热心同学来书写教程, 向开源开放精神点赞!
https://www.jianshu.com/p/5c445d8698a4
因为本次只着重介绍Android SDK ,
所以有需要帮助搭建服务端的同学可以加我微信 chason_win
因 Android Riot 示例程序编译时, 很容易会因为依赖包下不来 导致编译失败,如:
org.jitsi.react:jitsi-meet-sdk:2.2.2
即使该包侥幸下成功,但仍有问题
com.facebook.react.modules.core.PermissionListener;
会爆出该类找不到, 经检查发现需要一个
react-native 的依赖,而且版本不能太低,后续我会放出来让大家下载,着急可以加我微信,在下面有写。
因 Android Matrix SDK 身在外网,所以在下载依赖的时候, 有一个包实测是下不下来的!
react-native-webrtc-1.69.2-jitsi-2062090@aar
所以我们可以手动下载jar包,集成到项目中去, 后续我会放出来,急用可以联系我微信,下面有写。
如果你已经看到这里, 说明准备工作已经完成,挺复杂的吧? 我当时搞了一天, 各种办法都尝试过,你们算幸运的咯!
个人感觉这个应该是放在注册后面讲的, 但是因为他相对于注册来说极其简单, 所以让大家先学习这个,有点成就感, 不然一上来就摸索注册半天,有点难受。
先上代码:
/*初始化*/
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接口
重头戏来咯,我歇会先…
高能预警
此次注册需要调用四次接口,三个不同接口!
直接看代码吧!
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