如何使用signalr在视图页中显示更新的记录? [英] How to dispaly updated records in view page using signalr?
本文介绍了如何使用signalr在视图页中显示更新的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用SignalR需要显示没有刷新页面的更新记录我尝试使用signalR不能在更改事件上工作取决于此请帮助</ pre>
我尝试过:
使用SignalRDbUpdates.Hubs;
使用System;
使用System.Collections.Generic;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
命名空间SignalRDbUpdates.Models
{
公共静态类MessagesRepository
{
public static bool startUp = true;
public static bool subscribe = true;
public static SqlDependency dependency = null;
// readonly string _connString = ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString;
public static IEnumerable< Messages> GetAllMessages()
{
var messages = new List< Messages>();
using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString))
{
connection.Open();
使用(var command = new SqlCommand(@SELECT [JobId],[JobDesrciption],[JobName],getdate()as Date FROM [dbo]。[ScheduleJobs],connection))
{
command.Notification = null;
// var dependency = new SqlDependency(command);
////SqlDependency.Stop(_connString);
////SqlDependency.Start(_connString);
//dependency.OnChange + = new OnChangeEventHandler(dependency_OnChange);
//SqlDependency.Stop(_connString);
//SqlDependency.Start(_connString);
if(connection.State == ConnectionState.Closed)
connection.Open();
if(subscribe)
{
//创建SqlDependency对象
//并将其绑定到命令对象。
dependency = new SqlDependency(command);
dependency.OnChange - = new OnChangeEventHandler(dependency_OnChange);
dependency.OnChange + = new OnChangeEventHandler(dependency_OnChange);
subscribe = false;
}
var reader = command.ExecuteReader();
while(reader.Read())
{
messages.Add(item:new Messages {MessageID =(int)reader [JobId],Message =(string读者[JobDesrciption],EmptyMessage = reader [JobName]!= DBNull.Value?(string)reader [JobName]:,MessageDate = Convert.ToDateTime(reader [Date])}) ;
}
}
}
返回消息;
}
private static void dependency_OnChange(object sender,SqlNotificationEventArgs e)
{
// if(e.Type.ToString ()==subscribe)
// {
// MessagesHub.SendMessages();
//}
if(e.Type == SqlNotificationType.Change&&
(e.Info == SqlNotificationInfo.Insert ||
e.Info == SqlNotificationInfo .Delete ||
e.Info == SqlNotificationInfo.Update)
)
{
//删除处理程序,因为它只适用于单个通知。
SqlDependency dependency =(SqlDependency)sender;
dependency.OnChange - = dependency_OnChange;
MessagesHub.SendMessages();
subscribe = true;
}
}
}
}
@ {
ViewBag.Title =主页;
}
< div class =row>
< div class =col-md-12>
< div id =messagesTable>< / div>
< / div>
< / div>
@section脚本{
< script src =/ Scripts / jquery.signalR-2.1.1.js>< / script>
<! - 引用自动生成的SignalR集线器脚本。 - >
< script src =/ signalr / hubs>< / script>
< script type =text / javascript>
$(function(){
//声明代理以引用集线器。
var notifications = $ .connection.messagesHub;
//调试器;
//创建一个集线器可以调用广播消息的函数。
notifications.client.updateMessages = function(){
getAllMessages()
};
//开始连接。
$ .connection.hub.start()。done(function(){
alert(connection started)
getAllMessages();
})。失败(函数(e){
alert(e);
});
});
函数getAllMessages()
{
var tbl = $('#messagesTable');
$ .ajax({
url:'/ home / GetMessages',
cache:false,
contentType:'application / html; charset:utf-8',
类型:'GET',
dataType:'html'
})。success(函数(结果){
tbl.empty()。append(result);
})。error(function(){
});
}
< / script>
}
使用SignalRDbUpdates.Models;
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间SignalRDbUpdates.Controllers
{
public class HomeController:Controller
{
public ActionResult Index()
{
return视图();
}
public ActionResult关于()
{
ViewBag.Message =你的应用程序描述页面。;
return View();
}
public ActionResult联系人()
{
ViewBag.Message =您的联系页面。;
return View();
}
[OutputCache(Duration = 0)]
public ActionResult GetMessages()
{
// MessagesRepository _messageRepository = new MessagesRepository();
返回PartialView(_ MessagesList,MessagesRepository.GetAllMessages());
}
}
}
使用SignalRDbUpdates.Models;
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间SignalRDbUpdates.Controllers
{
public class HomeController:Controller
{
public ActionResult Index()
{
return视图();
}
public ActionResult关于()
{
ViewBag.Message =你的应用程序描述页面。;
return View();
}
public ActionResult联系人()
{
ViewBag.Message =您的联系页面。;
return View();
}
[OutputCache(Duration = 0)]
public ActionResult GetMessages()
{
// MessagesRepository _messageRepository = new MessagesRepository();
返回PartialView(_ MessagesList,MessagesRepository.GetAllMessages());
}
}
}
解决方案
(function(){
//声明代理以引用集线器。
var notifications =
.connection.messagesHub;
//调试器;
//创建集线器可以调用广播消息的函数。
notifications.client.updateMessages = function(){
getAllMessages()
};
//启动连接。
.connection.hub.start()。done(function(){
alert(connection started)
getAllMessages();
})。失败(函数(e){
alert(e);
});
});
函数getAllMessages()
{
var tbl =
Using SignalR need display updated records without refresh page i tried using signalR not working depenedncy on change event please help on this
What I have tried:
using SignalRDbUpdates.Hubs; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; namespace SignalRDbUpdates.Models { public static class MessagesRepository { public static bool startUp = true; public static bool subscribe = true; public static SqlDependency dependency = null; //readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; public static IEnumerable<Messages> GetAllMessages() { var messages = new List<Messages>(); using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { connection.Open(); using (var command = new SqlCommand(@"SELECT [JobId], [JobDesrciption], [JobName], getdate() as Date FROM [dbo].[ScheduleJobs]", connection)) { command.Notification = null; //var dependency = new SqlDependency(command); ////SqlDependency.Stop(_connString); ////SqlDependency.Start(_connString); //dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); //SqlDependency.Stop(_connString); //SqlDependency.Start(_connString); if (connection.State == ConnectionState.Closed) connection.Open(); if (subscribe) { // Create and bind the SqlDependency object // to the command object. dependency = new SqlDependency(command); dependency.OnChange -= new OnChangeEventHandler(dependency_OnChange); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); subscribe = false; } var reader = command.ExecuteReader(); while (reader.Read()) { messages.Add(item: new Messages { MessageID = (int)reader["JobId"], Message = (string)reader["JobDesrciption"], EmptyMessage = reader["JobName"] != DBNull.Value ? (string) reader["JobName"] : "", MessageDate = Convert.ToDateTime(reader["Date"]) }); } } } return messages; } private static void dependency_OnChange(object sender, SqlNotificationEventArgs e) { //if (e.Type.ToString() == "subscribe") //{ // MessagesHub.SendMessages(); //} if (e.Type == SqlNotificationType.Change && (e.Info == SqlNotificationInfo.Insert || e.Info == SqlNotificationInfo.Delete || e.Info == SqlNotificationInfo.Update) ) { // Remove the handler, since it is only good for a single notification. SqlDependency dependency = (SqlDependency)sender; dependency.OnChange -= dependency_OnChange; MessagesHub.SendMessages(); subscribe = true; } } } }
@{ ViewBag.Title = "Home Page"; } <div class="row"> <div class="col-md-12"> <div id="messagesTable"></div> </div> </div> @section Scripts{ <script src="/Scripts/jquery.signalR-2.1.1.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="/signalr/hubs"></script> <script type="text/javascript"> $(function () { // Declare a proxy to reference the hub. var notifications = $.connection.messagesHub; //debugger; // Create a function that the hub can call to broadcast messages. notifications.client.updateMessages = function () { getAllMessages() }; // Start the connection. $.connection.hub.start().done(function () { alert("connection started") getAllMessages(); }).fail(function (e) { alert(e); }); }); function getAllMessages() { var tbl = $('#messagesTable'); $.ajax({ url: '/home/GetMessages', cache: false, contentType: 'application/html ; charset:utf-8', type: 'GET', dataType: 'html' }).success(function (result) { tbl.empty().append(result); }).error(function () { }); } </script> }
using SignalRDbUpdates.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace SignalRDbUpdates.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } [OutputCache(Duration = 0)] public ActionResult GetMessages() { // MessagesRepository _messageRepository = new MessagesRepository(); return PartialView("_MessagesList", MessagesRepository.GetAllMessages()); } } }
using SignalRDbUpdates.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace SignalRDbUpdates.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } [OutputCache(Duration = 0)] public ActionResult GetMessages() { // MessagesRepository _messageRepository = new MessagesRepository(); return PartialView("_MessagesList", MessagesRepository.GetAllMessages()); } } }解决方案(function () { // Declare a proxy to reference the hub. var notifications =
.connection.messagesHub; //debugger; // Create a function that the hub can call to broadcast messages. notifications.client.updateMessages = function () { getAllMessages() }; // Start the connection.
.connection.hub.start().done(function () { alert("connection started") getAllMessages(); }).fail(function (e) { alert(e); }); }); function getAllMessages() { var tbl =
这篇关于如何使用signalr在视图页中显示更新的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文