ClientTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using SimpleJson;
  3. using System.Threading;
  4. using Pomelo.DotNetClient;
  5. using ZeusServerEdgeJS;
  6. using CommonAI.Zone;
  7. using CommonLang;
  8. namespace ZeusServerEdgeJSTest
  9. {
  10. //class PingStat
  11. //{
  12. // public long max;
  13. // public long min;
  14. // public long total;
  15. // public long count;
  16. // public long pingStart;
  17. // public double avg;
  18. //}
  19. public class ClientTest
  20. {
  21. public static PomeloClient pc = null;
  22. private static string tocken;
  23. private static JsonObject player;
  24. private static Timer pingTimer;
  25. private static Pomelo.DotNetClient.Monitor pingMonitor = new Pomelo.DotNetClient.Monitor("ping",30);
  26. public static void loginTest(string host, int port)
  27. {
  28. pc = new PomeloClient();
  29. pc.NetWorkStateChangedEvent += (state) =>
  30. {
  31. Console.WriteLine(state);
  32. };
  33. pc.initClient(host, port, () =>
  34. {
  35. pc.connect(null, data =>
  36. {
  37. JsonObject loginRequest = new JsonObject();
  38. loginRequest["username"] = "atest1";
  39. loginRequest["password"] = "111";
  40. pc.request("gate.gateHandler.login", loginRequest, result =>
  41. {
  42. JsonObject msg = new JsonObject();
  43. msg["uid"] = result["uid"];
  44. tocken = (string)result["token"];
  45. pc.request("gate.gateHandler.queryEntry", msg, OnQuery);
  46. });
  47. // Console.WriteLine("on data back" + data.ToString());
  48. });
  49. });
  50. }
  51. public static void OnQuery(JsonObject result)
  52. {
  53. if (Convert.ToInt32(result["code"]) == 200)
  54. {
  55. pc.disconnect();
  56. string host = (string)result["host"];
  57. int port = Convert.ToInt32(result["port"]);
  58. pc = new PomeloClient();
  59. pc.NetWorkStateChangedEvent += (state) =>
  60. {
  61. Console.WriteLine(state);
  62. };
  63. pc.initClient(host, port, () =>
  64. {
  65. pc.connect(null, (data) =>
  66. {
  67. JsonObject userMessage = new JsonObject();
  68. Console.WriteLine("on connect to connector!");
  69. //Login
  70. JsonObject msg = new JsonObject();
  71. msg["token"] = tocken;
  72. pc.request("connector.entryHandler.entry", msg, OnEnter);
  73. });
  74. });
  75. }
  76. }
  77. public static void ping()
  78. {
  79. pingMonitor.start(CUtils.CurrentTimeMS);
  80. // Console.WriteLine("send Ping:" + pingStat.pingStart);
  81. JsonObject msg = new JsonObject();
  82. JsonObject pingAction = new JsonObject();
  83. pingAction["type"] = "Ping";
  84. msg["data"] = ZoneService.instance().createAction(pingAction);
  85. pc.notify("area.playerHandler.battleEvent", msg);
  86. }
  87. public static void OnEnter(JsonObject result)
  88. {
  89. if (Convert.ToInt32(result["code"]) == 200)
  90. {
  91. player = (JsonObject)result["player"];
  92. // Console.WriteLine("on login " + result.ToString());
  93. pc.on("onBattleEvent", data =>
  94. {
  95. byte[] bytes = Convert.FromBase64String((string)data["data"]);
  96. object obj = ZoneService.instance().decodeEvent(bytes);
  97. if (obj.GetType() == typeof(Pong))
  98. {
  99. pingMonitor.end(CUtils.CurrentTimeMS);
  100. }
  101. Console.WriteLine("event: " + data["name"]);
  102. });
  103. pingTimer = new Timer(data =>
  104. {
  105. ping();
  106. },"",1000,1000);
  107. JsonObject msg = new JsonObject();
  108. pc.request("area.playerHandler.enterScene", msg, data =>
  109. {
  110. Console.WriteLine("enterScene success!");
  111. });
  112. }
  113. else
  114. {
  115. Console.WriteLine("enter error:" + result["code"]);
  116. }
  117. }
  118. public static void onDisconnect(JsonObject result)
  119. {
  120. Console.WriteLine("on sockect disconnected!");
  121. }
  122. public static void Run()
  123. {
  124. string host = "192.168.4.98";
  125. int port = 3014;
  126. ZoneService.instance().start( "D:/nodejs/node_global/npm/node_modules/zeus-csharp/lib/dll", "D:/nodejs/node_global/npm/node_modules/zeus-csharp/data");
  127. loginTest(host, port);
  128. do
  129. {
  130. Thread.Sleep(10);
  131. }
  132. while (true);
  133. }
  134. }
  135. }