You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.5 KiB

2 years ago
  1. using Microsoft.AspNet.SignalR.Client;
  2. using Euro.Transfer.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using Euro.Transfer.Base;
  8. using System.Threading;
  9. namespace Euro.Transfer
  10. {
  11. public class HubTransfer : ServiceBase
  12. {
  13. private string transferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  14. private string transferUserID = Common.ConfigGetValue("", "TransferUserID");
  15. public delegate void WriteOrLogsHandler(string text, int count);
  16. public event WriteOrLogsHandler writeOrLogs;
  17. public List<UserInfo> OnlineUsers = new List<UserInfo>(); // 在线用户列表
  18. public IHubProxy msgProxy;
  19. public HubConnection connection;
  20. public string clientOrgId;
  21. public string clientId;
  22. public string clientName;
  23. public HubTransfer()
  24. {
  25. clientOrgId = transferOrgID;
  26. clientId = transferUserID;
  27. clientName = "奕達小助手";
  28. }
  29. public async Task RunAsync(string url)
  30. {
  31. try
  32. {
  33. connection = new HubConnection(url);
  34. //connection.TraceWriter = _traceWriter;
  35. msgProxy = connection.CreateHubProxy("msgHub");
  36. msgProxy.On<string, string, List<UserInfo>>("onConnected", (connnectId, username, allUsers) =>
  37. {
  38. OnlineUsers = allUsers;
  39. });
  40. msgProxy.On<string, List<UserInfo>, string, string, string, bool>("onUserDisconnected", (connnectId, allUsers, orgid, userid, username, islogin) =>
  41. {
  42. //var user = OnlineUsers.FirstOrDefault(u => u.ConnectionId == connnectId);
  43. //// 判断用户是否存在,存在则删除
  44. //if (user != null)
  45. //{
  46. // OnlineUsers.Remove(user);
  47. //}
  48. OnlineUsers = allUsers;
  49. if (!islogin && orgid == clientOrgId && userid == clientId)
  50. {
  51. Thread.Sleep(10000); //延时10秒
  52. connection.Start().ContinueWith(t =>
  53. {
  54. if (!t.IsFaulted)
  55. {
  56. //连接成功,调用Register方法
  57. msgProxy.Invoke("Register", clientOrgId, clientId, clientName, true);
  58. }
  59. else
  60. {
  61. //MessageBox.Show("通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行");
  62. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, "Euro.Transfer.HubTransfer:通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行", true);
  63. }
  64. });
  65. }
  66. });
  67. msgProxy.On<List<UserInfo>>("onlineusers", (allUsers) =>
  68. {
  69. OnlineUsers = allUsers;
  70. });
  71. msgProxy.On("transfertips", () =>
  72. {
  73. msgProxy.Invoke("Register", clientOrgId, clientId, clientName, true);
  74. var sIsTest = Common.ConfigGetValue("", "IsTest");//是否為測試
  75. if (sIsTest == "true")
  76. {
  77. ServiceTools.WriteLog(Debuglog_Path, "HubTransfer.transfertips:" + clientOrgId + "-" + clientId + " " + connection.ConnectionId, true);
  78. }
  79. });
  80. //客户端接收实现,可以用js,也可以用后端接收
  81. var pushtransfer = msgProxy.On<string, string, string, int>("pushtransfer", (orgid, userid, data, index) =>
  82. {
  83. switch (index)
  84. {
  85. case 1:
  86. TransferService.TransferBill(writeOrLogs, orgid, userid, data);
  87. break;
  88. case 2:
  89. TransferService.TransferCus(writeOrLogs, orgid, userid, data);
  90. break;
  91. case 3:
  92. TransferService.TransferPrj(writeOrLogs, orgid, userid, data);
  93. break;
  94. default:
  95. break;
  96. }
  97. msgProxy.Invoke("transferBack", data, index);
  98. });
  99. await connection.Start().ContinueWith(t =>
  100. {
  101. if (!t.IsFaulted)
  102. {
  103. //连接成功,调用Register方法
  104. //msgProxy.Invoke("Register", clientOrgId, clientId, clientName,true);
  105. msgProxy.Invoke("GetOnlineUsers");
  106. }
  107. else
  108. {
  109. //MessageBox.Show("通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行");
  110. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, "Euro.Transfer.HubTransfer:通訊連接失敗!! 請檢查Tracking後臺系統是否正常運行", true);
  111. }
  112. });
  113. //await msgProxy.Invoke("Hello", "Hello World!");
  114. }
  115. catch (Exception ex)
  116. {
  117. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, ex.ToString(), true);
  118. }
  119. }
  120. }
  121. }