上次用客户端进行数据刷新的方式,和官方的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<===

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

不用找了,比较全的signalR例子已经为你准备好了(2)---JqGrid 服务端刷新方式-注释详细-DEMO源码下载的更多相关文章

  1. 不用找了,比较全的signalR例子已经为你准备好了.

    这几天想着将一个winform的工具上线到web上,因为对时时性的要求比较高,找朋友咨询了一下推荐了SignlarR 框架,比较强大.昨天才看到,今天研究了一下将里面的例子都拿出来共享. 最全的参考: ...

  2. 3、netty第二个例子,使用netty建立客户端,与服务端通讯

    第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...

  3. FLEX外包团队:Flex例子DEMO源码

    代码如下: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx=& ...

  4. webRTC源码下载 Windows Mac(iOS) Linux(Android)全

    webRTC源码下载地址:https://pan.baidu.com/s/18CjClvAuz3B9oF33ngbJIw  提取码:wl1e  Windows版:visual studio 2017工 ...

  5. signalR例子

    不用找了,比较全的signalR例子已经为你准备好了.   这几天想着将一个winform的工具上线到web上,因为对时时性的要求比较高,找朋友咨询了一下推荐了SignlarR 框架,比较强大.昨天才 ...

  6. Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程

    2022年1月,科学技术部高新技术司副司长梅建平在"第六届中国新金融高峰论坛"上表示,当前数据量已经大大超过了处理能力的上限,若信息技术仍然是渐进式发展,则数据处理能力的提升将远远 ...

  7. 一个简单的SignalR例子

    本文介绍如何使用SignalR的Hub制作一个简单的点赞页面.不同浏览器(或者不同窗口)打开同一个页面,在任何一个页面点赞,所有页面同时更新点赞数. 1.使用Visual Studio Communi ...

  8. Android图片加载框架最全解析(二),从源码的角度理解Glide的执行流程

    在本系列的上一篇文章中,我们学习了Glide的基本用法,体验了这个图片加载框架的强大功能,以及它非常简便的API.还没有看过上一篇文章的朋友,建议先去阅读 Android图片加载框架最全解析(一),G ...

  9. C#外挂QQ找茬辅助源码,早期开发

    这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成.女神使用后找茬等级瞬间从眼明手快升级为三只眼...每次看到这个就会想起那段屌丝与女神的回忆.今天特地把代码更新 ...

随机推荐

  1. poj 2356 抽屉原理

    基本原理: n+1个鸽子放到n个笼子里,至少有一个笼子里有两只及其以上的鸽子.若有n个笼子,kn+1个鸽子,至少有一个笼子里面有k+1个鸽子: 题意:给定N个数,挑出一些数,他们和和是n的整数倍: 分 ...

  2. Gym - 101334E 多叉树遍历

    题意:给定一个字符串,求有多少种树与之对应,对应方式是,每次遍历左节点,没有了,就回溯: 分析:d[i,j] = sum(d[i+1,k-1],d[k,j]) (str[i]==str[k]); 坑点 ...

  3. axis调用cxf的webservice注意事项

    需要注意的是: 1.wsdl显示部分内容 <?xml version="1.0" ?> - <wsdl:definitions name="Archiv ...

  4. intellig idea中jsp或html数据没有自动保存和更换字体

    主题一:保存数据jsp intellig idea是自动保存数据的,看到没有保存 解决方案: 成功解决 主题二:更换字体: 或者快捷键Ctel+Alt+s 成功解决

  5. 使用TextView/EditText应该注意的地方,监听EditText,addTextChangedListener

    http://blog.csdn.net/huichengongzi/article/details/7818676 监听 EditText 控件: addTextChangedListener(ne ...

  6. Redis的安装+哨兵模式+集群

    Redis安装(基于2.8版本) 哨兵(Sentinel集群)模式: 集群(基于3.0+)

  7. springMVC中接收请求参数&&数据转发

    ### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...

  8. MySQL的Root用户密码

    缘由:最近北京市二环内大兴土木,各种挖沟埋线.忽而一纸通令周末断电,故多年不断电的服务器,便令人有了关机后是否还能正常启动的隐忧.其中一台较年迈的服务器中搭载有MySQL数据库.数据库内容本属于外包项 ...

  9. php访问url(get和post请求)

    get请求 /* * php访问url路径,get请求 */ function curl_file_get_contents($durl){ // header传送格式 $headers = arra ...

  10. Mysql错误积累001-load data导入文件数据出现1290错误

    错误出现情景 在cmd中使用mysql命令,学生信息表添加数据.使用load data方式简单批量导入数据. 准备好文本数据: xueshengxinxi.txt 文件  数据之间以tab键进行分割 ...