Firebase SDK是一个游戏开发工具类合集 ,我只用到了 Auth(身份验证) DataBase (实时数据库)这2个功能,这个数据库内部采用socket连接,查询和同步数据比较快,经过测试并发性也很好,而且性价比很高,价格比较便宜。由于要同步的数据是基于cocos2dx的,所以我采用了c++版本,由于Firebase没有针对Cocos2dx的sdk版本,所以Firebase 踩坑比较多,另外,Firebase 采用标准的c++11,大家如果接入c++版本,c++语言基本功要过关。
Firebase首先要申请Google 账号,然后根据文档进行后台配置。
Firebase c++ SDK 文档 (需要科学上网),里面有下载sdk的连接
https://firebase.google.com/docs/cpp/setup?authuser=0
1:将下载好的sdk 放入工程根目录 Libs/firebase_cpp_sdk 这一步很重要
2:将从firebase 后台申请的 google-services.json 放入proj.android/app 目录下
3:在build.gradle中添加
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':libcocos2dx')
//firebase start
//5.1.1
compile 'com.google.firebase:firebase-auth:15.0.0'
compile 'com.google.firebase:firebase-database:15.0.0'
compile 'com.google.android.gms:play-services-base:15.0.0'
//firebase end
}
4:在Android.mk中添加,这个是NDK 查找c++so库
#FIREBASE_BEGIN
STL := $(firstword $(subst _, ,$(APP_STL)))
FIREBASE_CPP_SDK_DIR := ../../../Libs/firebase_cpp_sdk
FIREBASE_LIBRARY_PATH := $(FIREBASE_CPP_SDK_DIR)/libs/android/$(TARGET_ARCH_ABI)/$(STL)
include $(CLEAR_VARS)
LOCAL_MODULE := firebase_app
LOCAL_SRC_FILES := $(FIREBASE_LIBRARY_PATH)/libfirebase_app.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FIREBASE_CPP_SDK_DIR)/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := firebase_auth
LOCAL_SRC_FILES := $(FIREBASE_LIBRARY_PATH)/libfirebase_auth.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FIREBASE_CPP_SDK_DIR)/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := firebase_database
LOCAL_SRC_FILES := $(FIREBASE_LIBRARY_PATH)/libfirebase_database.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FIREBASE_CPP_SDK_DIR)/include
include $(PREBUILT_STATIC_LIBRARY)
#FIREBASE_END
#FIREBASE_BEGIN
LOCAL_STATIC_LIBRARIES += firebase_database
LOCAL_STATIC_LIBRARIES += firebase_auth
LOCAL_STATIC_LIBRARIES += firebase_app
#FIREBASE_END
5:在c++层初始化
void FirebaseManager::Init()
{
_scheduler = Director::getInstance()->getScheduler();
_scheduler->schedule(CC_SCHEDULE_SELECTOR(FirebaseManager::update), this, 0.1f, CC_REPEAT_FOREVER, 1.0f, false);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // && ANDROID_SDK_FIREBASE == true
state_ = FirebaseManager::State::kStateInitialize;
app_ = firebase::App::Create(firebase::AppOptions(), JniHelper::getEnv(), JniHelper::getActivity());
void* initialize_targets[] = {&auth_, &database_};
const firebase::ModuleInitializer::InitializerFn initializers[] = {
[](firebase::App* app, void* data) {
CCLOG("Attempt to initialize Firebase Auth.");
void** targets = reinterpret_cast<void**>(data);
firebase::InitResult result;
*reinterpret_cast<firebase::auth::Auth**>(targets[0]) =
firebase::auth::Auth::GetAuth(app, &result);
if(result == firebase::InitResult::kInitResultSuccess)
{
CCLOG("Auth kInitResultSuccess");
}
return result;
},
[](firebase::App* app, void* data) {
CCLOG("Attempt to initialize Firebase Database.");
void** targets = reinterpret_cast<void**>(data);
firebase::InitResult result;
*reinterpret_cast<firebase::database::Database**>(targets[1]) =
firebase::database::Database::GetInstance(app, &result);
if(result == firebase::InitResult::kInitResultSuccess)
{
CCLOG("Database kInitResultSuccess");
}
return result;
}};
initializer_.Initialize(firebase::App::GetInstance(), initialize_targets,
initializers,sizeof(initializers) / sizeof(initializers[0]));
#endif
}
6:friebase 登录
邮箱登录,firebase后台注册邮箱账号:
firebase::auth::Credential credential = firebase::auth::EmailAuthProvider::GetCredential(FIREBASE_EMAIL_LOGIN_NAME, FIREBASE_EMAIL_LOGIN_PASSWORD); firebase::Future<firebase::auth::User*> result = auth_->SignInWithCredential(credential);
Facebook token登录 主键是fackbook登录后的token:
std::string token = DataManager::getInstance()->getFBUserToken(); firebase::auth::Credential credential = firebase::auth::FacebookAuthProvider::GetCredential(token.c_str()); firebase::Future<firebase::auth::User*> result = auth_->SignInWithCredential(credential);
7:增删改查的功能就需要大家自己查看api文档了,firebase的数据库功能其实还是可编辑的,可以自己去后天配置查询方式,然后在代码中查询,比如下面这种方式,这个需求是从前一天获取数据 最近更新的100条,作为pvp挑战的数据:
void QueryPvpValues(const firebaseQueryCallback& callback)
{
_queryAllMapValuesCallback = callback;
firebase::database::DatabaseReference reference = this->database_->GetReference(GetRootKey().c_str());
double yesterdayTimeStamp = DataManager::getInstance()->getYesterdayTimestamp();
this->query_future_ = reference.OrderByChild("SYNC_TIMESTAMP").StartAt(yesterdayTimeStamp, "SYNC_TIMESTAMP").LimitToLast(100).GetValue();
}
最后,大家如果有海外同步数据,作为玩家存档的需求,建议选择Firebase,之前我也做了调研了一些,比如playfab, game spark,亚马逊,有的价格比较贵,有的不是很成熟,综合考虑,Frebase是首选。
如有错误,大家可以一起交流
联系方式 qq: 940299880