WebSocketServer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #region MIT License
  2. /**
  3. * WebSocketServer.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 WebSocketServer : WebSocketServerBase
  37. {
  38. #region Field
  39. private ServiceManager _services;
  40. #endregion
  41. #region Public Constructors
  42. public WebSocketServer()
  43. : this(80)
  44. {
  45. }
  46. public WebSocketServer(int port)
  47. : this(System.Net.IPAddress.Any, port)
  48. {
  49. }
  50. public WebSocketServer(string url)
  51. : base(url)
  52. {
  53. if (BaseUri.AbsolutePath != "/")
  54. {
  55. var msg = "Must not contain the path component: " + url;
  56. throw new ArgumentException(msg, "url");
  57. }
  58. init();
  59. }
  60. public WebSocketServer(int port, bool secure)
  61. : this(System.Net.IPAddress.Any, port, secure)
  62. {
  63. }
  64. public WebSocketServer(System.Net.IPAddress address, int port)
  65. : this(address, port, port == 443 ? true : false)
  66. {
  67. }
  68. public WebSocketServer(System.Net.IPAddress address, int port, bool secure)
  69. : base(address, port, "/", secure)
  70. {
  71. init();
  72. }
  73. #endregion
  74. #region Property
  75. public IEnumerable<string> ServicePath
  76. {
  77. get
  78. {
  79. var url = BaseUri.IsAbsoluteUri
  80. ? BaseUri.ToString().TrimEnd('/')
  81. : String.Empty;
  82. foreach (var path in _services.Path)
  83. yield return url + path;
  84. }
  85. }
  86. public bool Sweeped
  87. {
  88. get
  89. {
  90. return _services.Sweeped;
  91. }
  92. set
  93. {
  94. _services.Sweeped = value;
  95. }
  96. }
  97. #endregion
  98. #region Private Method
  99. private void init()
  100. {
  101. _services = new ServiceManager();
  102. }
  103. #endregion
  104. #region Protected Method
  105. protected override void AcceptWebSocket(TcpClient client)
  106. {
  107. var context = Ext.AcceptWebSocket(client, IsSecure);
  108. var socket = context.WebSocket;
  109. var path = Ext.UrlDecode(context.Path);
  110. IServiceHost svcHost;
  111. if (!_services.TryGetServiceHost(path, out svcHost))
  112. {
  113. socket.Close(HttpStatusCode.NotImplemented);
  114. return;
  115. }
  116. if (BaseUri.IsAbsoluteUri)
  117. socket.Url = new Uri(BaseUri, path);
  118. svcHost.BindWebSocket(socket);
  119. }
  120. #endregion
  121. #region Public Methods
  122. public void AddService<T>(string absPath)
  123. where T : WebSocketService, new()
  124. {
  125. string msg;
  126. if (!Ext.IsValidAbsolutePath(absPath, out msg))
  127. {
  128. Error(msg);
  129. return;
  130. }
  131. var svcHost = new WebSocketServiceHost<T>();
  132. svcHost.Uri = BaseUri.IsAbsoluteUri
  133. ? new Uri(BaseUri, absPath)
  134. : Ext.ToUri(absPath);
  135. if (!Sweeped)
  136. svcHost.Sweeped = Sweeped;
  137. _services.Add(absPath, svcHost);
  138. }
  139. public void Broadcast(string data)
  140. {
  141. _services.Broadcast(data);
  142. }
  143. public override void Stop()
  144. {
  145. base.Stop();
  146. _services.Stop();
  147. }
  148. #endregion
  149. }
  150. }