|
| 1 | +using SocketIOClient.Messages; |
| 2 | +using SocketIOClient.Transport; |
| 3 | +using SocketIOClient.UriConverters; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace SocketIOClient.Routers |
| 13 | +{ |
| 14 | + public class HttpRouter : Router |
| 15 | + { |
| 16 | + public HttpRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSocketProvider, SocketIOOptions options) : base(httpClient, clientWebSocketProvider, options) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + HttpTransport _httpTransport; |
| 21 | + CancellationTokenSource _pollingTokenSource; |
| 22 | + string _httpUri; |
| 23 | + |
| 24 | + protected override TransportProtocol Protocol => TransportProtocol.Polling; |
| 25 | + |
| 26 | + public override async Task ConnectAsync() |
| 27 | + { |
| 28 | + await base.ConnectAsync(); |
| 29 | + |
| 30 | + Uri uri = UriConverter.GetServerUri(false, ServerUri, EIO, Options.Path, Options.Query); |
| 31 | + var req = new HttpRequestMessage(HttpMethod.Get, uri); |
| 32 | + if (Options.ExtraHeaders != null) |
| 33 | + { |
| 34 | + foreach (var item in Options.ExtraHeaders) |
| 35 | + { |
| 36 | + req.Headers.Add(item.Key, item.Value); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (EIO == 3) |
| 41 | + { |
| 42 | + _httpTransport = new HttpEio3Transport(HttpClient); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + _httpTransport = new HttpEio4Transport(HttpClient); |
| 47 | + } |
| 48 | + |
| 49 | + _httpTransport.OnTextReceived = OnTextReceived; |
| 50 | + _httpTransport.OnBinaryReceived = OnBinaryReceived; |
| 51 | + |
| 52 | + await _httpTransport.SendAsync(req, new CancellationTokenSource(Options.ConnectionTimeout).Token).ConfigureAwait(false); |
| 53 | + if (_pollingTokenSource != null) |
| 54 | + { |
| 55 | + _pollingTokenSource.Cancel(); |
| 56 | + } |
| 57 | + _pollingTokenSource = new CancellationTokenSource(); |
| 58 | + |
| 59 | + StartPolling(_pollingTokenSource.Token); |
| 60 | + } |
| 61 | + |
| 62 | + private void StartPolling(CancellationToken cancellationToken) |
| 63 | + { |
| 64 | + Task.Factory.StartNew(async () => |
| 65 | + { |
| 66 | + while (!cancellationToken.IsCancellationRequested) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + await _httpTransport.GetAsync(_httpUri, CancellationToken.None).ConfigureAwait(false); |
| 71 | + } |
| 72 | + catch |
| 73 | + { |
| 74 | + OnTransportClosed(); |
| 75 | + throw; |
| 76 | + } |
| 77 | + } |
| 78 | + }, TaskCreationOptions.LongRunning); |
| 79 | + } |
| 80 | + |
| 81 | + protected override async Task OpenAsync(OpenedMessage msg) |
| 82 | + { |
| 83 | + Uri uri = UriConverter.GetServerUri(false, ServerUri, EIO, Options.Path, Options.Query); |
| 84 | + _httpUri = uri + "&sid=" + msg.Sid; |
| 85 | + await base.OpenAsync(msg); |
| 86 | + } |
| 87 | + |
| 88 | + public override async Task DisconnectAsync() |
| 89 | + { |
| 90 | + _pollingTokenSource.Cancel(); |
| 91 | + await base.DisconnectAsync(); |
| 92 | + } |
| 93 | + |
| 94 | + protected override async Task SendAsync(string text, CancellationToken cancellationToken) |
| 95 | + { |
| 96 | + if (EIO == 3) |
| 97 | + { |
| 98 | + text = text.Length + ":" + text; |
| 99 | + } |
| 100 | + await _httpTransport.PostAsync(_httpUri, text, cancellationToken).ConfigureAwait(false); |
| 101 | + Debug.WriteLine($"[Send] {text}"); |
| 102 | + } |
| 103 | + |
| 104 | + protected override async Task SendAsync(IEnumerable<byte[]> bytes, CancellationToken cancellationToken) |
| 105 | + { |
| 106 | + await _httpTransport.PostAsync(_httpUri, bytes, cancellationToken).ConfigureAwait(false); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments