RtcTerminalClient.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using Vinno.IUS.Common.Log;
  3. using Vinno.IUS.Common.Network.Leaf;
  4. using Vinno.IUS.Common.Network.Tcp;
  5. using Vinno.IUS.Common.Network.Transfer;
  6. using Vinno.vCloud.Protocol.Infrastructures;
  7. using Vinno.vCloud.Protocol.Messages.Client;
  8. using Vinno.vCloud.Protocol.Messages.Rtc;
  9. namespace Vinno.vCloud.Common.FIS.LiveVideos
  10. {
  11. class RtcTerminalClient : IDisposable
  12. {
  13. private ClientLeaf _clientLeaf;
  14. private ConnectionChecker _connectionKeeper;
  15. /// <summary>
  16. /// Disconnected with chat
  17. /// </summary>
  18. public event EventHandler<string> Disconnected;
  19. /// <summary>
  20. /// Chat room id = chat table id
  21. /// </summary>
  22. public string ChatRoomId { get; }
  23. public bool ClientIsOnline => _clientLeaf == null?false: _clientLeaf.Online;
  24. public RtcTerminalClient(string url, string roomId)
  25. {
  26. ChatRoomId = roomId;
  27. var tcpCreator = new TcpCreator(url);
  28. Logger.WriteLineInfo($"RtcTerminalClient:{tcpCreator.Url}");
  29. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
  30. if (!_clientLeaf.Online)
  31. {
  32. _clientLeaf.Close();
  33. throw new Exception("RtcClient not connect");
  34. }
  35. _connectionKeeper = new ConnectionChecker(_clientLeaf,5);
  36. _connectionKeeper.Offlined += OnOfflined;
  37. _connectionKeeper.Start();
  38. }
  39. public void NotifyServerJoinRoomSuccess(string roomId, string accountId, AccountType accountType)
  40. {
  41. try
  42. {
  43. Logger.WriteLineInfo("RequestRtcChatLive Start");
  44. if(!_clientLeaf.Online)
  45. {
  46. Logger.WriteLineError("RtcTerminalClient is not online");
  47. return;
  48. }
  49. if (_clientLeaf.IsClosed)
  50. {
  51. Logger.WriteLineError("RtcTerminalClient is Closed");
  52. return;
  53. }
  54. using (var request = MessagePool.GetMessage<RtcChatRequest>())
  55. {
  56. request.RoomId = roomId;
  57. request.SubscriberId = accountId;
  58. request.SubscriberType = accountType;
  59. var result = _clientLeaf?.Send(request);
  60. if (result != null)
  61. {
  62. var resultMessage = ResultMessage.Convert(result);
  63. if (resultMessage != CCR.OK)
  64. {
  65. Logger.WriteLineError("RequestRtcChatLive Fail");
  66. }
  67. else
  68. {
  69. Logger.WriteLineInfo("RequestRtcChatLive Success");
  70. }
  71. }
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. Logger.WriteLineError($"RequestRtcChatLive error:{ex}");
  77. }
  78. }
  79. private void OnOfflined(object sender, EventArgs e)
  80. {
  81. Disconnected?.Invoke(sender, ChatRoomId);
  82. }
  83. public void Dispose()
  84. {
  85. if (_connectionKeeper != null)
  86. {
  87. _connectionKeeper.Offlined -= OnOfflined;
  88. _connectionKeeper.Stop();
  89. _connectionKeeper = null;
  90. }
  91. if (_clientLeaf != null)
  92. {
  93. _clientLeaf.Close();
  94. _clientLeaf = null;
  95. }
  96. // _platformManager.LivePlayManager.UnRegisterChatVideo();
  97. }
  98. }
  99. }