WService_WebGL.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if UNITY_WEBGL
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. namespace ET
  8. {
  9. public class WService : AService
  10. {
  11. private long idGenerater = 200000000;
  12. private readonly Dictionary<long, WChannel> channels = new Dictionary<long, WChannel>();
  13. public override void Dispose()
  14. {
  15. if (this.IsDisposed())
  16. {
  17. return;
  18. }
  19. this.Id = 0;
  20. foreach (var kv in this.channels.ToArray())
  21. {
  22. kv.Value.Dispose();
  23. }
  24. }
  25. public WService(IEnumerable<string> prefixs)
  26. {
  27. this.ServiceType = ServiceType.Outer;
  28. }
  29. public WService()
  30. {
  31. this.ServiceType = ServiceType.Outer;
  32. }
  33. private long GetId
  34. {
  35. get { return ++this.idGenerater; }
  36. }
  37. public override void Create(long id, IPEndPoint ipEndPoint, string address)
  38. {
  39. if (!this.channels.TryGetValue(id, out _))
  40. {
  41. WChannel channel = new WChannel(id, ipEndPoint, this, address);
  42. this.channels[channel.Id] = channel;
  43. }
  44. }
  45. public override void Remove(long id, int error = 0)
  46. {
  47. WChannel channel;
  48. if (!this.channels.TryGetValue(id, out channel))
  49. {
  50. return;
  51. }
  52. channel.Error = error;
  53. this.channels.Remove(id);
  54. channel.Dispose();
  55. }
  56. protected void Get(long id, IPEndPoint ipEndPoint)
  57. {
  58. // if (!this.channels.TryGetValue(id, out _))
  59. // {
  60. // this.Create(id, ipEndPoint);
  61. // }
  62. }
  63. public WChannel Get(long id)
  64. {
  65. WChannel channel = null;
  66. this.channels.TryGetValue(id, out channel);
  67. return channel;
  68. }
  69. public override void Send(long channelId, long actorId, MemoryStream stream)
  70. {
  71. this.channels.TryGetValue(channelId, out WChannel channel);
  72. if (channel == null)
  73. {
  74. return;
  75. }
  76. channel.Send(stream);
  77. }
  78. public override void Update()
  79. {
  80. }
  81. public override bool IsDisposed()
  82. {
  83. return this.Id == 0;
  84. }
  85. }
  86. }
  87. #endif