UriConverter.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SocketIOClient.UriConverters
  5. {
  6. public class UriConverter : IUriConverter
  7. {
  8. public Uri GetServerUri(bool ws, Uri serverUri, int eio, string path, IEnumerable<KeyValuePair<string, string>> queryParams)
  9. {
  10. var builder = new StringBuilder();
  11. if (serverUri.Scheme == "https" || serverUri.Scheme == "wss")
  12. {
  13. builder.Append(ws ? "wss://" : "https://");
  14. }
  15. else if (serverUri.Scheme == "http" || serverUri.Scheme == "ws")
  16. {
  17. builder.Append(ws ? "ws://" : "http://");
  18. }
  19. else
  20. {
  21. throw new ArgumentException("Only supports 'http, https, ws, wss' protocol");
  22. }
  23. builder.Append(serverUri.Host);
  24. if (!serverUri.IsDefaultPort)
  25. {
  26. builder.Append(":").Append(serverUri.Port);
  27. }
  28. if (string.IsNullOrEmpty(path))
  29. {
  30. builder.Append("/socket.io");
  31. }
  32. else
  33. {
  34. builder.Append(path);
  35. }
  36. builder
  37. .Append("/?EIO=")
  38. .Append(eio)
  39. .Append("&transport=")
  40. .Append(ws ? "websocket" : "polling");
  41. if (queryParams != null)
  42. {
  43. foreach (var item in queryParams)
  44. {
  45. builder.Append('&').Append(item.Key).Append('=').Append(item.Value);
  46. }
  47. }
  48. return new Uri(builder.ToString());
  49. }
  50. }
  51. }