Introduction

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

Installation

Server Side

Install Abp.Web.SignalRnuget 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 Microsoft.AspNet.SignalR package to your Web project if not installed before (See SignalR documents 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 Abp.Web.Resources package (It's already installed in startup templates). We should include it after signalr hubs:

<script src="~/signalr/hubs"></script>
<script src="~/Abp/Framework/scripts/libs/abp.signalr.js"></script>

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禁用自动添加这些线连接:

<script>
abp.signalr = abp.signalr || {};
abp.signalr.autoConnect = false;
</script>

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 hub

chatHub.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 SignalR documentation for detailed information on SignalR.

ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)的更多相关文章

  1. ABP框架系列之四十:(Notification-System-通知系统)

    Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...

  2. ABP框架系列之四十一:(Nuget-Packages-Nuget包)

    Packages ASP.NET Boilerplate is distributed on nuget. Here, a list of all official packages. Abp Cor ...

  3. ABP框架系列之四:(Repositories-仓库)

    "Mediates between the domain and data mapping layers using a collection-like interface for acce ...

  4. ABP框架系列之四十九:(Startup-Configuration-启动配置)

    ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup. A ...

  5. ABP框架系列之四十二:(Object-To-Object-Mapping-对象映射)

    Introduction It's a common to map a similar object to another object. It's also tedious and repeatin ...

  6. ABP框架系列之十七:(Data-Filters-数据过滤)

    Introduction It's common to use the soft-deletepattern which is used to not delete an entity from da ...

  7. ABP框架系列之四十四:(OWIN)

    If you are using both of ASP.NET MVC and ASP.NET Web API in your application, you need to add Abp.Ow ...

  8. ABP框架系列之四十六:(Setting-Management-设置管理)

    Introduction Every application need to store some settings and use these settings in somewhere in th ...

  9. ABP框架系列之四十八:(Specifications-规范)

    Introduction Specification pattern is a particular software design pattern, whereby business rules c ...

随机推荐

  1. 学习 MeteoInfo二次开发教程(二)

    1.注意TSB_Select_Click等几个名称要改为toolStripButton2_Click等. 2.以下代码的位置与public Form1()函数并行. ToolStripButton _ ...

  2. doris 0.9.0版本docker镜像制作与使用

    1. 安装docker 详情请参见本人博客 2. 编译doris 详情请参见doris官网文档 3. 在编译好的doris output文件夹下编写两个Dockerfile 3.1  Dockerfi ...

  3. WordPress版微信小程序2.1.5版发布

    WordPress版微信小程序功能已经基本完善,利用这套程序,搭配WordPress提供的rest api,WordPress网站的站长可以快速搭建属于自己的网站微信小程序 . WordPress版微 ...

  4. Cookie的存活时间

    1. 默认情况下,cookie数据保存到内存里,当浏览器关闭后,Cookie数据被销毁 2. 持久化存储: setMaxAge(int seconds) 1. 正数:将Cookie数据写到硬盘的文件中 ...

  5. JS购物车编辑

    实现了:第一件商品的加减实现了:全选/全不选(使用prop而不是attr)实现了:删除(遮罩层) 未实现:第二件商品的删除未实现:小计及应付款额的初始化(写死的) 计算小数乘法时,要先乘100 < ...

  6. 使用openpyxl复制整张sheet

    通过无能的baidu逛了一圈,发现有两三段能用的代码,不过参考之下,发现还有不足的: 不能拷贝有合并格式的sheet.没有拷贝cell的相关格式(填充.边框.对齐)等参数 所以通过bing继续发掘,最 ...

  7. 机器学习linux系统环境安装

    机器学习linux系统环境安装 安装镜像下载 可以自己去ubuntu官方网站按照提示下载amd64的desktop版本 或者考虑到国内镜像站点下载,如tuna,163, ali等 课程使用最新的17. ...

  8. 移动端过禁止输入emoji表情实现方案

    最近手头上的项目有一个需求就是输入框不能输入表情,然后就各种在网上找资料,网上好多人给的方案是: str = str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uD ...

  9. mysql_day02

    MySQL-Day01回顾1.MySQL的特点 1.关系型数据库 2.跨平台 3.支持多种编程语言2.MySQL的启动和连接 1.服务端启动 sudo /etc/init.d/mysql start| ...

  10. 命名空间 extern的用法 static全局变量

    std是标准库中的命名空间: 关于extern的用法可以参考文献http://blog.163.com/sunjinxia%40126/blog/static/94984879201312145021 ...