Last active
March 29, 2021 14:55
-
-
Save ebicoglu/f7dc22cca2d353f8bf7f68a03e3395b8 to your computer and use it in GitHub Desktop.
SignalR Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
aliases: { | |
"@node_modules": "./node_modules", | |
"@libs": "./wwwroot/libs" | |
}, | |
clean: [ | |
"@libs" | |
], | |
mappings: { | |
"@node_modules/@microsoft/signalr/dist/browser/signalr.js": "@libs/signalr/", | |
"@node_modules/@microsoft/signalr/dist/browser/signalr.js.map": "@libs/signalr/" | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page | |
@model MyCompanyName.MyProjectName.Web.Pages.IndexModel | |
@using MyCompanyName.MyProjectName.Web.Menus | |
@using Volo.Abp.AspNetCore.Mvc.UI.Layout | |
@using Microsoft.AspNetCore.Mvc.Localization | |
@using MyCompanyName.MyProjectName.Localization | |
@using Volo.Abp.Users | |
@inject IPageLayout PageLayout | |
@inject IHtmlLocalizer<MyProjectNameResource> L | |
@{ | |
ViewBag.PageTitle = L["Home"]; | |
PageLayout.Content.Title = L["Home"].Value; | |
PageLayout.Content.MenuItemName = MyProjectNameMenus.Home; | |
} | |
<script> | |
var handleSignalR = function (response){ | |
$("#time").text(response.message); | |
} | |
var makePostRequest = function() { | |
$.ajax({ | |
type: "POST", | |
contentType: "application/json; charset=utf-8", | |
dataType: "json" | |
}); | |
}; | |
document.addEventListener('DOMContentLoaded', function(){ | |
myNotificationManager.messageListeners.notificationProcessMessages.push(handleSignalR); | |
}, false); | |
</script> | |
<h1 id="time"></h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using System.Timers; | |
namespace MyCompanyName.MyProjectName.Web.Pages | |
{ | |
public class IndexModel : MyProjectNamePageModel | |
{ | |
private readonly UiNotificationClient _uiNotificationClient; | |
public IndexModel(UiNotificationClient uiNotificationClient) | |
{ | |
_uiNotificationClient = uiNotificationClient; | |
} | |
public async Task OnGet() | |
{ | |
var timer = new Timer { | |
Interval = 1000, //ticks every 1 second | |
Enabled = true | |
}; | |
timer.Elapsed += async (sender, args) => | |
{ | |
//sends server data to client | |
await _uiNotificationClient.SendNotification("Server time is " + DateTime.Now.ToLongTimeString()); | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
public interface INotificationClient | |
{ | |
Task ReceiveNotificationMessage(string message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var hubName = "notification-hub"; | |
var connection = new signalR.HubConnectionBuilder() | |
.withUrl("/" + hubName) | |
.withAutomaticReconnect() | |
.configureLogging(signalR.LogLevel.Information) | |
.build(); | |
var myNotificationManager = $.extend({}, myNotificationManager, { | |
messageListeners: { | |
notificationProcessMessages: [] | |
} | |
}); | |
connection.on("receiveNotificationMessage", function (message) { | |
if (!myNotificationManager.messageListeners.notificationProcessMessages) { | |
return; | |
} | |
myNotificationManager.messageListeners.notificationProcessMessages.forEach(function (fn) { | |
fn({ | |
message: message | |
}); | |
}); | |
}); | |
connection.start().then(function () { | |
abp.log.info(hubName + " connected."); | |
}).catch(function (err) { | |
abp.log.error(err.toString()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.SignalR; | |
using Volo.Abp.DependencyInjection; | |
public class UiNotificationClient : ITransientDependency | |
{ | |
private readonly IHubContext<UiNotificationHub, INotificationClient> _notificationHub; | |
public UiNotificationClient(IHubContext<UiNotificationHub, INotificationClient> notificationHub) | |
{ | |
_notificationHub = notificationHub; | |
} | |
public async Task SendNotification(string message) | |
{ | |
await _notificationHub | |
.Clients | |
.All | |
.ReceiveNotificationMessage(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.SignalR; | |
using Volo.Abp.DependencyInjection; | |
public class UiNotificationHub : Hub<INotificationClient>, ITransientDependency | |
{ | |
public Task SendNotification(string message) | |
{ | |
return Clients | |
.Client(Context.ConnectionId) | |
.ReceiveNotificationMessage(message); | |
} | |
} |
Author
ebicoglu
commented
Mar 29, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment