我在SignalR枢纽代码:如何在SignalR中传递参数到集线器?
public class AlertHub : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
static AlertHub()
{
_Timer.Interval = 60000;
_Timer.Elapsed += TimerElapsed;
_Timer.Start();
}
static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Random rnd = new Random();
//int i = rnd.Next(0,2);
Alert alert = new Alert();
i = alert.CheckForNewAlerts(EmpId);
var hub = GlobalHost.ConnectionManager.GetHubContext("AlertHub");
hub.Clients.All.Alert(i);
}
}
不知怎的,我需要通过EMPID参数。如何做到这一点?
一些更多的客户的详细资料: 在我的aspx页面我有以下代码:
<script type="text/javascript">
$(function() {
var alert = $.connection.alertHub;
alert.client.Alert = function (msg) {
if (msg == 1) {
$("#HyperLink1").show();
$("#HyperLink2").hide();
}
else {
$("#HyperLink1").hide();
$("#HyperLink2").show();
}
//$("#logUl").append("<li>" + msg + "</li>");
};
$.connection.hub.start();
});
</script>
在ASPX页面,我的EmpID是在会话对象,我需要在SignalR以某种方式使用它枢纽。
'EmpId'从哪里来? – devshorts
这就是我需要通过的,但我很无聊,在哪里以及在哪里做到这一点。 – WinFXGuy
我不确定我关注。这个属性意味着什么,即使你知道如何通过它,你也可以在哪里访问它?它可以从其他地方暴露吗?您无法在构造函数中传递任何内容,因为您不负责创建Hub。任何你想让集线器拥有它的东西都需要知道,或者从其他地方拉出来。 – devshorts