-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ChatHub.cs
65 lines (56 loc) · 2.12 KB
/
ChatHub.cs
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
60
61
62
63
64
65
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Platform.DatabaseHandlers.Contexts;
using Platform.DataHandlers;
using Platform.DataHandlers.Interfaces;
using Rokono_Control.DatabaseHandlers;
using Rokono_Control.Models;
namespace RokonoControl
{
public class ChatHub : Hub
{
RokonocontrolContext RokonoContext;
IConfiguration Configuration;
public AutherizationManager AutherizationManager { get; }
public int UserId { get; }
public ChatHub(RokonocontrolContext context, IConfiguration config, IAutherizationManager autherizationManager,IHttpContextAccessor httpContextAccessor)
{
RokonoContext = context;
Configuration = config;
AutherizationManager = (AutherizationManager) autherizationManager;
UserId = AutherizationManager.GetCurrentUser(UserId,httpContextAccessor.HttpContext.Request);
}
[Authorize(Roles = "User")]
public Task Send( string message)
{
var user = Context.User;
if (user != null)
{
var username = user.Claims.FirstOrDefault();// Call the broadcastMessage method to update clients.
return Clients.Others.SendAsync("ReciveMessage", username.Value.ToString(), message);
}
return null;
}
[Authorize(Roles = "User")]
public Task NotificationRecived()
{
var user = Context.User;
if (user != null)
{
var res = string.Empty;
using(var context = new NotificationContext(RokonoContext,Configuration))
{
var notifications = context.GetNewNotifications(UserId);
res = JsonConvert.SerializeObject(notifications);
}
return Clients.Caller.SendAsync("ReciveNotification", res);
}
return null;
}
}
}