Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. class Program
  2. {
  3. public X509Certificate2 cert = new X509Certificate2("server.pfx", "enote");
  4.  
  5. static void Main(string[] args)
  6. {
  7. var url = "http://192.168.0.61:8080/";
  8. using (WebApp.Start<Startup>(url))
  9. {
  10. Console.WriteLine($"Статус: Сервер запущен. Адрес: {url}");
  11. Console.ReadLine();
  12. }
  13. }
  14.  
  15. }
  16.  
  17. public class Startup
  18. {
  19. public void Configuration(IAppBuilder app)
  20. {
  21. app.UseCors(CorsOptions.AllowAll);
  22. app.MapSignalR("/enote", new HubConfiguration());
  23. }
  24. }
  25.  
  26. public interface IClient
  27. {
  28. void ParticipantDisconnection(string name);
  29. void ParticipantReconnection(string name);
  30. void ParticipantLogin(User client);
  31. void ParticipantLogout(string name);
  32. void BroadcastMessage(string sender, string message);
  33. void UnicastMessage(string sender, string message);
  34. }
  35.  
  36. public class ChatHub : Hub<IClient>
  37. {
  38. private static ConcurrentDictionary<string, User> ChatClients = new ConcurrentDictionary<string, User>();
  39.  
  40. public override Task OnDisconnected(bool stopCalled)
  41. {
  42. var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
  43. if (userName != null)
  44. {
  45. Clients.Others.ParticipantDisconnection(userName);
  46. Console.WriteLine($"<> {userName} отключен");
  47. }
  48. return base.OnDisconnected(stopCalled);
  49. }
  50.  
  51. public override Task OnReconnected()
  52. {
  53. var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
  54. if (userName != null)
  55. {
  56. Clients.Others.ParticipantReconnection(userName);
  57. Console.WriteLine($"== {userName} переподключен");
  58. }
  59. return base.OnReconnected();
  60. }
  61.  
  62. public List<User> Login(string name, string password, byte[] photo)
  63. {
  64. if (!ChatClients.ContainsKey(name))
  65. {
  66. Console.WriteLine($"++ {name} выходит в сеть");
  67. List<User> users = new List<User>(ChatClients.Values);
  68. User newUser = new User { Name = name, Password = password, ID = Context.ConnectionId, Photo = photo };
  69. var added = ChatClients.TryAdd(name, newUser);
  70. if (!added) return null;
  71. Clients.CallerState.UserName = name;
  72. Clients.Others.ParticipantLogin(newUser);
  73. return users;
  74. }
  75. return null;
  76. }
  77.  
  78. public void Logout()
  79. {
  80. var name = Clients.CallerState.UserName;
  81. if (!string.IsNullOrEmpty(name))
  82. {
  83. User client = new User();
  84. ChatClients.TryRemove(name, out client);
  85. Clients.Others.ParticipantLogout(name);
  86. Console.WriteLine($"-- {name} ушел из сети");
  87. }
  88. }
  89.  
  90. public void BroadcastChat(string message)
  91. {
  92. var name = Clients.CallerState.UserName;
  93. if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(message))
  94. {
  95. Clients.Others.BroadcastMessage(name, message);
  96. }
  97. }
  98.  
  99. public void UnicastChat(string recepient, string message)
  100. {
  101. var sender = Clients.CallerState.UserName;
  102. if (!string.IsNullOrEmpty(sender) && recepient != sender &&
  103. !string.IsNullOrEmpty(message) && ChatClients.ContainsKey(recepient))
  104. {
  105. User client = new User();
  106. ChatClients.TryGetValue(recepient, out client);
  107. Clients.Client(client.ID).UnicastMessage(sender, message);
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement