模塊功能說明:
實現技術:sqlserver,MVC,WebAPI,ADO.NET,SignalR(服務器主動推送)
特殊車輛管理--->移動客戶端采集數據存入數據庫---->只要數據庫數據有變化,服務端自動推送到Wbe頁面展示(區別於傳統的web請求)-->審核人員審核數據-->返回審核結果給移動客戶端
在開發之前搜索了大量文檔,也在QQ群咨詢過群里的大牛,不少人都感覺比較懵逼的樣子。有人建議使用ajax中的長連接,輪詢等技術,最終還是決定使用SignalR技術實現頁面刷新的效果:
搜索了不少博客園大牛有關於使用SignakR的博客,最后終於找到一個,也是我目前在項目中實現功能的Demo。
以下內容轉自博客園園友楊根祥,感謝大牛提供資料,在此借鑒,如有不妥,請聯系刪除。
原博客中的代碼在項目使用中有問題,已在下面代碼中解決。
測試環境
.net 4.6
vs2015
mvc5
sqlserver2008
1.數據庫
CREATE TABLE [dbo].[CarInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CarNo] [varchar](50) NOT NULL,
[Lng] [varchar](50) NOT NULL,
[Lat] [varchar](50) NOT NULL,
[LocDt] [datetime] NOT NULL,
CONSTRAINT [PK_CarInfo] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Go
數據庫要使用service broker
ALTER DATABASE DBName SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE DBName SET ENABLE_BROKER;
初始化數據庫
INSERT INTO CARINFO (CARNO,LNG,LAT,LOCDT) VALUES ('豫A12345','113.123','43.123',GETDATE())
2.新建MVC項目
安裝SignalR包
Install-Package Microsoft.AspNet.SignalR
修改startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR();
}
}
修改web.config,增加數據庫連接字符串
<add name="ConnString" connectionString="data source=.;initial catalog=MySignalR;persist security info=True;user id=sa;password=yourpass;" providerName="System.Data.SqlClient" />
增加數據模
public class CarInfo
{
public int ID { get; set; }
public string CarNo { get; set; }
public string Lng { get; set; }
public string Lat { get; set; }
public DateTime LocDt { get; set; }
}
新建類
public class CarInfoHub : Microsoft.AspNet.SignalR.Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<CarInfoHub>();
context.Clients.All.displayStatus();
}
}
業務實現
public class CarInfoRepository
{
public CarInfo GetData(int id)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand($"SELECT [ID],[CarNo],[Lng],[Lat],[LocDt] FROM [dbo].[CarInfo] WHERE ID = {0}", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += dependency_OnChange;
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
return new CarInfo()
{
ID = reader.GetInt32(0),
CarNo = reader.GetString(1),
Lng = reader.GetString(2),
Lat = reader.GetString(3),
LocDt = reader.GetDateTime(4)
};
}
}
}
}
return null;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
CarInfoHub.Show();
}
}
修改Global.asax
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_End(object sender, EventArgs e)
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
}
新建API
public class DefaultController : ApiController
{
readonly CarInfoRepository _repository = new CarInfoRepository();
// GET api/values
public CarInfo Get(int id)
{
return _repository.GetData(id);
}
}
修改控制器
public ActionResult Index()
{
//這里只是測試功能,指定了車輛編號
ViewBag.ID = 1;
return View();
}
修改視圖
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>車輛實時跟蹤</title>
<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 maxID;
$(function () {
var CarID = @ViewBag.ID;
// Proxy created on the fly
var job = $.connection.carInfoHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tblJobInfo');
$.ajax({
url: '../api/Default/'+CarID,
type: 'GET',
datatype: 'json',
success: function (data) {
$tbl.empty();
$tbl.append(' <tr><th>ID</th><th>CarNo</th><th>Lng</th><th>Lat</th></tr>');
var str ='<tr><td>' + data.ID + '</td><td>' + data.CarNo + '</td><td>' + data.Lng + '</td><td>' + data.Lat + '</td></tr>';
$tbl.append(str);
}
});
}
</script>
</head>
<body>
<div>
<table id="tblJobInfo" style="text-align: center; margin-left: 10px">
</table>
</div>
</body>
</html>
現在測試一下
update carinfo set
Lng = CAST(Lng as float)+0.0001,
Lat = CAST(Lat as float)+0.0001,
LocDt = getdate();
面頁數據可以實時發生變化。
下一步:使用 BootStrap框架中的Table展示數據,實現數據綁定,動態刷新。