Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public X509Certificate2 cert = new X509Certificate2("server.pfx", "enote");
- static void Main(string[] args)
- {
- var url = "http://192.168.0.61:8080/";
- using (WebApp.Start<Startup>(url))
- {
- Console.WriteLine($"Статус: Сервер запущен. Адрес: {url}");
- Console.ReadLine();
- }
- }
- }
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- app.UseCors(CorsOptions.AllowAll);
- app.MapSignalR("/enote", new HubConfiguration());
- }
- }
- public interface IClient
- {
- void ParticipantDisconnection(string name);
- void ParticipantReconnection(string name);
- void ParticipantLogin(User client);
- void ParticipantLogout(string name);
- void BroadcastMessage(string sender, string message);
- void UnicastMessage(string sender, string message);
- }
- public class ChatHub : Hub<IClient>
- {
- private static ConcurrentDictionary<string, User> ChatClients = new ConcurrentDictionary<string, User>();
- public override Task OnDisconnected(bool stopCalled)
- {
- var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
- if (userName != null)
- {
- Clients.Others.ParticipantDisconnection(userName);
- Console.WriteLine($"<> {userName} отключен");
- }
- return base.OnDisconnected(stopCalled);
- }
- public override Task OnReconnected()
- {
- var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
- if (userName != null)
- {
- Clients.Others.ParticipantReconnection(userName);
- Console.WriteLine($"== {userName} переподключен");
- }
- return base.OnReconnected();
- }
- public List<User> Login(string name, string password, byte[] photo)
- {
- if (!ChatClients.ContainsKey(name))
- {
- Console.WriteLine($"++ {name} выходит в сеть");
- List<User> users = new List<User>(ChatClients.Values);
- User newUser = new User { Name = name, Password = password, ID = Context.ConnectionId, Photo = photo };
- var added = ChatClients.TryAdd(name, newUser);
- if (!added) return null;
- Clients.CallerState.UserName = name;
- Clients.Others.ParticipantLogin(newUser);
- return users;
- }
- return null;
- }
- public void Logout()
- {
- var name = Clients.CallerState.UserName;
- if (!string.IsNullOrEmpty(name))
- {
- User client = new User();
- ChatClients.TryRemove(name, out client);
- Clients.Others.ParticipantLogout(name);
- Console.WriteLine($"-- {name} ушел из сети");
- }
- }
- public void BroadcastChat(string message)
- {
- var name = Clients.CallerState.UserName;
- if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(message))
- {
- Clients.Others.BroadcastMessage(name, message);
- }
- }
- public void UnicastChat(string recepient, string message)
- {
- var sender = Clients.CallerState.UserName;
- if (!string.IsNullOrEmpty(sender) && recepient != sender &&
- !string.IsNullOrEmpty(message) && ChatClients.ContainsKey(recepient))
- {
- User client = new User();
- ChatClients.TryGetValue(recepient, out client);
- Clients.Client(client.ID).UnicastMessage(sender, message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement