UrlConverter.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace EZXR.Glass.Network.SocketIOClient
  5. {
  6. public class UrlConverter
  7. {
  8. public Uri HttpToWs(Uri httpUri, string eio, Dictionary<string, string> parameters)
  9. {
  10. var builder = new StringBuilder();
  11. if (httpUri.Scheme == "https" || httpUri.Scheme == "wss")
  12. {
  13. builder.Append("wss://");
  14. }
  15. else
  16. {
  17. builder.Append("ws://");
  18. }
  19. builder.Append(httpUri.Host);
  20. if (!httpUri.IsDefaultPort)
  21. {
  22. builder.Append(":").Append(httpUri.Port);
  23. }
  24. builder
  25. .Append("/socket.io/?EIO=")
  26. .Append(eio)
  27. .Append("&transport=websocket");
  28. if (parameters != null)
  29. {
  30. foreach (var item in parameters)
  31. {
  32. builder
  33. .Append("&")
  34. .Append(item.Key)
  35. .Append("=")
  36. .Append(item.Value);
  37. }
  38. }
  39. return new Uri(builder.ToString());
  40. }
  41. }
  42. }