Development Example

Update time: 2021/08/20 11:00:07

Development Example

#include "stdafx.h"
#include <windows.h>
#include <cassert>
#include <iostream>
#include <string>
#include "json/json.h"
#include "nim_res_code_def.h"
#include "nim_client_def.h"

//Initialize and uninstall
typedef bool (*nim_client_init)(const char *app_data_dir, const char *app_install_dir, const char *json_extension);
typedef void (*nim_client_cleanup)(const char *json_extension);

//Log in and log out
typedef void (*nim_client_login)(const char *app_key, const char *account, const char *password, const char *json_extension, nim_json_transport_cb_func cb, const void *user_data);
typedef void (*nim_client_logout)(NIMLogoutType logout_type, const char *json_extension, nim_json_transport_cb_func cb, const void *user_data);

//Load module and interface
class SdkNim
{
public:
	template <typename F>
	static F Function(const char* function_name)
	{
		F f = (F) ::GetProcAddress(nim_sdk_, function_name);
		assert(f);
		return f;
	}
public:
	static bool Load()
	{
		nim_sdk_ = ::LoadLibraryW(L"nim_sdk_dll.dll");
		if( nim_sdk_ == NULL )
		{
			wprintf_s(L"Load nim_sdk_dll.dll Fail: %d", ::GetLastError());
			return false;
		}
		else
		{
			return true;
		}
	}
	static void UnLoad()
	{
		assert(nim_sdk_ != NULL);
		if( nim_sdk_ != NULL )
		{
			::FreeLibrary(nim_sdk_);
			nim_sdk_ = NULL;
		}
	}
	static bool Init()
	{
		nim_client_init f_init = Function<nim_client_init>("nim_client_init");
		return f_init("Netease", "", "");
	}
	static void UnInit()
	{
		assert(nim_sdk_ != NULL);
		if( nim_sdk_ != NULL )
		{
			nim_client_cleanup f_cleanup = Function<nim_client_cleanup>("nim_client_cleanup");
			f_cleanup("");
		}
	}
private:
	static HINSTANCE nim_sdk_;
};

HINSTANCE SdkNim::nim_sdk_ = NULL;

//Set simple tag
bool wait_for_login = false, wait_for_logout = false;
bool login_success = false;

//Callback function
void OnLoginCallback(const char *json_params, const void *user_data)
{
	printf_s("OnLoginCallback:%s", json_params);

	Json::Value json;
	Json::Reader reader;
	if( reader.parse(json_params, json) )
	{
		int code = json[kNIMErrorCode].asInt();
		int step = json[kNIMLoginStep].asInt();
		if( code == kNIMResSuccess )
		{
			if( step == kNIMLoginStepLogin )
			{
				login_success = true;
				wait_for_login = false;
			}
		}
		else
		{
			wait_for_login = false;
		}
	}
	else
	{
		assert(0);
		wait_for_login = false;
	}
}

void OnLogoutCallback(const char *json_params, const void *user_data)
{
	printf_s("OnLogoutCallback:%s", json_params);

	wait_for_logout = false;
}

//Log in
void NimLogin()
{
	nim_client_login f_login = SdkNim::Function<nim_client_login>("nim_client_login");
	f_login("app_key", "account", "password", "", &OnLoginCallback, NULL);
}

//Exit
void NimLogout()
{
	nim_client_logout f_logout = SdkNim::Function<nim_client_logout>("nim_client_logout");
	f_logout(kNIMLogoutAppExit, "", &OnLogoutCallback, NULL);
}

int main(int, char**)
{
	if( SdkNim::Load() )
	{
		if( SdkNim::Init() )
		{
			wait_for_login = true;
			NimLogin();

			while( wait_for_login )
			{
				::Sleep(1000);
			}

			if( login_success )
			{
				::Sleep(3000);

				wait_for_logout = true;
				NimLogout();

				while( wait_for_logout )
				{
					::Sleep(1000);
				}
			}

			SdkNim::UnInit();
		}
		else
		{
			wprintf_s(L"SdkNim::Init Fail");
		}
		SdkNim::UnLoad();
	}
	system("pause");
	return 0;
}

C++ SDK development manual

C# SDK development manual

Was this page helpful?
Yes
No
  • Development Example
  • C++ SDK development manual
  • C# SDK development manual