Skip to content

Tutorial: ChatSystem

sl-sandy edited this page May 20, 2020 · 12 revisions

The goal of this tutorial is to show how to process the message that comes from the client-side and how to send a message to players who in the same room.


1. First of all, we need to define the message-id for communicating between client-side and game server before we start (Now NF has done this for you.)

{ServerProject}/NFComm/NFMessageDefine/NFDefine.proto

	REQ_CHAT								= 350;
	ACK_CHAT								= 351;

2. Besides, we need to define a message structure to describe what is that message's content (Now NF has done this for you.)

{ServerProject}/NFComm/NFMessageDefine/NFMsgShare.proto

message ReqAckPlayerChat
{
	enum EGameChatChannel
	{
		EGCC_GLOBAL = 0;
		EGCC_CLAN = 1;
		EGCC_FRIEND = 2;
		EGCC_BATTLE = 3;
		EGCC_TEAM = 4;
		EGCC_ROOM = 5;
	}
	enum EGameChatType
	{
		EGCT_TEXT= 0;
		EGCT_VOICE = 1;
		EGCT_EMOJI = 2;
		EGCT_DONATE_HERO = 10;
		EGCT_DONATE_BUILDING = 11;
		EGCT_DONATE_ITEM = 12;
	}

	Ident	player_id = 1;
	bytes 	player_name = 2;
	bytes 	player_hero_id = 3;
	bytes 	player_hero_level = 4;
	EGameChatChannel  chat_channel = 5;
	EGameChatType  chat_type = 6;
	bytes chat_info = 7;
	Ident	target_id = 8;
}

3. In addition, we need to generate C++ code and C# code from IDL(Now protocol-buf)

  • Run the script "NFComm/NFMessageDefine/cpp.bat" and "NFComm/NFMessageDefine/cs.bat" (cpp.sh cs.sh) to generate the source code
  • Copy the source code files from {ServerProject}/_Out/NFDataCfg/client/ to the folder {ClientProject}/Assets/Resources/NFDataCfg/

4. Equally important, add a Plugin and a module for the chat system. (Now NF has done this: {ServerProject}/NFExamples/NFChatPlugin)


5. Last but not least, add an observer(or call it as a watchdog?) for that message-id we defined at step 1

bool NFChatModule::AfterInit()
{
	m_pNetModule->AddReceiveCallBack(NFMsg::REQ_CHAT, this, &NFChatModule::OnClientChatProcess);

	return true;
}

void NFChatModule::OnClientChatProcess(const NFSOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	CLIENT_MSG_PROCESS( nMsgID, msg, nLen, NFMsg::ReqAckPlayerChat);

	switch (xMsg.chat_channel())
	{
	case NFMsg::ReqAckPlayerChat::EGCC_GLOBAL:
	{
		//this code means the game server will sends a message to all players who playing game
		m_pNetModule->SendMsgPBToAllClient(NFMsg::ACK_CHAT, xMsg);
	}
	break;
	case NFMsg::ReqAckPlayerChat::EGCC_ROOM:
	{
		const int sceneID = m_pKernelModule->GetPropertyInt(nPlayerID, NFrame::Player::SceneID());
		const int groupID = m_pKernelModule->GetPropertyInt(nPlayerID, NFrame::Player::GroupID());

		//this code means the game server will sends a message to all players who in the same room
		m_pGameServerNet_ServerModule->SendGroupMsgPBToGate(NFMsg::ACK_CHAT, xMsg, sceneID, groupID);
	}
	break;
	default:
	{
		//this code means the game server will sends a message yourself(nPlayerID)
		m_pGameServerNet_ServerModule->SendMsgPBToGate(NFMsg::ACK_CHAT, xMsg, nPlayerID);
	}
	break;;
	}
}

That's all.

Clone this wiki locally