WebSocketServiceHost.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #region MIT License
  2. /**
  3. * WebSocketServiceHost.cs
  4. *
  5. * A C# implementation of the WebSocket protocol server.
  6. *
  7. * The MIT License
  8. *
  9. * Copyright (c) 2012 sta.blockhead
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #endregion
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Net.Sockets;
  33. using WebSocketSharp.Net;
  34. namespace WebSocketSharp.Server
  35. {
  36. public class WebSocketServiceHost<T> : WebSocketServerBase, IServiceHost where T : WebSocketService, new()
  37. {
  38. #region Field
  39. private SessionManager _sessions;
  40. #endregion
  41. #region Internal Constructor
  42. internal WebSocketServiceHost()
  43. {
  44. init();
  45. }
  46. #endregion
  47. #region Public Constructors
  48. public WebSocketServiceHost(int port)
  49. : this(port, "/")
  50. {
  51. }
  52. public WebSocketServiceHost(string url)
  53. : base(url)
  54. {
  55. init();
  56. }
  57. public WebSocketServiceHost(int port, bool secure)
  58. : this(port, "/", secure)
  59. {
  60. }
  61. public WebSocketServiceHost(int port, string absPath)
  62. : this(System.Net.IPAddress.Any, port, absPath)
  63. {
  64. }
  65. public WebSocketServiceHost(int port, string absPath, bool secure)
  66. : this(System.Net.IPAddress.Any, port, absPath, secure)
  67. {
  68. }
  69. public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath)
  70. : this(address, port, absPath, port == 443 ? true : false)
  71. {
  72. }
  73. public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath, bool secure)
  74. : base(address, port, absPath, secure)
  75. {
  76. init();
  77. }
  78. #endregion
  79. #region Properties
  80. public bool Sweeped
  81. {
  82. get
  83. {
  84. return _sessions.Sweeped;
  85. }
  86. set
  87. {
  88. _sessions.Sweeped = value;
  89. }
  90. }
  91. public Uri Uri
  92. {
  93. get
  94. {
  95. return BaseUri;
  96. }
  97. internal set
  98. {
  99. BaseUri = value;
  100. }
  101. }
  102. #endregion
  103. #region Private Method
  104. private void init()
  105. {
  106. _sessions = new SessionManager();
  107. }
  108. #endregion
  109. #region Protected Method
  110. protected override void AcceptWebSocket(TcpClient client)
  111. {
  112. var context = Ext.AcceptWebSocket(client, IsSecure);
  113. var socket = context.WebSocket;
  114. var path = Ext.UrlDecode(context.Path);
  115. if (path != Ext.UrlDecode(Ext.GetAbsolutePath(Uri)))
  116. {
  117. socket.Close(HttpStatusCode.NotImplemented);
  118. return;
  119. }
  120. if (Uri.IsAbsoluteUri)
  121. socket.Url = Uri;
  122. BindWebSocket(socket);
  123. }
  124. #endregion
  125. #region Public Methods
  126. public void BindWebSocket(WebSocket socket)
  127. {
  128. T service = new T();
  129. service.Bind(socket, _sessions);
  130. service.Start();
  131. }
  132. public void Broadcast(string data)
  133. {
  134. _sessions.Broadcast(data);
  135. }
  136. public Dictionary<string, bool> Broadping(string message)
  137. {
  138. return _sessions.Broadping(message);
  139. }
  140. public override void Stop()
  141. {
  142. base.Stop();
  143. _sessions.Stop();
  144. }
  145. #endregion
  146. }
  147. }