MessageQueue in Unity3D

using UnityEngine;
using System.Collections;




public class MessageService : MonoBehaviour {

//This object just can be construct one time.
private static MessageService __instance = null;
public MessageService()
{
if( null != __instance )
Debug.LogError( "MessageService has been constructed twice, no !" );
else
__instance = this;

DontDestroyOnLoad( this );
}
public static MessageService Instance(){ return __instance; }

//The queue hold messages witch will be send to server, we need to ensure the order
private System.Collections.Generic.List<MessageBase> mMessagesPendingToSend;

//Recieve messages from server, and wait to dispatch
private System.Collections.Generic.List<MessageBase> mMessagesRecievedFromServer;

// Use this for initialization
void Start () {
Debug.Log( "MessageService has been started." );
}

// Update is called once per frame
void Update () {
ProcessMessage();
}


private void ProcessMessage()
{
//Note: When we send any message to the server, we will recieve a feedback message immediately

//loop and pop the top message in mMessagesPendingToSend
{
//CombinMessagesInSendQueue();

//SendMessagesToServer(); 
//you need format the C# version message into a url string with appropriate params.

//Recieve messages from server by the send operation: 
//you will recieve a JSON string that comminbed server messages in it.

//Parse the JSON message string into serveral MessageBase objects, and hold the in a list such as: "List<MessageBase> recievedMessages"

//Iter the messages recieved and dispatch them
{
//DispatchRecievedMessage( recievedMsg )
}
}
}

//Some message can be combin to a single message, combin them to reverse server performance.
private void  CombinMessagesInSendQueue()
{
//If we have some class like following:

/*
class GetCoinsForBuilding
{
Generric.List<long> mBuildingInstanceIDList; 
}
*/

//We can combin server same message in the queue into a single one with a combined buildingInstanceList;
}

private string SendMessagesToServer()
{
return string.Empty;
}

//Queue received message into the queue for clinent.
private void DispatchRecievedMessage( MessageBase msg )
{

}

//Client push messages witch wana be sent to server in a queue first
public void QueueMessageToServer( MessageBase msg )
{

}


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值