I want to use SignalR
in my project for real time updates.
我想在我的项目中使用SignalR实时更新。
My project is developed in WebForms
.
我的项目是在WebForms中开发的。
I searched for for 3,4 days but all I found were MVC examples. Can anyone suggest a solution?
我搜索了3,4天,但我发现的都是MVC示例。有人能提出解决方案吗?
3 个解决方案
#1
12
You can use SignalR with webforms. See below for an example from the tutorial here
你可以在webforms中使用SignalR。下面是本教程中的一个示例
-
Create a new ASP.NET WebForms project targeting .NET Framework 4.5 or later
创建一个新的ASP。NET WebForms项目针对。NET Framework 4.5或更高版本
-
Change the home page to contain the following
将主页更改为包含以下内容
<asp:content runat="server" id="BodyContent" contentplaceholderid="MainContent"> <h3>Log Items</h3> <asp:listview id="logListView" runat="server" itemplaceholderid="itemPlaceHolder" clientidmode="Static" enableviewstate="false"> <layouttemplate> <ul id="logUl"> <li runat="server" id="itemPlaceHolder"></li> </ul> </layouttemplate> <itemtemplate> <li><span class="logItem"><%#Container.DataItem.ToString() %></span></li> </itemtemplate> </asp:listview> </asp:content>
-
Edit the default.aspx.cs codebehind file to include the following event
编辑default . aspx。cs码后文件包含以下事件
protected void Page_Load(object sender, EventArgs e) { var myLog = new List<string>(); myLog.Add(string.Format("{0} - Logging Started", DateTime.UtcNow)); logListView.DataSource = myLog; logListView.DataBind(); }
-
Add SignalR packages via NuGet. (Trying searching for "Microsoft ASP.Net SignalR JS" and "Microsoft ASP.Net SignalR JS")
通过NuGet添加信号包。(尝试搜索“微软ASP”。Net SignalR JS和Microsoft ASP。净SignalR JS”)
-
Create a Hub class
创建一个中心类
public class LogHub : Hub { public static readonly System.Timers.Timer _Timer = new System.Timers.Timer(); static LogHub() { _Timer.Interval = 2000; _Timer.Elapsed += TimerElapsed; _Timer.Start(); } static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { var hub = GlobalHost.ConnectionManager.GetHubContext("LogHub"); hub.Clients.All.logMessage(string.Format("{0} - Still running", DateTime.UtcNow)); } }
-
Setup the following script block at the bottom of your page (your jquery and jquery.signalr version may vary)
在页面底部设置下面的脚本块(jquery和jquery)。signalr版本可能会有所不同)
<script src="Scripts/jquery.1.7.1.min.js"></script> <script src="Scripts/jquery.signalR-1.0.0-rc1.min.js"></script> <script src="http://www.codeproject.com/signalr/hubs" type="text/javascript"></script> <script type="text/javascript"> $(function() { var logger = $.connection.logHub; logger.client.logMessage = function(msg) { $("#logUl").append("<li>" + msg + "</li>"); }; $.connection.hub.start(); }); </script>
-
Add the following to the Application_Start event handler in global.asax.cs
在global.asax.cs中添加以下的Application_Start事件处理程序。
void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHubs(); }
#2
13
The answer provided by @Stephen is now outdated as it does not apply to the latest Version of SignalR (v2.2.0). Also there are few other things that are not mentioned which IMHO might help future readers to quickly get started with SignalR using the Good old Webforms framework. The solution might appear tedious but it is not. I hope it helps people who visit this page looking to get help on SignalR for Webforms.
@Stephen提供的答案现在已经过时了,因为它不适用于最新版本的SignalR (v2.2.0)。另外,IMHO可能会帮助未来的读者使用优秀的旧Webforms框架快速开始使用SignalR。这个解决方案可能看起来单调乏味,但实际上并非如此。我希望它能帮助那些访问这个页面的人在web表单的SignalR上获得帮助。
Pre-Reqs: (Versions used by me are in brackets) . I have NOT tested this solution on other versions
预请求:(我使用的版本在括号中)。我还没有在其他版本上测试这个解决方案
- MS Visual Studio 2015/2013. (2015) On Win-7 x64
- MS Visual Studio 2015/2013。在win 7 x64(2015)
- .Net FrameWork version 4.5 or Higher (4.5.2)
- .Net框架4.5或更高版本(4.5.2)
- SignalR version 2.2.0 from NuGet (Release Date 01/13/2015)
- 来自NuGet的SignalR 2.2.0版本(发布日期01/13/2015)
- jQuery ver 1.6.4
- jQuery版本1.6.4,
- Owin v1.0.0 and few others like Json, owin.Security etc... (see packages.config)
- Owin v1.0.0和很少其他类似Json的版本。安全等等……(见packages.config)
- IIS v7.0 and above. Works on IIS Express version 10.0 that ships with VS2015.
- IIS v7.0以上。适用于与VS2015一起发布的IIS Express 10.0版本。
Steps:
步骤:
Follow the steps below to get SignalR working in a WebForms Project. The Aim of this project is to broadcast timestamps at periodic intervals to all connected clients (browser sessions) using SignalR. Only the first timestamp is generated by the Server Side code in the code behind file. Rest comes from the SignalR HubClass which is responsible for generating the Timestamps at periodic intervals and bradcast them to ALL connected sessions.
按照下面的步骤,让SignalR在WebForms项目中工作。这个项目的目的是利用SignalR为所有连接的客户端(浏览器会话)定期发送时间戳。只有第一个时间戳是由文件后面的代码中的服务器端代码生成的。Rest来自SignalR HubClass,它负责定期生成时间戳,并将它们转换为所有连接的会话。
- In Visual Studio (2015) Create a Empty WebForms Project.( Pick Empty Template and check WebForms under "Add Core Librares and Folders). Lets say we name it as "SignalR_WebForms".
- 在Visual Studio(2015)中创建一个空的WebForms项目。(选择空模板并在“添加核心Librares和文件夹”下检查WebForms)。我们把它命名为“SignalR_WebForms”。
-
To Download, Install and add references to SignalR+jQuery+Owin libraries
要下载,请安装并添加对SignalR+jQuery+Owin库的引用
2a. Tools --> NuGet Package Manager --> Manage Nuget Packages for Solutions.
2 a。工具——> NuGet包管理器——>管理解决方案的NuGet包。
2b. Type "Microsoft.ASPNet.SignalR" in Search and pick "Microsoft.ASPNet.SignalR" (server component).
2 b。“Microsoft.ASPNet类型。在搜索和选择"Microsoft.ASPNet. SignalR"。SignalR”(服务器组件)。
2c. On the Right Panel Now check the box next to "SignalR_WebForms". This will enable the "Install" button. Pick the latest Version (2.2.0 as of today) and click on the Install Button. This will popup a "Review Changes" dialog box which informs you of all the packages (total 10) that will be installed. Click Ok. Then Click on"Accept" to Accept the License Terms. This will start the download and install process (very quick). Once done open the Packages.config file (which is located under the root of the proj folder) and it should look like this :
2 c。在右边的面板上,勾选“SignalR_WebForms”旁边的复选框。这将启用“安装”按钮。选择最新的版本(今天的2.2.0版),点击安装按钮。这将弹出一个“Review Changes”对话框,该对话框通知您将安装的所有包(总共10个)。单击Ok。然后点击“接受”以接受许可条款。这将启动下载和安装过程(非常快)。一旦完成,打开包。配置文件(位于proj文件夹的根目录下)应该是这样的:
`
”
<-- Packages.config should look like this -->
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jQuery" version="1.6.4" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
</packages>
`
”
-
Add a webform and name it as default.aspx ( RightClick on Proj Add --> Webform --> type default.aspx --> click ok.
添加一个webform并将其命名为默认。aspx(右击Proj Add—> Webform—>类型默认。aspx - - >单击ok。
-
Copy paste this code into the default.aspx file (Markup)
将此代码复制到默认值中。aspx文件(标记)
`
”
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="SignalR_WebForms._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>SignalR Using webForms</title>
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function() {
var logger = $.connection.logHub;
logger.client.logMessage = function(msg) {
$("#logUl").append("<li>" + msg + "</li>");
};
$.connection.hub.start();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Log Items</h3>
<asp:listview id="logListView" runat="server" itemplaceholderid="itemPlaceHolder" clientidmode="Static" enableviewstate="false">
<layouttemplate>
<ul id="logUl">
<li runat="server" id="itemPlaceHolder"></li>
</ul>
</layouttemplate>
<itemtemplate>
<li><span class="logItem"><%#Container.DataItem.ToString() %></span></li>
</itemtemplate>
</asp:listview>
</div>
</form>
</body>
</html>
`
”
- Copy paste the code below into the code-behind file (default.aspx.cs)
- 将下面的代码复制到代码后面的文件中(default.aspx.cs)
`
”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalR_WebForms
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var myLog = new List<string>();
myLog.Add(string.Format("{0} - Logging Started", DateTime.UtcNow));
logListView.DataSource = myLog;
logListView.DataBind();
}
}
}
`
-
Add the App_Code folder to the project. ( Right Click on Proj --> Add --> Add ASP.Net Folder --> Pick App_Code ).
向项目添加App_Code文件夹。(右击Proj——>添加——>添加ASP。Net文件夹——>选择App_Code)。
-
Add a SignalR Hub Class and name it as LogHub.cs To do this Right click on App_Code Folder --> Add --> Pick Class .. (at the bottom of the list) --> Click on Vsual C# then Web then SignalR --> Pick SignalR HubClass --> type LogHub.cs as filename. Click ok.
添加一个SignalR Hub类并将其命名为LogHub。cs点击App_Code文件夹——> Add——> Pick Class。(在列表底部)——>点击Vsual c#然后Web然后SignalR——>选择SignalR HubClass——>类型的LogHub。cs作为文件名。单击ok。
-
Open the LogHub.cs class file and delete existing code and Copy paste code below into it. Save.
打开LogHub。cs类文件并删除现有代码并将下面的代码粘贴到其中。保存。
`
”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalR_WebForms.App_Code
{
public class LogHub : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
static LogHub()
{
_Timer.Interval = 5000;
_Timer.Elapsed += TimerElapsed;
_Timer.Start();
}
static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var hub = GlobalHost.ConnectionManager.GetHubContext("LogHub");
hub.Clients.All.logMessage(string.Format("{0} - Still running", DateTime.UtcNow));
}
}
}
`
”
-
Add a Owin Startup Class file and name it as Startup1.cs. ( Right Click on App_code --> Add --> Class --> Click on Vsual C# then Web then General --> Pick Owin Startup class.) Delete existing code and Copy paste code below into this class file.Save.
添加Owin启动类文件,并将其命名为Startup1.cs。(右键点击App_code——>添加——>类——>点击Vsual c#,然后点击Web——>选择Owin启动类)删除现有代码并将下面的代码复制到这个类文件中。
`
”
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(WebApplication1.App_Code.Startup1))] namespace WebApplication1.App_Code { public class Startup1 { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } }
`
”
- Build and Run the Proj (F5). If no errors , you should see the following output on the local browser.
- 构建并运行Proj (F5)。如果没有错误,您应该在本地浏览器上看到以下输出。
`
”
Log Items
•06/04/2016 09:50:02 PM - Logging Started
•06/04/2016 09:50:06 PM - Still running
•06/04/2016 09:50:11 PM - Still running
•06/04/2016 09:50:16 PM - Still running
•06/04/2016 09:50:21 PM - Still running
.....
.....
.....
.....
Keeps Going **without** having to refresh the Browser.
`
”
-
From a remote PC access the same site and you should get the exact same timestamps. This verifies that the site works as expected.
从远程PC访问相同的站点,您应该获得相同的时间戳。这将验证站点是否按照预期工作。
-
To further verify Right Click on the Browser and click View Source. On I.E this opens up a Notepad window with page html. Find "logUL" and you should see just the markup that shows the initial timestamp. There is no markup that shows the remaining updates as those are injected by the SignalR hub. This is similar to AJAX.
要进一步验证,请在浏览器上右键单击并单击View Source。在我。这将打开一个带有页面html的记事本窗口。找到“logUL”,您应该只看到显示初始时间戳的标记。没有标记显示剩余的更新,因为这些更新是由SignalR hub注入的。这类似于AJAX。
`
”
<div>
<h3>Log Items</h3>
<ul id="logUl">
<li><span class="logItem">06/04/2016 09:50:02 PM - Logging Started</span></li>
</ul>
</div>
`
”
Thats it !. HTH !!
它!。HTH ! !
#3
-1
type this in Package Manager Console : install-package Microsoft.AspNet.SignalR -Version 1.1.3
在包管理器控制台输入:安装包Microsoft.AspNet。SignalR - version 1.1.3
#1
12
You can use SignalR with webforms. See below for an example from the tutorial here
你可以在webforms中使用SignalR。下面是本教程中的一个示例
-
Create a new ASP.NET WebForms project targeting .NET Framework 4.5 or later
创建一个新的ASP。NET WebForms项目针对。NET Framework 4.5或更高版本
-
Change the home page to contain the following
将主页更改为包含以下内容
<asp:content runat="server" id="BodyContent" contentplaceholderid="MainContent"> <h3>Log Items</h3> <asp:listview id="logListView" runat="server" itemplaceholderid="itemPlaceHolder" clientidmode="Static" enableviewstate="false"> <layouttemplate> <ul id="logUl"> <li runat="server" id="itemPlaceHolder"></li> </ul> </layouttemplate> <itemtemplate> <li><span class="logItem"><%#Container.DataItem.ToString() %></span></li> </itemtemplate> </asp:listview> </asp:content>
-
Edit the default.aspx.cs codebehind file to include the following event
编辑default . aspx。cs码后文件包含以下事件
protected void Page_Load(object sender, EventArgs e) { var myLog = new List<string>(); myLog.Add(string.Format("{0} - Logging Started", DateTime.UtcNow)); logListView.DataSource = myLog; logListView.DataBind(); }
-
Add SignalR packages via NuGet. (Trying searching for "Microsoft ASP.Net SignalR JS" and "Microsoft ASP.Net SignalR JS")
通过NuGet添加信号包。(尝试搜索“微软ASP”。Net SignalR JS和Microsoft ASP。净SignalR JS”)
-
Create a Hub class
创建一个中心类
public class LogHub : Hub { public static readonly System.Timers.Timer _Timer = new System.Timers.Timer(); static LogHub() { _Timer.Interval = 2000; _Timer.Elapsed += TimerElapsed; _Timer.Start(); } static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { var hub = GlobalHost.ConnectionManager.GetHubContext("LogHub"); hub.Clients.All.logMessage(string.Format("{0} - Still running", DateTime.UtcNow)); } }
-
Setup the following script block at the bottom of your page (your jquery and jquery.signalr version may vary)
在页面底部设置下面的脚本块(jquery和jquery)。signalr版本可能会有所不同)
<script src="Scripts/jquery.1.7.1.min.js"></script> <script src="Scripts/jquery.signalR-1.0.0-rc1.min.js"></script> <script src="http://www.codeproject.com/signalr/hubs" type="text/javascript"></script> <script type="text/javascript"> $(function() { var logger = $.connection.logHub; logger.client.logMessage = function(msg) { $("#logUl").append("<li>" + msg + "</li>"); }; $.connection.hub.start(); }); </script>
-
Add the following to the Application_Start event handler in global.asax.cs
在global.asax.cs中添加以下的Application_Start事件处理程序。
void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHubs(); }
#2
13
The answer provided by @Stephen is now outdated as it does not apply to the latest Version of SignalR (v2.2.0). Also there are few other things that are not mentioned which IMHO might help future readers to quickly get started with SignalR using the Good old Webforms framework. The solution might appear tedious but it is not. I hope it helps people who visit this page looking to get help on SignalR for Webforms.
@Stephen提供的答案现在已经过时了,因为它不适用于最新版本的SignalR (v2.2.0)。另外,IMHO可能会帮助未来的读者使用优秀的旧Webforms框架快速开始使用SignalR。这个解决方案可能看起来单调乏味,但实际上并非如此。我希望它能帮助那些访问这个页面的人在web表单的SignalR上获得帮助。
Pre-Reqs: (Versions used by me are in brackets) . I have NOT tested this solution on other versions
预请求:(我使用的版本在括号中)。我还没有在其他版本上测试这个解决方案
- MS Visual Studio 2015/2013. (2015) On Win-7 x64
- MS Visual Studio 2015/2013。在win 7 x64(2015)
- .Net FrameWork version 4.5 or Higher (4.5.2)
- .Net框架4.5或更高版本(4.5.2)
- SignalR version 2.2.0 from NuGet (Release Date 01/13/2015)
- 来自NuGet的SignalR 2.2.0版本(发布日期01/13/2015)
- jQuery ver 1.6.4
- jQuery版本1.6.4,
- Owin v1.0.0 and few others like Json, owin.Security etc... (see packages.config)
- Owin v1.0.0和很少其他类似Json的版本。安全等等……(见packages.config)
- IIS v7.0 and above. Works on IIS Express version 10.0 that ships with VS2015.
- IIS v7.0以上。适用于与VS2015一起发布的IIS Express 10.0版本。
Steps:
步骤:
Follow the steps below to get SignalR working in a WebForms Project. The Aim of this project is to broadcast timestamps at periodic intervals to all connected clients (browser sessions) using SignalR. Only the first timestamp is generated by the Server Side code in the code behind file. Rest comes from the SignalR HubClass which is responsible for generating the Timestamps at periodic intervals and bradcast them to ALL connected sessions.
按照下面的步骤,让SignalR在WebForms项目中工作。这个项目的目的是利用SignalR为所有连接的客户端(浏览器会话)定期发送时间戳。只有第一个时间戳是由文件后面的代码中的服务器端代码生成的。Rest来自SignalR HubClass,它负责定期生成时间戳,并将它们转换为所有连接的会话。
- In Visual Studio (2015) Create a Empty WebForms Project.( Pick Empty Template and check WebForms under "Add Core Librares and Folders). Lets say we name it as "SignalR_WebForms".
- 在Visual Studio(2015)中创建一个空的WebForms项目。(选择空模板并在“添加核心Librares和文件夹”下检查WebForms)。我们把它命名为“SignalR_WebForms”。
-
To Download, Install and add references to SignalR+jQuery+Owin libraries
要下载,请安装并添加对SignalR+jQuery+Owin库的引用
2a. Tools --> NuGet Package Manager --> Manage Nuget Packages for Solutions.
2 a。工具——> NuGet包管理器——>管理解决方案的NuGet包。
2b. Type "Microsoft.ASPNet.SignalR" in Search and pick "Microsoft.ASPNet.SignalR" (server component).
2 b。“Microsoft.ASPNet类型。在搜索和选择"Microsoft.ASPNet. SignalR"。SignalR”(服务器组件)。
2c. On the Right Panel Now check the box next to "SignalR_WebForms". This will enable the "Install" button. Pick the latest Version (2.2.0 as of today) and click on the Install Button. This will popup a "Review Changes" dialog box which informs you of all the packages (total 10) that will be installed. Click Ok. Then Click on"Accept" to Accept the License Terms. This will start the download and install process (very quick). Once done open the Packages.config file (which is located under the root of the proj folder) and it should look like this :
2 c。在右边的面板上,勾选“SignalR_WebForms”旁边的复选框。这将启用“安装”按钮。选择最新的版本(今天的2.2.0版),点击安装按钮。这将弹出一个“Review Changes”对话框,该对话框通知您将安装的所有包(总共10个)。单击Ok。然后点击“接受”以接受许可条款。这将启动下载和安装过程(非常快)。一旦完成,打开包。配置文件(位于proj文件夹的根目录下)应该是这样的:
`
”
<-- Packages.config should look like this -->
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jQuery" version="1.6.4" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.0" targetFramework="net452" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
</packages>
`
”
-
Add a webform and name it as default.aspx ( RightClick on Proj Add --> Webform --> type default.aspx --> click ok.
添加一个webform并将其命名为默认。aspx(右击Proj Add—> Webform—>类型默认。aspx - - >单击ok。
-
Copy paste this code into the default.aspx file (Markup)
将此代码复制到默认值中。aspx文件(标记)
`
”
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="SignalR_WebForms._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>SignalR Using webForms</title>
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function() {
var logger = $.connection.logHub;
logger.client.logMessage = function(msg) {
$("#logUl").append("<li>" + msg + "</li>");
};
$.connection.hub.start();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Log Items</h3>
<asp:listview id="logListView" runat="server" itemplaceholderid="itemPlaceHolder" clientidmode="Static" enableviewstate="false">
<layouttemplate>
<ul id="logUl">
<li runat="server" id="itemPlaceHolder"></li>
</ul>
</layouttemplate>
<itemtemplate>
<li><span class="logItem"><%#Container.DataItem.ToString() %></span></li>
</itemtemplate>
</asp:listview>
</div>
</form>
</body>
</html>
`
”
- Copy paste the code below into the code-behind file (default.aspx.cs)
- 将下面的代码复制到代码后面的文件中(default.aspx.cs)
`
”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalR_WebForms
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var myLog = new List<string>();
myLog.Add(string.Format("{0} - Logging Started", DateTime.UtcNow));
logListView.DataSource = myLog;
logListView.DataBind();
}
}
}
`
-
Add the App_Code folder to the project. ( Right Click on Proj --> Add --> Add ASP.Net Folder --> Pick App_Code ).
向项目添加App_Code文件夹。(右击Proj——>添加——>添加ASP。Net文件夹——>选择App_Code)。
-
Add a SignalR Hub Class and name it as LogHub.cs To do this Right click on App_Code Folder --> Add --> Pick Class .. (at the bottom of the list) --> Click on Vsual C# then Web then SignalR --> Pick SignalR HubClass --> type LogHub.cs as filename. Click ok.
添加一个SignalR Hub类并将其命名为LogHub。cs点击App_Code文件夹——> Add——> Pick Class。(在列表底部)——>点击Vsual c#然后Web然后SignalR——>选择SignalR HubClass——>类型的LogHub。cs作为文件名。单击ok。
-
Open the LogHub.cs class file and delete existing code and Copy paste code below into it. Save.
打开LogHub。cs类文件并删除现有代码并将下面的代码粘贴到其中。保存。
`
”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalR_WebForms.App_Code
{
public class LogHub : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
static LogHub()
{
_Timer.Interval = 5000;
_Timer.Elapsed += TimerElapsed;
_Timer.Start();
}
static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var hub = GlobalHost.ConnectionManager.GetHubContext("LogHub");
hub.Clients.All.logMessage(string.Format("{0} - Still running", DateTime.UtcNow));
}
}
}
`
”
-
Add a Owin Startup Class file and name it as Startup1.cs. ( Right Click on App_code --> Add --> Class --> Click on Vsual C# then Web then General --> Pick Owin Startup class.) Delete existing code and Copy paste code below into this class file.Save.
添加Owin启动类文件,并将其命名为Startup1.cs。(右键点击App_code——>添加——>类——>点击Vsual c#,然后点击Web——>选择Owin启动类)删除现有代码并将下面的代码复制到这个类文件中。
`
”
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(WebApplication1.App_Code.Startup1))] namespace WebApplication1.App_Code { public class Startup1 { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } }
`
”
- Build and Run the Proj (F5). If no errors , you should see the following output on the local browser.
- 构建并运行Proj (F5)。如果没有错误,您应该在本地浏览器上看到以下输出。
`
”
Log Items
•06/04/2016 09:50:02 PM - Logging Started
•06/04/2016 09:50:06 PM - Still running
•06/04/2016 09:50:11 PM - Still running
•06/04/2016 09:50:16 PM - Still running
•06/04/2016 09:50:21 PM - Still running
.....
.....
.....
.....
Keeps Going **without** having to refresh the Browser.
`
”
-
From a remote PC access the same site and you should get the exact same timestamps. This verifies that the site works as expected.
从远程PC访问相同的站点,您应该获得相同的时间戳。这将验证站点是否按照预期工作。
-
To further verify Right Click on the Browser and click View Source. On I.E this opens up a Notepad window with page html. Find "logUL" and you should see just the markup that shows the initial timestamp. There is no markup that shows the remaining updates as those are injected by the SignalR hub. This is similar to AJAX.
要进一步验证,请在浏览器上右键单击并单击View Source。在我。这将打开一个带有页面html的记事本窗口。找到“logUL”,您应该只看到显示初始时间戳的标记。没有标记显示剩余的更新,因为这些更新是由SignalR hub注入的。这类似于AJAX。
`
”
<div>
<h3>Log Items</h3>
<ul id="logUl">
<li><span class="logItem">06/04/2016 09:50:02 PM - Logging Started</span></li>
</ul>
</div>
`
”
Thats it !. HTH !!
它!。HTH ! !
#3
-1
type this in Package Manager Console : install-package Microsoft.AspNet.SignalR -Version 1.1.3
在包管理器控制台输入:安装包Microsoft.AspNet。SignalR - version 1.1.3