MobileMQ.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using WebSocketSharp;
  6. using System.Threading;
  7. using System.Configuration;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. namespace Bowin.Common.Mobile
  12. {
  13. public class MessageBody
  14. {
  15. public string Type { get; set; }
  16. public string Key { get; set; }
  17. public string Message { get; set; }
  18. }
  19. public static class MobileMQ
  20. {
  21. private const string CONFIG_HEARTBEAT_INTERVAL_NAME = "MobileMQ_Heartbeat_Interval";
  22. private enum SendType
  23. {
  24. HeartBeat,
  25. Message
  26. }
  27. private class MQAddress
  28. {
  29. internal string IPAddress { get; set; }
  30. internal int Port { get; set; }
  31. }
  32. private static Dictionary<string, MQAddress> _ipPool;
  33. private static WebSocket socket;
  34. static MobileMQ() {
  35. _ipPool = new Dictionary<string, MQAddress>();
  36. }
  37. private static void OnInit()
  38. {
  39. int interval;
  40. try
  41. {
  42. interval = Convert.ToInt32(ConfigurationManager.AppSettings[CONFIG_HEARTBEAT_INTERVAL_NAME]);
  43. }
  44. catch
  45. {
  46. //默认两分钟ping一次
  47. interval = 120000;
  48. }
  49. while (_ipPool.Count > 0)
  50. {
  51. Thread.Sleep(interval);
  52. //测心跳,清废弃链接
  53. HeartBeat();
  54. }
  55. }
  56. public static void Regist(string key, string ip, int port)
  57. {
  58. if (!_ipPool.ContainsKey(key))
  59. {
  60. _ipPool.Add(key, new MQAddress { IPAddress = ip, Port = port });
  61. if (_ipPool.Count == 1)
  62. {
  63. OnInit();
  64. }
  65. }
  66. else
  67. {
  68. _ipPool[key].IPAddress = ip;
  69. _ipPool[key].Port = port;
  70. }
  71. }
  72. public static void Remove(string key)
  73. {
  74. _ipPool.Remove(key);
  75. }
  76. public static void SendMessage(string[] keys, MessageBody message)
  77. {
  78. sendMessage(SendType.Message, message, x => keys.Contains(x.Key));
  79. }
  80. public static void SendMessage(MessageBody message)
  81. {
  82. sendMessage(SendType.Message, message);
  83. }
  84. private static void sendMessage(SendType sendType, MessageBody message = null, Func<KeyValuePair<string, MQAddress>, bool> predicate = null)
  85. {
  86. List<KeyValuePair<string, MQAddress>> sendPool;
  87. if (predicate == null)
  88. {
  89. sendPool = _ipPool.ToList();
  90. }
  91. else
  92. {
  93. sendPool = _ipPool.Where(predicate).ToList();
  94. }
  95. sendPool.ForEach(x =>
  96. {
  97. socket = new WebSocket(string.Format("ws://{0}:{1}", x.Value.IPAddress, x.Value.Port));
  98. socket.Connect();
  99. //发送两种信号:心跳和消息
  100. if (sendType == SendType.HeartBeat)
  101. {
  102. if (!socket.Ping())
  103. {
  104. Remove(x.Key);
  105. }
  106. }
  107. else
  108. {
  109. if (message != null)
  110. {
  111. using (MemoryStream ms = new MemoryStream())
  112. {
  113. IFormatter formatter = new BinaryFormatter();
  114. formatter.Serialize(ms, message);
  115. socket.SendAsync(ms.GetBuffer(), (res =>
  116. {
  117. socket.Close();
  118. }));
  119. }
  120. }
  121. }
  122. });
  123. }
  124. private static void HeartBeat()
  125. {
  126. _ipPool.ToList().ForEach(x =>
  127. {
  128. sendMessage(SendType.HeartBeat);
  129. });
  130. }
  131. }
  132. }