WebSocketService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #region MIT License
  2. /**
  3. * WebSocketService.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Threading;
  32. using WebSocketSharp.Frame;
  33. namespace WebSocketSharp.Server {
  34. public abstract class WebSocketService
  35. {
  36. #region Private Fields
  37. private SessionManager _sessions;
  38. private WebSocket _socket;
  39. #endregion
  40. #region Public Constructor
  41. public WebSocketService()
  42. {
  43. ID = String.Empty;
  44. IsBound = false;
  45. }
  46. #endregion
  47. #region Protected Properties
  48. protected NameValueCollection QueryString {
  49. get {
  50. return IsBound ? _socket.QueryString : null;
  51. }
  52. }
  53. protected SessionManager Sessions {
  54. get {
  55. return IsBound ? _sessions : null;
  56. }
  57. }
  58. #endregion
  59. #region Public Properties
  60. public string ID { get; private set; }
  61. public bool IsBound { get; private set; }
  62. #endregion
  63. #region Private Method
  64. private void defaultBind()
  65. {
  66. _socket.OnOpen += (sender, e) =>
  67. {
  68. ID = _sessions.Add(this);
  69. };
  70. _socket.OnClose += (sender, e) =>
  71. {
  72. _sessions.Remove(ID);
  73. };
  74. }
  75. #endregion
  76. #region Protected Methods
  77. protected virtual void OnClose(object sender, CloseEventArgs e)
  78. {
  79. }
  80. protected virtual void OnError(object sender, ErrorEventArgs e)
  81. {
  82. }
  83. protected virtual void OnMessage(object sender, MessageEventArgs e)
  84. {
  85. }
  86. protected virtual void OnOpen(object sender, EventArgs e)
  87. {
  88. }
  89. #endregion
  90. #region Internal Methods
  91. internal void SendAsync(byte[] data, Action completed)
  92. {
  93. _socket.SendAsync(data, completed);
  94. }
  95. internal void SendAsync(string data, Action completed)
  96. {
  97. _socket.SendAsync(data, completed);
  98. }
  99. #endregion
  100. #region Public Methods
  101. public void Bind(WebSocket socket, SessionManager sessions)
  102. {
  103. _socket = socket;
  104. _sessions = sessions;
  105. defaultBind();
  106. _socket.OnOpen += OnOpen;
  107. _socket.OnMessage += OnMessage;
  108. _socket.OnError += OnError;
  109. _socket.OnClose += OnClose;
  110. IsBound = true;
  111. }
  112. public bool Ping()
  113. {
  114. return Ping(String.Empty);
  115. }
  116. public bool Ping(string message)
  117. {
  118. return IsBound
  119. ? _socket.Ping(message)
  120. : false;
  121. }
  122. public Dictionary<string, bool> PingAround()
  123. {
  124. return PingAround(String.Empty);
  125. }
  126. public Dictionary<string, bool> PingAround(string message)
  127. {
  128. return IsBound
  129. ? _sessions.Broadping(message)
  130. : null;
  131. }
  132. public bool PingTo(string id)
  133. {
  134. return PingTo(id, String.Empty);
  135. }
  136. public bool PingTo(string id, string message)
  137. {
  138. if (!IsBound)
  139. return false;
  140. WebSocketService service;
  141. return _sessions.TryGetByID(id, out service)
  142. ? service.Ping(message)
  143. : false;
  144. }
  145. public void Publish(byte[] data)
  146. {
  147. if (IsBound)
  148. _sessions.Broadcast(data);
  149. }
  150. public void Publish(string data)
  151. {
  152. if (IsBound)
  153. _sessions.Broadcast(data);
  154. }
  155. public void Send(byte[] data)
  156. {
  157. if (IsBound)
  158. _socket.Send(data);
  159. }
  160. public void Send(string data)
  161. {
  162. if (IsBound)
  163. _socket.Send(data);
  164. }
  165. public void SendTo(string id, byte[] data)
  166. {
  167. if (!IsBound)
  168. return;
  169. WebSocketService service;
  170. if (_sessions.TryGetByID(id, out service))
  171. service.Send(data);
  172. }
  173. public void SendTo(string id, string data)
  174. {
  175. if (!IsBound)
  176. return;
  177. WebSocketService service;
  178. if (_sessions.TryGetByID(id, out service))
  179. service.Send(data);
  180. }
  181. public void Start()
  182. {
  183. if (IsBound)
  184. _socket.Connect();
  185. }
  186. public void Stop()
  187. {
  188. if (!IsBound)
  189. return;
  190. _socket.Close();
  191. }
  192. public void Stop(CloseStatusCode code, string reason)
  193. {
  194. Stop((ushort)code, reason);
  195. }
  196. public void Stop(ushort code, string reason)
  197. {
  198. if (!IsBound)
  199. return;
  200. _socket.Close(code, reason);
  201. }
  202. #endregion
  203. }
  204. }