aboutsummaryrefslogtreecommitdiff
path: root/backend/api/api/Services/ChatHub.cs
blob: 790b4de6826ea5bd965ba74ed74ceab1ba220394 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using api.Models;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading.Tasks;



namespace api.Services
{
    public class ChatHub :Hub
    {
        static public readonly Dictionary<string,string> Users=new Dictionary<string,string>();
        private readonly IJwtToken _tokenService;
        public ChatHub(IJwtToken tokenService)
        {
            _tokenService=tokenService;
        }

        public override async Task OnConnectedAsync()
        {
            string token=Context.GetHttpContext().Request.Query["access_token"];
            if (token == null)
                return;
            string id=_tokenService.TokenToId(token);
            Users.Add(Context.ConnectionId,id);
            //await SendDirect(id, "poruka");
            //await Send(Context.ConnectionId);
            await base.OnConnectedAsync();

        }
        public override async Task OnDisconnectedAsync(Exception? exception)
        {
            Users.Remove(Context.ConnectionId);
        }
        public static bool CheckUser(string id)
        {
            var users=Users.Values;
            foreach (var user in users)
            {
                if(user==id)
                    return true;
            }
            return false;
        }
        public static List<string> getAllConnectionsOfUser(string id)
        {
            List<string> keys=new List<string>();
            foreach (var user in Users)
            {
                if(user.Value==id)
                    keys.Add(user.Key);
            }
            return keys;
        }
    }
    

}