private static void notify(string typeName, string methodName, UTodoJsonObject jsonObject) { Debug.Log("client notify : " + typeName + "." + methodName); Type type = Type.GetType(typeName, true, true); if (null == type) { throw new NullReferenceException(typeName + "reflect class failed.Type if null."); } object obj = GetInstance(typeName); if (null == obj) { obj = Activator.CreateInstance(type); instances.Add(typeName, obj); } MethodInfo method = type.GetMethod(methodName); BindingFlags flags = BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public; object[] parameters = { jsonObject }; if (null == method) { throw new NullReferenceException(); } method.Invoke(obj, flags, Type.DefaultBinder, parameters, null); }
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 handleObserver(string router, UTodoJsonObject jsonObject) { var clients = observers[router]; foreach (var client in clients) { client.ClientSocket.Send(UTodoMessage.PackMessage(jsonObject)); //todo error } }
public static void ReadMessage(string content, Action <UTodoJsonObject> callback) { // "type.classname.methodname|data" 数据格式 string[] routeAndData = content.Split('|'); string route = routeAndData[0]; string data = routeAndData[1]; UTodoJsonObject jsonObject = JsonUtility.FromJson <UTodoJsonObject>(data); callback(jsonObject); }
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); }
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; } }
public void notify(string route, UTodoJsonObject jsonObject) { var data = UTodoMessage.PackMessage("notify." + route, jsonObject); clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, SendCallback, null); }
public void request(string route, UTodoJsonObject jsonObject, Action <UTodoJsonObject> callback) { var data = UTodoMessage.PackMessage("request." + route, jsonObject); clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, SendCallback, callback); }
public static byte[] PackMessage(string router, UTodoJsonObject jsonObject) { string data = router + "|" + JsonUtility.ToJson(jsonObject) + "<EOF>"; return(Encoding.ASCII.GetBytes(data)); }
public static byte[] PackMessage(UTodoJsonObject jsonObject) // 打包数据并返回给客户端 { string data = "|" + JsonUtility.ToJson(jsonObject) + "<EOF>"; return(Encoding.ASCII.GetBytes(data)); }