您的位置:首页 > 其它

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

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

官方的参考:http://www.asp.net/signalr/overview/getting-started

安装SignalR: NuGet命令:

PM> Install-Package Microsoft.AspNet.SignalR

<------------1:与他人聊天:[b]------------>[/b]

后台代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace MyReresh.ViewData
{
[HubName("viewDataHub")]
public class ViewDataHub : Hub
{
[HubMethodName("refresh")]
public List<Stock> Refresh()
{
return Stock.GetAll();
}

[HubMethodName("RefreshClients")]
public void RefreshClients()
{
Clients.All.myrefresh(Stock.GetAll());
}

}

public class Stock
{
private string opendoor;

public string Opendoor
{
get { return opendoor; }
set { opendoor = value; }
}

private double price;

public double Price
{
get { return price; }
set { price = value; }
}

private DateTime opentiem = System.DateTime.Now;

public DateTime Opentiem
{
get { return opentiem; }
set { opentiem = value; }
}

public static List<Stock> GetAll()
{
Random rand = new Random();
List<Stock> list = new List<Stock>()
{
new Stock{Opendoor="Door1",Price=rand.NextDouble()*100},
new Stock{Opendoor="Door2",Price=rand.NextDouble()*100},
new Stock{Opendoor="Door3",Price=rand.NextDouble()*100}
};
return list;
}

}
}


View Code

注:本次实现的所谓的时时刷新数据和官方提供的Demo有很大的差异,并不是后台代码的角度来刷新,而是从前端的角度来实现的。

====>点我下载DEMO<====
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐