博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)
阅读量:5776 次
发布时间:2019-06-18

本文共 5856 字,大约阅读时间需要 19 分钟。

Introduction

 nuget package makes it easily to use SignalR in ASP.NET Boilerplate based applications. See  for detailed information on SignalR.

Installation

Server Side

Install nuget package to your project (generally to your Web layer) and add a dependency to your module:

[DependsOn(typeof(AbpWebSignalRModule))]public class YourProjectWebModule : AbpModule{    //...}

Then use MapSignalR method in your OWIN startup class as you always do:

[assembly: OwinStartup(typeof(Startup))]namespace MyProject.Web{    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.MapSignalR();            //...        }    }}

Note: Abp.Web.SignalR only depends on Microsoft.AspNet.SignalR.Core package. So, you will also need to install  package to your Web project if not installed before (See  for more).

注:abp.web.signalr只取决于microsoft.aspnet.signalr.core包。所以,您还需要安装microsoft.aspnet.signalr包到您的Web项目,如果没有安装前(见更多的signalr文件)。

Client Side

abp.signalr.js script should be included to the page. It's located in  package (It's already installed in startup templates). We should include it after signalr hubs:

That's all. SignalR is properly configured and integrated to your project.

Connection Establishment(建立连接

ASP.NET Boilerplate automatically connects to the server (from the client) when abp.signalr.js is included to your page. This is generally fine. But there may be cases you don't want to it. You can add these lines just before including abp.signalr.js to disable auto connecting:

ASP.NET样板自动连接到服务器(从客户端)当abp.signalr.js包含到你的页面。这通常很好。但有些情况下你不想这样做。你可以在包括abp.signalr.js禁用自动添加这些线连接:

In this case, you can call abp.signalr.connect() function manually whenever you need to connect to the server.

ASP.NET Boilerplate also automatically reconnects to the server (from the client) when client disconnects, if abp.signalr.autoConnect is true.

"abp.signalr.connected" global event is triggered when client connects to the server. You can register to this event to take actions when the connection is successfully established. See javascript event bus documentation for more about client side events.

“abp.signalr.connected“ global事件被触发时,客户端连接到服务器。您可以注册此事件,在连接成功建立时采取行动。有关客户端事件的更多内容,请参见JavaScript事件总线文档。

Built-In Features

You can use all power of SignalR in your applications. In addition, Abp.Web.SignalR package implements some built-in features.

Notification

Abp.Web.SignalR package implements IRealTimeNotifier to send real time notifications to clients (see notification system). Thus, you users can get real time push notifications.

Online Clients(在线客户

ASP.NET Boilerplate provides IOnlineClientManager to get information about online users (inject IOnlineClientManager and use GetByUserIdOrNull, GetAllClients, IsOnline methods for example). IOnlineClientManager needs a communication infrastructure to properly work. Abp.Web.SignalR package provides the infrastructure. So, you can inject and use IOnlineClientManager in any layer of your application if SignalR is installed.

ABP提供 ionlineclientmanager 获取在线用户的信息(注入ionlineclientmanager getbyuseridornull isOnline getallclients和使用,方法,例如)。需要一个ionlineclientmanager通信基础设施的工作时间。abp.web.signalr包提供的基础设施。所以,你可以在任何层ionlineclientmanager注入和使用您的应用程序,如果signalr是安装。

PascalCase vs. camelCase

Abp.Web.SignalR package overrides SignalR's default ContractResolver to use CamelCasePropertyNamesContractResolver on serialization. Thus, we can have classes have PascalCase properties on the server and use them as camelCase on the client for sending/receiving objects (because camelCase is preferred notation in javascript). If you want to ignore this for your classes in some assemblies, then you can add those assemblies to AbpSignalRContractResolver.IgnoredAssemblies list.

Your SignalR Code

Abp.Web.SignalR package also simplifies your SignalR code. Assume that we want to add a Hub to our application:

public class MyChatHub : Hub, ITransientDependency{    public IAbpSession AbpSession { get; set; }    public ILogger Logger { get; set; }    public MyChatHub()    {        AbpSession = NullAbpSession.Instance;        Logger = NullLogger.Instance;    }    public void SendMessage(string message)    {        Clients.All.getMessage(string.Format("User {0}: {1}", AbpSession.UserId, message));    }    public async override Task OnConnected()    {        await base.OnConnected();        Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);    }    public async override Task OnDisconnected(bool stopCalled)    {        await base.OnDisconnected(stopCalled);        Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);    }}

We implemented ITransientDependency to simply register our hub to dependency injection system (you can make it singleton based on your needs). We property-injected the session and logger.

SendMessage is a method of our hub that can be used by clients. We call getMessage function of all clients in this method. We can use AbpSession to get current user id (if user logged in) as you see. We also overridedOnConnected and OnDisconnected, which is just for demonstration and not needed here actually.

SendMessage是一个客户可以使用我们的中心法。我们称这种方法GetMessage函数的所有客户。我们可以用abpsession获取当前用户ID(如果用户登录)你看。

我们也overridedonconnected和ondisconnected,这只是示范,没有必要在这里实际上。

Here, the client side javascript code to send/receive messages using our hub.

var chatHub = $.connection.myChatHub; //get a reference to the hubchatHub.client.getMessage = function (message) { //register for incoming messages    console.log('received message: ' + message);};abp.event.on('abp.signalr.connected', function() { //register for connect event    chatHub.server.sendMessage("Hi everybody, I'm connected to the chat!"); //send a message to the server});

Then we can use chatHub anytime we need to send message to the server. See  for detailed information on SignalR.

转载地址:http://wueux.baihongyu.com/

你可能感兴趣的文章
.NET开源现状
查看>>
可替换元素和非可替换元素
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>
搭建vsftpd服务器,使用匿名账户登入
查看>>
AMD改善Linux驱动,支持动态电源管理
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
Java虚拟机管理的内存运行时数据区域解释
查看>>
人人都会深度学习之Tensorflow基础快速入门
查看>>
ChPlayer播放器的使用
查看>>
js 经过修改改良的全浏览器支持的软键盘,随机排列
查看>>
Mysql读写分离
查看>>
Oracle 备份与恢复学习笔记(5_1)
查看>>
Oracle 备份与恢复学习笔记(14)
查看>>
分布式配置中心disconf第一部(基本介绍)
查看>>
Scenario 9-Shared Uplink Set with Active/Active uplink,802.3ad(LACP)-Flex-10
查看>>
UML类图中的六种关系
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
mysql(待整理)
查看>>