C(※)のEvent類.nodejsのEventスタイル

1481 ワード

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18408
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using LitJson;

namespace TSGame
{
	public class TSEvent
	{
		public delegate void EventHandler(JsonData data);
		static public TSEvent singleTon = new TSEvent();

		public Dictionary<string, EventHandler> eventMap = new Dictionary<string, EventHandler>();

		public TSEvent ()
		{

		}

		public void RegistEvent(string msg, EventHandler eh)
		{
			if (eventMap.ContainsKey(msg))
			{
				eventMap[msg] += eh;
			}
			else
			{
				eventMap.Add(msg, eh);
			}
		}

		public void RemoveRegistEvent(string msg, EventHandler eh)
		{
			if (eventMap.ContainsKey(msg))
			{
				eventMap[msg] -= eh;
			}
		}

		public void SendMessage(string msg, JsonData json)
		{
			eventMap[msg].Invoke(json);
		}
	}
}


 
 

用起来非常简单. 比方要监听回来的C_GetGW消息.那么就下面这写法.nodejs的 风格.

TSEvent.singleTon.RegistEvent("C_GetGW",        );
メッセージを送ります 
TSEvent.singleTon.SendMessage("C_GetGW", JSON  );