| | 1 | | using System; |
| | 2 | | using System.Net; |
| | 3 | | using System.Text; |
| | 4 | | using NLog; |
| | 5 | | using ReFlex.Core.Common.Components; |
| | 6 | | using ReFlex.Core.Networking.Interfaces; |
| | 7 | | using ReFlex.Core.Networking.Util; |
| | 8 | | using WebSocketSharp.Server; |
| | 9 | | using Logger = NLog.Logger; |
| | 10 | |
|
| | 11 | | namespace ReFlex.Core.Networking.Components |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Can broadcast messages or objects to connected clients via http protocol. |
| | 15 | | /// </summary> |
| | 16 | | /// <seealso cref="IServer" /> |
| | 17 | | /// <inheritdoc /> |
| | 18 | | public class WebSocketServer : IServer, IDisposable |
| | 19 | | { |
| 0 | 20 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | 21 | |
|
| | 22 | | protected HttpServer Server; |
| | 23 | | private IPAddress _address; |
| 0 | 24 | | private readonly Guid _serverId = Guid.NewGuid(); |
| | 25 | |
|
| 0 | 26 | | protected int Port { get; private set; } |
| | 27 | |
|
| 0 | 28 | | protected string Endpoint { get; private set; } |
| | 29 | |
|
| 0 | 30 | | public NetworkInterface Type => NetworkInterface.Websockets; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Gets a value indicating whether this instance is ready. |
| | 34 | | /// </summary> |
| | 35 | | /// <value> |
| | 36 | | /// <c>true</c> if this instance is ready; otherwise, <c>false</c>. |
| | 37 | | /// </value> |
| | 38 | | /// <inheritdoc /> |
| 0 | 39 | | public bool IsReady => Server != null; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets a value indicating whether this instance is started. |
| | 43 | | /// </summary> |
| | 44 | | /// <value> |
| | 45 | | /// <c>true</c> if this instance is started; otherwise, <c>false</c>. |
| | 46 | | /// </value> |
| 0 | 47 | | public bool IsStarted { get; private set; } |
| | 48 | |
|
| | 49 | |
|
| 0 | 50 | | public string Id => _serverId.ToString(); |
| | 51 | |
|
| | 52 | | public string Address |
| | 53 | | { |
| 0 | 54 | | get => $"{_address}:{Port}{Endpoint}"; |
| | 55 | | set |
| 0 | 56 | | { |
| 0 | 57 | | var p1 = value.IndexOf(':'); |
| 0 | 58 | | var ipAddress = _address.ToString(); |
| | 59 | |
|
| 0 | 60 | | if (p1 > 0) |
| 0 | 61 | | { |
| 0 | 62 | | ipAddress = value.Substring(0, p1); |
| 0 | 63 | | } |
| | 64 | |
|
| 0 | 65 | | var portAndEndpoint = value.Substring(p1 + 1); |
| 0 | 66 | | var p2 = portAndEndpoint.IndexOf('/'); |
| 0 | 67 | | var success = int.TryParse(portAndEndpoint.Substring(0, p2), out var port); |
| 0 | 68 | | var endpoint = portAndEndpoint.Substring(p2); |
| | 69 | |
|
| 0 | 70 | | Stop(); |
| | 71 | |
|
| 0 | 72 | | Init(ipAddress, port, endpoint); |
| 0 | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="ipAddress">the ip address of the server</param> |
| | 80 | | /// <param name="endpoint">the path for the websocket (e.g. "/MyService")</param> |
| 0 | 81 | | public WebSocketServer(string ipAddress, int port, string endpoint) |
| 0 | 82 | | { |
| 0 | 83 | | Init(ipAddress, port, endpoint); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | private void Init(string ipAddress, int port, string endpoint) |
| 0 | 87 | | { |
| 0 | 88 | | Port = port; |
| 0 | 89 | | IsStarted = false; |
| 0 | 90 | | var success = IPAddress.TryParse(ipAddress, out _address); |
| 0 | 91 | | if (!success) |
| 0 | 92 | | throw new ArgumentOutOfRangeException($"Provided IP Address {ipAddress} is not valid for {GetType().Name |
| | 93 | |
|
| 0 | 94 | | Endpoint = endpoint; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Starts the websocket using the specified port. |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="port">The port.</param> |
| | 101 | | /// <inheritdoc /> |
| | 102 | | public void Start() |
| 0 | 103 | | { |
| 0 | 104 | | if (IsStarted) |
| 0 | 105 | | return; |
| | 106 | |
|
| 0 | 107 | | Server = new HttpServer(_address, Port); |
| 0 | 108 | | InitServices(); |
| | 109 | |
|
| 0 | 110 | | Server.OnGet += OnGet; |
| | 111 | |
|
| 0 | 112 | | try { |
| | 113 | |
|
| 0 | 114 | | Server.Start(); |
| 0 | 115 | | IsStarted = true; |
| | 116 | |
|
| 0 | 117 | | } catch (Exception exc) { |
| 0 | 118 | | Logger.Error(exc, $"{exc.GetType().Name} when starting {GetType().Name}: {exc?.Message}{Environment.NewL |
| 0 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Stops this instance. |
| | 125 | | /// </summary> |
| | 126 | | /// <inheritdoc /> |
| | 127 | | public void Stop() |
| 0 | 128 | | { |
| 0 | 129 | | if (!IsStarted) |
| 0 | 130 | | return; |
| | 131 | |
|
| | 132 | |
|
| 0 | 133 | | Server?.Stop(); |
| 0 | 134 | | if (Server != null) |
| 0 | 135 | | Server.OnGet -= OnGet; |
| 0 | 136 | | IsStarted = false; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Broadcasts the specified data. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="data">The data.</param> |
| | 143 | | /// <inheritdoc /> |
| | 144 | | public void Broadcast(object data) |
| 0 | 145 | | { |
| 0 | 146 | | if (IsReady && IsStarted) |
| 0 | 147 | | Broadcast(SerializationUtils.SerializeToJson(data)); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Broadcasts the specified data. |
| | 152 | | /// </summary> |
| | 153 | | /// <param name="data">The data.</param> |
| | 154 | | /// <inheritdoc /> |
| | 155 | | public void Broadcast(byte[] data) |
| 0 | 156 | | { |
| 0 | 157 | | if (IsReady && IsStarted) |
| 0 | 158 | | Server?.WebSocketServices?.Broadcast(data); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | /// <summary> |
| | 162 | | /// Broadcasts the specified data. |
| | 163 | | /// </summary> |
| | 164 | | /// <param name="data">The data.</param> |
| | 165 | | /// <inheritdoc /> |
| | 166 | | public void Broadcast(string data) |
| 0 | 167 | | { |
| 0 | 168 | | if (IsReady && IsStarted) |
| 0 | 169 | | Server?.WebSocketServices?.Broadcast(data); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | protected virtual void InitServices() |
| 0 | 173 | | { |
| 0 | 174 | | Server.AddWebSocketService<BroadcastService>(Endpoint); |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Called when [get]. |
| | 179 | | /// </summary> |
| | 180 | | /// <param name="sender">The sender.</param> |
| | 181 | | /// <param name="args">The <see cref="HttpRequestEventArgs"/> instance containing the event data.</param> |
| | 182 | | private static void OnGet(object sender, HttpRequestEventArgs args) |
| 0 | 183 | | { |
| 0 | 184 | | var request = args.Request; |
| 0 | 185 | | var response = args.Response; |
| | 186 | |
|
| 0 | 187 | | var path = request.RawUrl; |
| 0 | 188 | | if (path == "/") |
| 0 | 189 | | path += "index.html"; |
| | 190 | |
|
| 0 | 191 | | if (path.EndsWith(".html")) |
| 0 | 192 | | { |
| 0 | 193 | | response.ContentType = "text/html"; |
| 0 | 194 | | response.ContentEncoding = Encoding.UTF8; |
| 0 | 195 | | } |
| 0 | 196 | | else if (path.EndsWith(".js")) |
| 0 | 197 | | { |
| 0 | 198 | | response.ContentType = "application/javascript"; |
| 0 | 199 | | response.ContentEncoding = Encoding.UTF8; |
| 0 | 200 | | } |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | public void Dispose() |
| 0 | 204 | | { |
| 0 | 205 | | Server?.Stop(); |
| 0 | 206 | | } |
| | 207 | | } |
| | 208 | | } |