默认情况下,信号器集线器在每个页面加载时启动,并在同一页面刷新或c#中的新选项卡上的同一浏览器上创建新的连接ID。
我很抱歉它不是信号器问题,但在我的情况下,但我需要如何更新信号灯。
signalr在每个页面刷新时创建新的连接ID并保留以前的连接ID。当任何更新接收时,它会向每个新连接发送更新
但我希望每个标签一个浏览器有一个连接,它不应该在刷新或新选项卡上为同一个浏览器创建新的连接ID。
我使用的代码:
JobHub.cs
上课
public class JobHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<JobHub>();
context.Clients.All.displayStatus();
}
}
View.cshtml
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
var job = $.connection.jobHub;
job.client.displayStatus = function () {
alert("change notice");
getData();
};
$.connection.hub.start().done(function () {
//connection started
alert("connection started");
getData();
});
function getData() {
var $tbl = $('#tblJobInfo');
$.ajax({
url: 'http://localhost:51737/api/values',
type: 'GET',
datatype: 'json',
success: function (data) {
if (data.length > 0) {
$tbl.empty();
$tbl.append(' <tr><th>ID</th><th>Name</th><th>Last Executed Date</th><th>Status</th></tr>');
var rows = [];
for (var i = 0; i < data.length; i++) {
rows.push(' <tr><td>' + data[i].JobID + '</td><td>' + data[i].Name + '</td><td>' + data[i].LastExecutionDate.toString().substr(0,10) + '</td><td>' + data[i].Status + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
Div
<div>
<table id="tblJobInfo" style="text-align:center;margin-left:10px"> </table></div>
JobInfo.cs
和JobInfoRepository.cs
public class JobInfo
{
public int JobID { get; set; }
public string Name { get; set; }
public DateTime LastExecutionDate { get; set; }
public string Status { get; set; }
}
public class JobInfoRepository
{
public IEnumerable<JobInfo> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [JobID],[Name],[LastExecutionDate],[Status]
FROM [dbo].[JobInfo]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == System.Data.ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new JobInfo()
{
JobID = x.GetInt32(0),
Name = x.GetString(1),
LastExecutionDate = x.GetDateTime(2),
Status = x.GetString(3)
}).ToList();
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
//calling hub notification
JobHub.Show();
}
}
API ValuesController.cs
public class ValuesController : ApiController
{
JobInfoRepository objRepo = new JobInfoRepository();
// GET api/values
public IEnumerable<JobInfo> Get()
{
return objRepo.GetData();
}
}
每当我通过DB获取更新时刷新页面,它会多次调用sql依赖项更改事件。 请指导或帮助我,我该如何处理这种情况。我感谢您宝贵的时间和精力。