我创建了一个Hub类。获取所有数据但是当api调用 Clients.Group(spokeCode).broadCast(message);
函数时,它不会在我的ASP.NET网页(客户端页面)上执行广播功能。
我检查过/signalR/hubs
是否正确生成了。
2.成功调用OnConnected()
[MethodImpl(MethodImplOptions.Synchronized)]
public void BroadCastMessage(string spokeCode, String constr)
{
lock (this)
{
string procedureName = "procedureName";
string message = "message goes here ";
DataAccessLayer dal = new DataAccessLayer();
//DataSet ds = dal.Executedataset(constr, procedureName);
DataSet ds = dal.ExecuteDataset(constr, procedureName, new object[] { spokeCode });
foreach (var set in ds.Tables[0].AsEnumerable().GroupBy(c => c["SpokeCode"]))
{
try
{
DataTable dt = set.CopyToDataTable();
//for (int i = 0; i < dt.Rows.Count; i++)
//{
foreach (var r in set)
{
message += r.Field<string>("BatchNo") + " Lot No " + r.Field<string>("LotNo");
Clients.Group(spokeCode).broadCast(message);
//Clients.Group(r.Field<string>("SpokeCode")).BroadCast(message);
}
}
catch (Exception ex)
{
ErrorLogging log = new ErrorLogging();
log.EventLog(ex.Message);
//Clients.Caller.executeError("An Error has Occured Contact System Administrator");
Clients.Group(spokeCode).executeError("An Error has been Occured, Contact Administrator");
}
}
}
以下是用JavaScript编写的客户端代码
connectionHub.client.broadCast = function (message) {
alert(message);
};