上次用客户端进行数据刷新的方式,和官方的Demo实现存在差异性,今天花了一点时间好好研究了一下后台时时刷新的方式.将写的代码重新update了一次,在这之前找过好多的资料,发现都没有找到好的例子,自己查了一下官方的DEMO然后本地实现了一次,结合Jqgrid的前端库,发现还是非常便捷的.不多说了,直接上代码了.

前端代码:

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="Style/jquery-ui.css" rel="stylesheet" />
<link href="Style/ui.jqgrid.css" rel="stylesheet" />
<link href="Style/ui.jqgrid-bootstarp.css" rel="stylesheet" /> <!--Reference the jQuery library. this library should be first one -->
<script src="Scripts/jquery-1.10.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.2.js"></script>
<!--Reference the jqgrid core library. -->
<script src="Scripts/jquery.jqGrid.min.js"></script>
<!--Reference the jqgrid language library. -->
<script src="Scripts/grid.locale-en.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src='signalr/hubs'></script> <script type="text/javascript"> var mydata = { State: "none", Price: 1.99, Brand: "none" }; var ticket; $(function () {
InitJqGrid();
ticket = $.connection.pricehub;
InitTicket(ticket);
$.connection.hub.start().done(function () { $("#btnstart").click(function () {
ticket.server.startTickets();
}); $("#btnstop").click(function () {
ticket.server.stopTickets();
});
}); }) //
function InitJqGrid() {
$("#tbprice").jqGrid({
datatype: "local",
data: mydata,
height: 600,
width: 500,
multiselect: false,
autowidth: true,
rownumbers: true,
rowNum: 50, //if you don't set this ,the page size will just show about 20 row counts.
colNames: ['State', 'Price', 'Brand'],
colModel: [
{ label: 'State', name: 'State', width: 60 },
{ label: 'Price', name: 'Price', width: 80 },
{ label: 'Brand', name: 'Brand', width: 80 }
],
viewrecords: true, // show the current page, data rang and total records on the toolbar
caption: "Current Price Tag",
pager: "#jqGridPager"
});
} function refreshGrid($grid, results) {
$grid.jqGrid('clearGridData')
.jqGrid('setGridParam', { data: results })
.trigger('reloadGrid');
} function InitTicket(ticket) {
//init the client function
ticket.client.updateprice = function (tickets) {
refreshGrid($("#tbprice"), tickets);
}
}
</script> <title>Price Price </title> </head>
<body> <div>
<input type="button" id="btnstart" value="Start" /> <input type="button" id="btnstop" value="Stop" />
</div>
<div>
<table id="tbprice"></table>
<div id="jqGridPager"></div>
</div>
</body> </html>

后端代码:

 using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using SignalRChat.Hubs.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading; namespace SignalRChat.Hubs
{
[HubName("pricehub")]
public class PriceHub : Hub
{
private readonly TicketPrice _ticketprice = new TicketPrice(); private readonly object _ticketrefreshlock = new object(); private readonly object _ticketupdatelock = new object(); //the time val should be static or in the static class
private static Timer _timer; //the Interval of call function
private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(); private static string state = "close"; [HubMethodName("startTickets")]
public void StartTickets()
{
lock (_ticketrefreshlock)
{
//the judge if it is necessary to init another thread to fresh the value
if (state == "close")
{
_timer = new Timer(RefreshTicket, null, _updateInterval, _updateInterval);
state = "open";
}
}
} [HubMethodName("stopTickets")]
public void StopTickets()
{
lock (_ticketrefreshlock)
{
if (state == "open")
{
if (_timer != null)
{
_timer.Dispose();
state = "close";
}
}
}
} private void RefreshTicket(object state)
{
lock (_ticketupdatelock)
{
//return the tickets to client
BroadcastPriceTicketBoard(_ticketprice.GetAllTickets());
}
} //this is the reference for client broswer to update the price ,and pass the value to client .
private void BroadcastPriceTicketBoard(List<TicketPrice> tickets)
{
//call the client javascript function to refresh data to jqgrid table(the caller proproty mean the data only to pass to caller ,not all clients)
Clients.Caller.updateprice(tickets);
} }
}

效果图:

参考:

===>JqGridDemo<===

===>SignalR-StockTicker-Demo<===

项目文件:

===>官方StockTicker.zip<===

===>图中演示项目<===

最新文章

  1. Android之线程池深度剖析
  2. css3 input边框蓝光特效
  3. ADB server didn&#39;t ACK
  4. php session的理解与使用
  5. Eclipse 4.2 (Juno) &#39;Cannot create a server using the selected type&#39; in Tomcat 7 - Stack Overflow.html
  6. java BigInteger使用
  7. Spring Boot 集成MyBatis
  8. Caesar
  9. 游戏开场镜头拉近(Unity3D开发之四)
  10. 【程序员联盟】官网上线啦!coderunity.com
  11. Java 多态透析 详细理解
  12. Python内置函数(62)——exec
  13. 关于ajax的content-download时间过慢问题的解决方案与思考
  14. NLP系列(4)_朴素贝叶斯实战与进阶
  15. WORDPRESS博客完美更换网站空间服务器的方法
  16. SSE图像算法优化系列四:图像转置的SSE优化(支持8位、24位、32位),提速4-6倍
  17. vue中$emit 和$on 和$set的用法
  18. Python2.7-stat
  19. html 图片在一个div中放大缩小效果
  20. Linux sar

热门文章

  1. 从命令行运行Jmeter及jmeter参数说明、Html报告生成
  2. 【洛谷T37388】P哥破解密码
  3. Android学习笔记_34_自定义窗口标题
  4. CSU 1023 修路(二分+模拟)
  5. java遍历文件夹两种方式
  6. c的三个内存分配函数(malloc,realloc,calloc)
  7. node学习----Promise 初见
  8. HTML5笔记——第一节
  9. UICollectionViewCell「居左显示」
  10. Navicat for Mysql中错误提示索引过长1071-max key length is 767 byte