What is SignalR and Why Should I Use It?
原文地址:
What is SignalR and why should I use it?
When I first heard about SignalR, I was not sure what was the point of it. The official description says that SignalR is a library that can be used with any ASP.NET application to add real-time communication. But since what they meant by real-time was not totally clear to me, and the only example was a chat room, I pushed it aside for a while.
When I started to work on my side project, I also started thinking about SignalR again. One of the things I want to do was refresh the schedule as soon as a change was made: if a user makes a modification, it should be visible to all other users. The classic way to do something like that would be to call the server at regular intervals to get the status of the schedule, but to have pseudo real-time update, you must call the server pretty often.
With SignalR, the server can call a JavaScript methods on all the clients by itself when updates are required. The library will handle the connection needed to achieve this: by default WebSocket
is used, but it will fallback automatically to older connections types if WebSocket
is not available in the browser. The JavaScript can also call the server: this can already be done with AJAX, but if two-way communication is needed it may be easier and cleaner to do it all with SignalR.
So, using a real-time library is the way to go if you want to build an application that requires collaboration between users. Common uses cases includes editors, social networks, chats or a schedule like my project.
Getting started with SignalR is pretty simple. Here is an example with my side project: sessions for an event can be modified by any user, and when a session is modified the change is visible for all the user currently logged in the application.
On the Server
First, you must add the Microsoft ASP.NET SignalR package to your ASP.NET application using NuGet, along with the jQuery package if you don’t already have it.
After this, you must create a class that derives from the Hub
class. All the methods of the hub can be called from JavaScript. Also, those methods can call JavaScript methods on the client.
Hide Copy Code
namespace EventScheduling
{
public class SessionsHub : Hub
{
public void SessionModified(ScheduledSession scheduledSession)
{
// Call the JavaScript method sendSessionChanged on all the clients
Clients.All.sendSessionChanged(scheduledSession);
}
}
}
To enable communications between the hub and the JavaScript, you must also enable SignalR
in the Configuration
method in the Startup
class of your application.
Hide Copy Code
namespace EventScheduling
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
On the Client
On the client, you must include the jquery.signalR-2.2.0.js file containing the JavaScript for the SignalR
library and the package ~/signalr/hubs. This package is automatically generated from your Hub
classes and contains all the JavaScript required to call the server and receive message from the server.
Hide Copy Code
<script src="/Scripts/jquery-1.10.2.js"></script>
After this, when the page is loaded and ready, you must declare the JavaScript function called by the server-side code. Since the Hub
class is called SessionsHub
and the SessionModified
method of the hub
class calls the sendSessionChanged
method, this function must be declared in the $.connection.sessionsHub.client.sendSessionChanged
variable.
Once this function is created, you must also connect to the hubs using the $.connection.hub.start
method. You can also add initialization code in the done()
callback which will be called when the connection is started.
Hide Copy Code
$(document).ready(function () {
// Create a function that the hub can call to broadcast messages.
$.connection.sessionsHub.client.sendSessionChanged = function (session) {
// Display the session again using the new data received from the server
});
}; // Start the connection and create a function that the hub will call when loaded
$.connection.hub.start().done($("#Sessions").on("click", ".Session", function (event) {
var that = $(this);
var currentSession = that.data("session");
currentSession.Title = currentSession.Title + "Clicked";
// Call the server, which will notify all clients of the change in the title
$.connection.sessionsHub.server.sessionModified(currentSession);
}));
});
In the example, when the hubs are done starting, a click handler is added to each session on the page. When the users clicks a session, the word Clicked
is added to the title of that session, and all browsers showing the page are notified. Since the SessionModified
method was declared in the SessionsHub
class of the server, it can be called from JavaScript using the $.connection.sessionsHub.server.sessionModified
method.
With only this small bit of code, you now have two-way communications between the client and the server, which you can use to update data in real-time for your users. I will use SignalR in future projects, that’s for sure!
What is SignalR and Why Should I Use It?的更多相关文章
- SignalR系列续集[系列8:SignalR的性能监测与服务器的负载测试]
目录 SignalR系列目录 前言 也是好久没写博客了,近期确实很忙,嗯..几个项目..头要炸..今天忙里偷闲.继续我们的小系列.. 先谢谢大家的支持.. 我们来聊聊SignalR的性能监测与服务器的 ...
- ABP文档 - SignalR 集成
文档目录 本节内容: 简介 安装 服务端 客户端 连接确立 内置功能 通知 在线客户端 帕斯卡 vs 骆峰式 你的SignalR代码 简介 使用Abp.Web.SignalR nuget包,使基于应用 ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#signalR 后台创建了一个DntHub的集线器 前台在调用的时候出现了问题(经检查是代理对象 ...
- 基于SignalR实现B/S系统对windows服务运行状态的监测
通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...
- SignalR SelfHost实时消息,集成到web中,实现服务器消息推送
先前用过两次SignalR,但是中途有段时间没弄了,今天重新弄,发现已经忘得差不多了,做个笔记! 首先创建一个控制台项目Nuget添加引用联机搜索:Microsoft.AspNet.SignalR.S ...
- SignalR系列目录
[置顶]用SignalR 2.0开发客服系统[系列1:实现群发通讯] [置顶]用SignalR 2.0开发客服系统[系列2:实现聊天室] [置顶]用SignalR 2.0开发客服系统[系列3:实现点对 ...
- 基于SignalR的消息推送与二维码描登录实现
1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...
- XAMARIN.ANDROID SIGNALR 实时消息接收发送示例
SignalR 是一个开发实时 Web 应用的 .NET 类库,使用 SignalR 可以很容易的构建基于 ASP.NET 的实时 Web 应用.SignalR 支持多种服务器和客户端,可以 Host ...
- ABP源码分析三十二:ABP.SignalR
Realtime Realtime是ABP底层模块提供的功能,用于管理在线用户.它是使用SignalR实现给在线用户发送通知的功能的前提 IOnlineClient/OnlineClient: 封装在 ...
随机推荐
- 浅谈Linux系统的启动流程
Linux系统的启动时通过读取不同的配置文件,执行相应的Shell脚本完成的.当然本文只是简单的从文件的角度分析,更深层次的本文没涉及. 主要读取了以下文件: /boot/grub/grub.con ...
- [汇编语言]-第十章 ret,retf,call指令
1- ret 相当于 pop IP;用栈中数据,修改IP内容.从而实现近转移. 执行后(IP)=0, CS:IP指向代码段的第一条指令. assume cs:code stack segment db ...
- curl批量伪造数据
<?php set_time_limit(); $url = "http://www.ciweishixi.dev/app.php?c=form&a=submitActivit ...
- PHP使用curl伪造IP地址和header信息
curl虽然功能强大,但是只能伪造$_SERVER["HTTP_X_FORWARDED_FOR"],对于大多数IP地址检测程序来说,$_SERVER["REMOTE_AD ...
- 还是畅通工程(1233 并查集+kruskal)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 【转】WPF - 第三方控件
WPF - 第三方控件 目前第三方控件在网上形成巨大的共享资源,其中包括收费的也有免费的,有开源的也有不开源的,合理的使用第三方控件将使项目组的工作事半功倍.比如项目中有些复杂的业务逻辑.有些绚丽的效 ...
- 使用fragment,Pad手机共用一套代码
项目代码结构: 1:MainActivity.java package com.example.fgtest; import android.app.Activity; import android. ...
- USB系列之一:列出你的USB设备
USB现在已经成为PC机必不可少的接口之一,几乎所有的设备都可以接在USB设备上,USB键盘.鼠标.打印机.摄像头,还有常用的U盘等等,从本篇文章开始,将集中篇幅介绍一下在DOS中使用USB设备的方法 ...
- css案例学习之div a实现立体菜单
效果 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- Android自定义垂直滚动自动选择日期控件
------------------本博客如未明正声明转载,皆为原创,转载请注明出处!------------------ 项目中需要一个日期选择控件,该日期选择控件是垂直滚动,停止滚动时需要校正日期 ...