Nakama C++ Client SDK 2.5.1
Client for Nakama Server
|
General C/C++ client for Nakama server.
Nakama is an open-source server designed to power modern games and apps. Features include user accounts, chat, social, matchmaker, realtime multiplayer, and much more.
This client implements the full API and socket options with the server. It's written in C and C++11 with minimal dependencies to support Cocos2d-x, Unreal and other custom engines and frameworks.
If you experience any issues with the client, it can be useful to enable debug logs (see Logging section) and open an issue.
Full documentation is online - https://heroiclabs.com/docs
You'll need to setup the server and database before you can connect with the client. The simplest way is to use Docker but have a look at the server documentation for other options.
In theory any platform that meets the requirement for cpprest
and boost
is also supported. The client is compiled with C++11.
When you've downloaded the Nakama C++ archive and extracted it to NAKAMA_CPP_SDK
folder, you should include it in your project.
We don't recommend to copy Nakama C++ SDK to your project because it's quite big in size (~1 Gb).
NAKAMA_CPP_SDK/include
in Build Settings > Header Search Paths
Build Settings > Library Search Paths
:NAKAMA_CPP_SDK/libs/ios
- for iOSNAKAMA_CPP_SDK/libs/mac
- for MacGeneral > Frameworks, Libraries, and Embedded Content
add following:.a
files located in libs folderfoundation
and security
frameworksIf you use CMake
then see Setup for CMake projects section.
If you use ndk-build
then add following to your Android.mk
file:
For most NativeActivity projects, if you have an entry point like:
Add include:
Add the following code at the top of the android_main
function:
Don't be afraid that nakama shared library are near 100 Mb in size. After building final apk it will be just few Mb.
Android uses a permissions system which determines which platform services the application will request to use and ask permission for from the user. The client uses the network to communicate with the server so you must add the "INTERNET" permission.
To link Nakama's static lib add following to your CMakeLists.txt
file:
To link Nakama's shared lib add following to your CMakeLists.txt
file:
In Project Settings
add following:
NAKAMA_CPP_SDK/include
in C/C++ > General > Additional Include Directories
Linker > General > Additional Library Directories
:NAKAMA_CPP_SDK/libs/win32/v140
- for VS 2015 x86NAKAMA_CPP_SDK/libs/win64/v140
- for VS 2015 x64NAKAMA_CPP_SDK/libs/win32/v141
- for VS 2017 x86NAKAMA_CPP_SDK/libs/win64/v141
- for VS 2017 x64NAKAMA_CPP_SDK/libs/win32/v142
- for VS 2019 x86NAKAMA_CPP_SDK/libs/win64/v142
- for VS 2019 x64.lib
files in your Debug libs folder to Linker > Input > Additional Dependencies
for your Debug configuration. Do the same for your Release libs and configuration.NLOGS_ENABLED
- define it if you want to use Nakama logger. See Logging section/include
/libs/{platform}/{ABI}
Nakama C++ is designed to use in one thread only.
Include nakama header and use nakama namespace.
Include nakama header and use nakama namespace.
Include following header only once in some source file e.g. in main.cpp
:
This header includes implementation of Nakama C++ wrapper. It uses C interface to communicate with Nakama shared library (DLL).
The client object has many methods to execute various features in the server or open realtime socket connections with the server.
Use the connection credentials to build a client object.
The createDefaultClient
will create HTTP/1.1 client to use REST API.
The tick
method pumps requests queue and executes callbacks in your thread. You must call it periodically (recommended every 50ms) in your thread.
Without this the default client and realtime client will not work, and you will not receive responses from the server.
There's a variety of ways to authenticate with the server. Authentication can create a user if they don't already exist with those credentials. It's also easy to authenticate with a social profile from Google Play Games, Facebook, Game Center, etc.
When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a NSession
object.
It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.
The client includes lots of builtin APIs for various features of the game server. These can be accessed with the async methods. It can also call custom logic as RPC functions on the server. These can also be executed with a socket object.
All requests are sent with a session object which authorizes the client.
The client can create one or more realtime clients with the server. Each realtime client can have it's own events listener registered for responses received from the server.
Don't forget to call tick
method. See Tick section for details.
Client logging is off by default.
To enable logs output to console with debug logging level:
To enable logs output to custom sink with debug logging level:
To log string with debug logging level:
formatted log:
Changing logging level boundary:
NLogger
behaviour depending on logging level boundary:
Debug
writes all logs.Info
writes logs with Info
, Warn
, Error
and Fatal
logging level.Warn
writes logs with Warn
, Error
and Fatal
logging level.Error
writes logs with Error
and Fatal
logging level.Fatal
writes only logs with Fatal
logging level.Note: to use logging macroses you have to define NLOGS_ENABLED
.
Nakama C++ client has built-in support for WebSocket. This is available on all supported platforms.
Client will default to use the Websocket transport provided by C++ REST SDK.
You can use a custom Websocket transport by implementing the NRtTransportInterface:
For more code examples, have a look at:
Built-in websocket transport supports "Activity timeout" feature - if no any message received from server during "Activity timeout" then connection will be closed. Set 0 to disable this feature (default value).
You can change ping period on server - ping_period_ms
parameter:
https://heroiclabs.com/docs/install-configuration/#socket
The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested to enhance the code please open an issue to discuss the changes or drop in and discuss it in the community forum.
Clone the Nakama C++ repository:
git clone --recurse-submodules -j8 git://github.com/heroiclabs/nakama-cpp.git
To update all submodules:
git submodule update --init --recursive
Change submodule branch:
.gitmodules
git submodule update --remote
Third party libraries:
BOOST_ROOT
system variable. Used by cpprest
library on Windows, Mac and Linux.Build configuration is located here:
In the build configuration you can enable components depending on your needs.
There are following components:
In the config you can also set whenever you need to build nakama-cpp
as static, dynamic library or both version.
Where Mode
is build mode: Debug
or Release
Where Arch
is architecture: x86
or x64
Where Toolset
is platform toolset:
v140
- Visual Studio 2015v141
- Visual Studio 2017v142
- Visual Studio 2019--dll
is optional parameter. If set then Nakama will be built as DLL otherwise as static library.
It builds and copies nakama lib to release folder.
To build for all modes, architectures and toolsets:
Prerequisites:
Build:
--dylib
is optional parameter. If set then Nakama will be built as dynamic library otherwise as static.
It builds in Release
mode and copies nakama lib to release folder.
To build for one architecture:
Where Arch
is architecture: arm64
, armv7
, armv7s
or x86_64
.
--dylib
is optional parameter. If set then Nakama will be built as dynamic library otherwise as static.
It builds in Release
mode.
To build for all architectures arm64
, armv7
, armv7s
and x86_64
:
It builds in Release
mode, creates universal libraries and copies them to release folder.
We use Ubuntu 18.04 amd64.
Prerequisites:
To automatically install all dependencies call:
It will install all needed packages, will download and build boost and CMake.
Execute command which will be printed at end.
To manually install dependencies:
sudo apt-get install build-essential autoconf libtool pkg-config
sudo apt-get install libgflags-dev libgtest-dev
sudo apt-get install clang libc++-dev golang perl
download boost
1.69 sources and build them:
./bootstrap.sh --with-libraries=system,chrono,thread
./b2
set BOOST_ROOT
env var to boost
folder:
export BOOST_ROOT={path to boost}
download CMake
3.15+ sources and build them:
./bootstrap && make && make install
To build as static or shared object library:
--so
is optional parameter. If set then Nakama will be built as shared library otherwise as static.
It builds in Release
mode and copies nakama lib to release folder.
To build both static and shared object library (see Build Configuration):
Currently buid for Android is supported on Mac OS and Linux.
Set ANDROID_NDK
or NDK_ROOT
system variable to Android NDK folder.
To build for one ABI:
Where ABI
is Android ABI e.g. armeabi-v7a
, arm64-v8a
, x86
or x86_64
--so
is optional parameter. If set then Nakama will be built as shared library otherwise as static.
It builds for Andoid API level 16 in Release
mode.
To build for all ABIs armeabi-v7a
, arm64-v8a
, x86
and x86_64
:
Tests are built when you build Nakama C++ SDK for desktop OS (Windows, Mac or Linux).
By default tests connect to local server by 127.0.0.1
. To use another IP of your server, edit test/test_serverConfig.h
file.
Tests require lua modules: download them from https://github.com/heroiclabs/nakama/tree/master/data/modules and put to <nakama-server>/data/modules
.
Restart server.
Run tests executable (console application):
You can find the C++ Client example here
Please follow README-C
API docs are generated with Doxygen and deployed to GitHub pages.
When changing the API comments, rerun Doxygen and commit the changes in docs/*
.
To run Doxygen:
This project is licensed under the Apache-2 License.
Thanks to @dimon4eg for this excellent support on developing Nakama C/C++, Cocos2d-x and Unreal client libraries.