Windows/macOS

开发示例

更新时间: 2022/09/27 03:09:56

开发示例

	#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"

	//初始化和卸载
	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);

	//登录和退出
	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);

	//加载模块和接口
	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;
	
	//设置简单的标记
	bool wait_for_login = false, wait_for_logout = false;
	bool login_success = false;

	//回调函数
	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;
	}

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

	//退出
	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开发手册

C# SDK开发手册

此文档是否对你有帮助?
有帮助
去反馈
  • 开发示例
  • C++ SDK开发手册
  • C# SDK开发手册