클라이언트가 서버 허브에서 메서드를 호출 한 다음 표시 할 수 있다는 점에서 ASP.NET (이전의 MVC) 서버와 Windows 서비스 클라이언트간에 SignalR이 작동합니다 브라우저에게. 허브 코드는 다음과 같습니다 클라이언트 (Windows 서비스) 나는 허브의 메서드 호출 된에SignalR - 서버에서 서버의 허브 메서드를 호출하려면 어떻게해야합니까?
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Call Windows Service
string message1 = System.Environment.MachineName;
Clients.All.Notify(message1);
}
public void CallForReport(string reportName)
{
Clients.All.CallForReport(reportName);
}
: 이제
var hubConnection = new HubConnection("http://localhost/AlphaFrontEndService/signalr",
useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
await hubConnection.Start();
string cid = hubConnection.ConnectionId.ToString();
eventLog1.WriteEntry("ConnectionID: " + cid);
// Invoke method on hub
await alphaProxy.Invoke("Hello", "Message from Service - ConnectionID: " + cid + " - " + System.Environment.MachineName.ToString() + " " + DateTime.Now.ToString());
는이 시나리오를 가정 : 사용자가 특정 ASP.NET 갈거다 서버의 Insured.aspx와 같은 형식입니다. 그 나는 CallForReport를 호출 한 다음 클라이언트에서이 메소드를 호출 할 :
public void CallFromReport(string reportName)
{
eventLog1.WriteEntry(reportName);
}
가 어떻게 서버에서 내 자신의 허브로의 연결을 얻고 메소드를 호출 않습니다. Insured.aspx에서 다음과 같은 작업을 시도했습니다.
protected void Page_Load(object sender, EventArgs e)
{
// Hubs.AlphaHub.CallForReport("Insured Report");
// IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<AlphaHub>();
// hubContext.Clients.All.CallForReport("Insured Report");
}
마지막으로 주석 처리 된 코드 조각 (마지막 두 줄은 무엇입니까?)을 실행하면 어떻게됩니까? GetHubContext 팩토리를 통해 로컬로 인스턴스화 된 허브 인스턴스를 가져 오는 올바른 방법입니다. 브로드 캐스트를 위해 작동해야하는 허브가 구성되고 작동한다고 가정합니다. –