如何将参数传递给SignalR中的集线器?

时间:2014-03-04 21:14:49

标签: c# signalr signalr-hub

我在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集线器中使用它。

2 个答案:

答案 0 :(得分:3)

您可以通过连接(check herehere作为示例)跟踪已连接的用户(以及任何关联的元数据),并在计时器上勾选您存储的本地数据以查找所需内容。

信号R本身不会传递任何东西。客户必须传递信息。

如果您的客户有员工ID,请将其发送到连接上的信号中心。您可以添加一个处理程序,知道客户端何时连接到您的aspx页面javascript,然后将其发送到集线器。然后,集线器可以在字典中跟踪ConnectionId, EmployeeID,您可以使用它来访问特定客户端或执行任何操作。

检查我发布的链接,他们会说明如何执行此操作。

答案 1 :(得分:1)

除了接受的答案外,我还使用它来将多个查询字符串从客户端传递到中心: 在客户端中:

import itertools
import bokeh 
from bokeh import *
  source0 = ColumnDataSource(data={'x' : x1 ,'y=' : y1,})
  p1 = p.line(x = 'x',y = 'y',source = source0))

---其余代码--------

在Hub类中:

Dictionary<string, string> queryString = new Dictionary<string, string>();
        queryString.Add("key1", "value1");
        queryString.Add("key2", "value2");
        hubConnection = new HubConnection("http://localhost:49493/signalr/hubs", queryString);

我正在使用c#在Windows窗体应用程序中使用版本“ 2.3.0.0”的“ Microsoft.AspNet.SignalR.Client”库。

相关问题