Web Forms 어플리케이션을 보유하고 있으며, SignalR이 내 요구 사항 중 하나에서 작동하는 방식을 테스트하고 있습니다. 내 허브 코드 :SignalR 허브에서 컨텍스트가 null입니다.
는using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRTest.Hubs
{
public class NotificationHub : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
public NotificationHub()
{
var myInfo = Context.QueryString["myInfo"];
_Timer.Interval = 2000;
_Timer.Elapsed += TimerElapsed;
_Timer.Start();
}
void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Random rnd = new Random();
int i = rnd.Next(0, 2);
var hub = GlobalHost.ConnectionManager.GetHubContext("NotificationHub");
hub.Clients.All.Alert(i);
}
}
}
내 고객 전화 :
<script type="text/javascript">
$(function() {
var logger = $.connection.notificationHub;
logger.client.Alert = function (msg) {
if (msg == 1) {
$("#HyperLink1").show();
$("#HyperLink2").hide();
}
else {
$("#HyperLink1").hide();
$("#HyperLink2").show();
}
};
$.connection.hub.qs = "myInfo=12345";
$.connection.hub.start();
});
</script>
(허브) 서버 코드의 컨텍스트를 조사하지만, 어떤 이유로, 그것은 null의 경우, 그래서 나는를 검색 할 수 없습니다 쿼리 문자열 값. 어떤 아이디어?
notificationsHub.server.setValue (12345); 클라이언트에서이 오류가 발생합니다. 'notificationsHub'가 정의되지 않았습니다. – WinFXGuy
당신은 아마도'notificationsHub'가 아닌'notificationHub' 허브를 호출했습니다. querystring 값을 검색 할 수있는 방법으로 내 대답을 업데이트했습니다. –
정확합니다. 컨텍스트는 OnConnected를 종료 할 때까지 null이됩니다. 재정의에서 쿼리 문자열 값을 검색 할 수있었습니다. – WinFXGuy