上次用客户端进行数据刷新的方式,和官方的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. Linux云主机 监控方案浅析

    1.为何需要监控 监控是运维工程师的眼睛,它可帮助运维工程师第一时间发现系统的问题. 对于服务器的整个生命周期,都要和监控打交道: 当有服务器上架,都需要加入比如CPU负载.内存.网络.磁盘等基础监控 ...

  2. Latex 编辑器安装

    MiKTex + TexStudio 1. 下载安装MiKTex,从http://www.miktex.org/下载basic-miktex-2.9.5872-x64.exe,一路默认安装... 或者 ...

  3. 【luogu P1640 [SCOI2010]连续攻击游戏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1640 数据有点水吧,从属性值连向对应武器编号. 枚举属性值匹配,遇到第一个无法匹配的直接跳出就好惹~. #i ...

  4. 【题解】洛谷P3166 [CQOI2014] 数三角形(组合+枚举)

    洛谷P3166:https://www.luogu.org/problemnew/show/P3166 思路 用组合数求出所有的3个点组合(包含不合法的) 把横竖的3个点共线的去掉 把斜的3个点共线的 ...

  5. Struts2 第二讲 -- Struts2的入门

    搭建struts2环境时,我们一般需要做以下几个步骤的工作: 第一步:创建javaweb工程(这个很废话有木有) 第二步:找到开发Struts2应用需要使用到的jar文件.(这个很白痴有没有) 到ht ...

  6. 优雅的QSignleton (三) 通过属性器实现Singleton

    接下来介绍,不通过继承的方式实现单例模式.大家都出去嗨了,而我却在家码代码... 代码如下: MonoSingletonProperty.cs namespace QFramework.Example ...

  7. spring入门(七) spring mvc+mybatis+generator

    1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...

  8. Xcode 9.0 报错, Safe Area Layout Guide Before IOS 9.0

    Xcode 9.0 新建工程报错 xcode Safe Area Layout Guide Before IOS 9.0 如下图,在Builds for 选择iOS9.0 and Later,不勾选U ...

  9. [HAOI2010]软件安装(树形背包,tarjan缩点)

    题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...

  10. ABAP术语-Error Message

    Error Message 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/30/1058283.html Information from ...