The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

IRCClientHub.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Microsoft.AspNet.SignalR;
  2. using Microsoft.AspNet.SignalR.Hubs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using Teknik.Attributes;
  8. using Teknik.SignalR;
  9. using System.Threading.Tasks;
  10. using System.Timers;
  11. using Teknik.Areas.Users.Utility;
  12. using Teknik.Areas.Users.Models;
  13. using Teknik.Models;
  14. using Teknik.Configuration;
  15. using Teknik.Utilities;
  16. namespace Teknik.Hubs
  17. {
  18. [HubName("ircClient")]
  19. public class IRCClientHub : Hub
  20. {
  21. private static Dictionary<string, string> _Connections = new Dictionary<string, string>();
  22. private static Dictionary<string, Tuple<IRCClient, Timer>> _Clients = new Dictionary<string, Tuple<IRCClient, Timer>>();
  23. private const int _DisconnectTimeout = 30; // Timeout to disconnect from IRC server after client disconnects from hub (Seconds)
  24. public IRCClientHub()
  25. {
  26. }
  27. public override Task OnDisconnected(bool stopCalled)
  28. {
  29. // Get the username for this connection
  30. string username = string.Empty;
  31. if (Context != null)
  32. {
  33. if (_Connections.ContainsKey(Context.ConnectionId))
  34. {
  35. // Get the username
  36. username = _Connections[Context.ConnectionId];
  37. // Remove this connection from the list
  38. _Connections.Remove(Context.ConnectionId);
  39. }
  40. }
  41. // Start disconnect timer for this user
  42. if (_Clients.ContainsKey(username))
  43. {
  44. // Start disconnect timer
  45. _Clients[username].Item2.Start();
  46. }
  47. return base.OnDisconnected(stopCalled);
  48. }
  49. public string Connect()
  50. {
  51. // Create guest username
  52. string username = "Guest_" + StringHelper.RandomString(6, "0123456789");
  53. Connect(username, string.Empty);
  54. return username;
  55. }
  56. public bool Connect(string username, string password)
  57. {
  58. bool success = true;
  59. // If the password is supplied, verify the password
  60. if (!string.IsNullOrEmpty(password))
  61. {
  62. using (TeknikEntities db = new TeknikEntities())
  63. {
  64. User user = UserHelper.GetUser(db, username);
  65. if (user != null)
  66. {
  67. Config config = Config.Load();
  68. success = UserHelper.UserPasswordCorrect(db, config, user, password);
  69. }
  70. else
  71. {
  72. success = false;
  73. }
  74. }
  75. }
  76. if (success)
  77. {
  78. // Update this connection with the username associated we want to associate with it
  79. if (Context != null)
  80. {
  81. if (!_Connections.ContainsKey(Context.ConnectionId))
  82. {
  83. _Connections.Add(Context.ConnectionId, string.Empty);
  84. }
  85. _Connections[Context.ConnectionId] = username;
  86. }
  87. // Add the client for this user if it doesn't exist
  88. if (!_Clients.ContainsKey(username))
  89. {
  90. // Client doesn't exist, so create it
  91. Timer disconnectTimer = new Timer(_DisconnectTimeout * 1000);
  92. disconnectTimer.Elapsed += (sender, e) => DisconnectTimer_Elapsed(sender, e, username);
  93. // Add the new connection to the main list
  94. _Clients.Add(username, new Tuple<IRCClient, Timer>(new IRCClient(Clients.Caller), disconnectTimer));
  95. }
  96. // Stop the disconnect timer because we have reconnected
  97. _Clients[username].Item2.Stop();
  98. // Reassociate this connection with the user
  99. _Clients[username].Item1.CallerContext = Clients.Caller;
  100. // Connect
  101. _Clients[username].Item1.Connect(username, password);
  102. }
  103. return success;
  104. }
  105. public void SendMessage(string location, string message)
  106. {
  107. string username = GetConnectionUser();
  108. if (_Clients.ContainsKey(username))
  109. {
  110. _Clients[username].Item1.SendMessage(location, message);
  111. }
  112. }
  113. public void SendRawMessage(string message)
  114. {
  115. string username = GetConnectionUser();
  116. if (_Clients.ContainsKey(username))
  117. {
  118. _Clients[username].Item1.SendRawMessage(message);
  119. }
  120. }
  121. private void DisconnectTimer_Elapsed(object sender, ElapsedEventArgs e, string username)
  122. {
  123. if (_Clients.ContainsKey(username))
  124. {
  125. // Disconnect from the IRC session
  126. _Clients[username].Item1.Disconnect();
  127. // Remove the irc client
  128. _Clients.Remove(username);
  129. }
  130. }
  131. private string GetConnectionUser()
  132. {
  133. if (Context != null)
  134. {
  135. if (_Connections.ContainsKey(Context.ConnectionId))
  136. {
  137. return _Connections[Context.ConnectionId];
  138. }
  139. }
  140. return string.Empty;
  141. }
  142. }
  143. }