Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DotNetty.Transport.Bootstrapping;
  7. using DotNetty.Transport.Channels;
  8. using DotNetty.Buffers;
  9. using System.Threading;
  10. namespace nettyCommunication
  11. {
  12. class Program
  13. {
  14. private static Communication comm = null;
  15. private static Thread receiveMsg = null;
  16. private static bool connected { get; set; }
  17. static void Main(string[] args)
  18. {
  19. //持续进行连接尝试
  20. Task.Factory.StartNew(() =>
  21. {
  22. //初始化后与web持续连接
  23. while (true)
  24. {
  25. try
  26. {
  27. Connections.close();
  28. Connections.Connection();
  29. connected = true;
  30. comm = new Communication();
  31. break;
  32. }
  33. catch (Exception)
  34. {
  35. connected = false;
  36. Console.WriteLine("服务没有开启,请检查服务器");
  37. }
  38. Thread.Sleep(200);
  39. }
  40. //持续判断连接状态并重连
  41. while (true)
  42. {
  43. Console.WriteLine(Connections.isAlive() + ", " + receiveMsg.ThreadState.ToString());
  44. if (Connections.isAlive())
  45. {
  46. if (!connected)
  47. {
  48. comm = new Communication();
  49. }
  50. connected = true;
  51. if (receiveMsg.ThreadState == ThreadState.Aborted)
  52. {
  53. try
  54. {
  55. receiveMsg.Start();
  56. }
  57. catch (Exception ex) { Console.WriteLine(ex.Message); }
  58. }
  59. }
  60. else
  61. {
  62. connected = false;
  63. try
  64. {
  65. if (receiveMsg.ThreadState == ThreadState.WaitSleepJoin)
  66. {
  67. receiveMsg.Interrupt();
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. Console.WriteLine("连接断开,终止消息接收线程");
  73. }
  74. Console.WriteLine(" 连接关闭,需要重新连接注册");
  75. try
  76. {
  77. Connections.close();
  78. Connections.Connection();
  79. }
  80. catch (Exception)
  81. {
  82. Console.WriteLine("服务没有开启,请检查服务器");
  83. }
  84. }
  85. Thread.Sleep(200);
  86. }
  87. });
  88. //持续接收消息
  89. receiveMsg = new Thread(() =>
  90. {
  91. while (true)
  92. {
  93. try
  94. {
  95. if (connected && comm != null)
  96. {
  97. MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage());
  98. }
  99. }
  100. catch { Console.WriteLine("线程已中断"); }
  101. }
  102. });
  103. receiveMsg.Start();
  104. Console.ReadLine();
  105. }
  106. //public static async Task sendMessage(IChannelHandlerContext ctx,object message)
  107. //{
  108. // AbstractMessage ms = (AbstractMessage)message;
  109. // IByteBuffer initialMessage = Unpooled.Buffer(ClientSettings.Size);
  110. // //byte[] messageByte = Encoding.UTF8.GetBytes(msg);
  111. // byte[] byteMessage = JsonByByteToObjectTools.ObjToJsonByte(ms);
  112. // initialMessage.WriteBytes(byteMessage);
  113. // await ctx.WriteAndFlushAsync(initialMessage);
  114. // ctx.Flush();
  115. //}
  116. }
  117. }