EnVisageOnline/Main/Source/EnVisage/Code/Notifications/ClientNotificationHub.cs

100 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.ComponentModel.DataAnnotations;
using EnVisage.Models;
using System.Web.Mvc;
using EnVisage.Code;
using Microsoft.AspNet.Identity;
using EnVisage.Code.BLL;
using NLog;
namespace EnVisage.Code.Hubs.Notifications
{
[HubName("clientnotificationsHub")]
public class ClientNotificationHub : Hub
{
protected Logger _logger = NLog.LogManager.GetCurrentClassLogger();
public void TaskFinished(Guid taskId, NotificationModel model)
{
var hub = GlobalHost.ConnectionManager.GetHubContext<ClientNotificationHub>();
if (model.ParentId != Guid.Empty)
{
hub.Clients.Group(model.ParentId.ToString()).TaskFinished(model.Id.ToString(), model.clone());
}
else
{
hub.Clients.All.TaskFinished(model.Id.ToString(), model.clone());
}
}
public void TaskStarted(Guid taskId, NotificationModel model)
{
var hub = GlobalHost.ConnectionManager.GetHubContext<ClientNotificationHub>();
if (model.ParentId != Guid.Empty)
{
hub.Clients.Group(model.ParentId.ToString()).TaskStarted(model.Id.ToString(), model.clone());
}
else
{
hub.Clients.All.TaskStarted(model.Id.ToString(), model.clone());
}
}
public void SendNotification(Guid taskId, NotificationModel model)
{
var hub = GlobalHost.ConnectionManager.GetHubContext<ClientNotificationHub>();
if (model.ParentId != Guid.Empty)
{
hub.Clients.Group(model.ParentId.ToString()).addNotification(model.Id.ToString(), model.clone());
}
else
{
hub.Clients.All.addNotification(model.Id.ToString(), model.clone());
}
}
public void RemoveNotification(Guid taskId, NotificationModel model)
{
var hub = GlobalHost.ConnectionManager.GetHubContext<ClientNotificationHub>();
if (model.ParentId != Guid.Empty)
{
hub.Clients.Group(model.ParentId.ToString()).removeNotification(model.Id.ToString(), model.clone());
}
else
{
hub.Clients.All.addNotification(model.Id.ToString(), model.clone());
}
}
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
return base.OnDisconnected(stopCalled);
}
public async Task registerWithHub()
{
try {
string userId = this.Context.User.Identity.GetID();
if (userId != Guid.Empty.ToString())
{
await Groups.Add(Context.ConnectionId, userId);
Clients.Group(userId).UserMembershipSet(Context.ConnectionId + " added to group");
}
}
catch (Exception e)
{
_logger.Fatal("Error in client register", e);
}
}
}
}