MasterInteractionSyncService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using CSScriptLib;
  6. using Newtonsoft.Json;
  7. using WingInterfaceLibrary.DB.Request;
  8. using WingInterfaceLibrary.DTO.DistributedServerInfo;
  9. using WingInterfaceLibrary.Interface.DBInterface;
  10. using WingInterfaceLibrary.OpLog;
  11. using WingInterfaceLibrary.Request.DBRequest;
  12. using WingInterfaceLibrary.Result;
  13. using WingServerCommon.Config;
  14. using WingServerCommon.Service;
  15. namespace WingCloudServer.InteractionCenter
  16. {
  17. public partial class MasterInteractionCenterService : IMasterInteractionCenterService
  18. {
  19. /// <summary>
  20. /// 实时同步业务数据到主服务器
  21. /// </summary>
  22. /// <param name="request"></param>
  23. /// <returns></returns>
  24. public async Task<bool> SyncReceiveSlaveServiceDataAsync(SyncReceiveServiceDataRequest request)
  25. {
  26. //todo 判断是当前服务器并且是主服务器 则只需要发送oplogs到其他副服务器
  27. var localServerUrl = ConfigurationManager.Host;
  28. var baseLiveConsultation = JsonConvert.DeserializeObject<BaseLiveConsultationJson>(request.ServiceDataJson);
  29. // 调用manager
  30. // 判断是否存在其他服务器的用户
  31. var getLiveRoomByCodeDBRequest = new GetLiveRoomByCodeDBRequest();
  32. getLiveRoomByCodeDBRequest.LiveRoomCode = baseLiveConsultation.RoomCode;
  33. var roomDTO = await _liveRoomDBService.GetLiveRoomByCodeAsync(getLiveRoomByCodeDBRequest);
  34. var users = roomDTO.UserInfos;
  35. //操作人所在服务器
  36. var operatorUser = users.FirstOrDefault(x => x.Code == baseLiveConsultation.OperatorCode);
  37. //连接其他副服务器的用户
  38. var usersConnectOtherSlave = users.Where(x => x.LoginServerHost != operatorUser.LoginServerHost && x.LoginServerHost != localServerUrl);
  39. if (usersConnectOtherSlave != null && usersConnectOtherSlave.Count() > 0)
  40. {
  41. //获取oplogs
  42. var getOpLogsByCodesFormMasterRequest = new GetOpLogsByCodesFormMasterRequest();
  43. getOpLogsByCodesFormMasterRequest.Codes = new List<string> { baseLiveConsultation.ConsultationRecordCode, baseLiveConsultation.RoomCode };
  44. var oplogs = await _opLogDBService.GetOpLogsByCodesAsync(getOpLogsByCodesFormMasterRequest);
  45. //通知副服务器
  46. var otherSlaves = usersConnectOtherSlave.Select(x => x.LoginServerHost).Distinct();
  47. if (otherSlaves.Count() > 0)
  48. {
  49. //动态注册副服务器服务
  50. var otherSlaveHosts = new List<string>();
  51. foreach (var item in otherSlaves)
  52. {
  53. var slaveHost = item.Replace("http:", "").Replace(".", "").Replace("/", "");
  54. DynamicSlaveService(request, slaveHost);
  55. }
  56. }
  57. }
  58. return true;
  59. }
  60. public void DynamicSlaveService(SyncReceiveServiceDataRequest request, string host)
  61. {
  62. var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC);
  63. var str = @"using System;
  64. using System.Threading.Tasks;
  65. using WingServerCommon.Service;
  66. using WingInterfaceLibrary.OpLog;
  67. using System.Collections.Generic;
  68. using JsonRpcLite.Rpc;
  69. using WingInterfaceLibrary.Interface.DBInterface;
  70. public class DynamicSlaveServiceClass : JsonRpcService
  71. {
  72. public void DynamicSlaveMethod(SyncReceiveServiceDataRequest request, IEnumerable<string> otherSlaveHosts)
  73. {
  74. var _dynamicService = GetProxy<IDynamicSlaveService" + host + @">();
  75. _dynamicService.DynamicSlaveAsync(request);
  76. }
  77. }";
  78. Assembly compilecode = eval.CompileCode(str);
  79. var ps = compilecode.GetType("css_root+DynamicSlaveServiceClass");
  80. var obj = compilecode.CreateInstance("css_root+DynamicSlaveServiceClass");
  81. var mes = ps.GetMethod("DynamicSlaveMethod");
  82. mes.Invoke(obj, new object[] { request, host });
  83. }
  84. }
  85. }