Win*_*Guy 6 c# signalr signalr-hub
我在 SignalR hub 中的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
不知何故,我需要传递 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>
Run Code Online (Sandbox Code Playgroud)
在 ASPX 页面上,我的 EmpID 位于会话对象中,我需要以某种方式在 SignalR 中心中使用它。
小智 7
除了接受的答案之外,我还使用它将多个查询字符串从客户端传递到集线器:\n在客户端中:
\n\nDictionary<string, string> queryString = new Dictionary<string, string>();\n queryString.Add("key1", "value1");\n queryString.Add("key2", "value2");\n hubConnection = new HubConnection("http://localhost:49493/signalr/hubs", queryString);\nRun Code Online (Sandbox Code Playgroud)\n\n---其余代码--------
\n\n在集线器类中:
\n\npublic override Task OnConnected()\n{\n var value1 = Convert.ToString(Context.QueryString["key1"]);\n var value2 = Convert.ToString(Context.QueryString["key2"]);\n return base.OnConnected();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我正在使用 c# 的 Windows 窗体应用程序中使用版本 \xe2\x80\x9c2.3.0.0\xe2\x80\x9d 的 \xe2\x80\x9cMicrosoft.AspNet.SignalR.Client\xe2\x80\x9d 库。
\n