C#的Event类. nodejs的Event风格
- //------------------------------------------------------------------------------
- // <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数据);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。