// 取消注册 public static void unRegister(string router, UTodoServerClient client) { if (observers.ContainsKey(router)) { observers[router].Remove(client); } }
public static void route(string route, UTodoJsonObject jsonObject, UTodoServerClient client) { string[] routes = route.Split('.'); string type = routes[0]; string typeName = routes[1]; string methodName = routes[2]; routeType(type, typeName, methodName, jsonObject, client); }
public static void ReadMessage(string content, UTodoServerClient client) // 包含路由和数据 和客户端定制相同的规则 { // "type.classname.methodname|data" 数据格式 string[] routeAndData = content.Split('|'); string route = routeAndData[0]; string data = routeAndData[1].Replace("<EOF>", ""); UTodoJsonObject jsonObject = JsonUtility.FromJson <UTodoJsonObject>(data); UTodoRouter.route(route, jsonObject, client); }
// 注册 public static void register(string router, UTodoServerClient client) { if (observers.ContainsKey(router)) { observers[router].Add(client); } else { observers.Add(router, new List <UTodoServerClient>() { client }); } }
public void Remove(UTodoServerClient client) { clients.Remove(client); }
public void Add(UTodoServerClient client) { clients.Add(client); }
private static void on(string typeName, string methodName, UTodoServerClient client) { Debug.Log("client on : " + typeName + "." + methodName); UTodoObserver.register(typeName + "." + methodName, client); }
private static void routeType(string type, string typeName, string methodName, UTodoJsonObject jsonObject, UTodoServerClient client) { switch (type) { case "request": // 要求即时返回数据 request(typeName, methodName, jsonObject); break; case "notify": // 无需返回数据 notify(typeName, methodName, jsonObject); break; case "on": // 客户端监听服务器事件 on(typeName, methodName, client); break; } }