난 당신의 stockticker 샘플 read하는 것이 좋습니다 : https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
를 내가 여기 당신이 당신의 응용 프로그램에 적응할 수있는 작은 샘플을 보여줍니다. 자신의 소켓 통신에서 메시지를 구독해야하며이 메시지를 연결된 클라이언트에 전달할 수 있습니다.
다음은 서버에서 클라이언트로 시간을 보내는 방법을 보여주는 작은 샘플입니다. 은 (당신을 위해 흥미로운 부분은 라인 GlobalHost.ConnectionManager.GetHubContext<ClockHub>().Clients.All.sendTime(DateTime.UtcNow.ToString());
입니다이 모든 연결된 클라이언트에 뭔가를 보낼 수있는
내 주요 클래스는 연결된 모든 클라이언트에 실제 시간을 보내는 시계입니다 :..
public class Clock
{
private static Clock _instance;
private Timer timer;
private Clock()
{
timer = new Timer(200);
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{ // ---> This is the important part for you: Get hubContext where ever you use it and call method on hub GlobalHost.ConnectionManager.GetHubContext<ClockHub>().Clients.All.sendTime(DateTime.UtcNow.ToString());
GlobalHost.ConnectionManager.GetHubContext<ClockHub>().Clients.Clients()
}
public static Clock Instance
{
get
{
if (_instance == null)
{
_instance = new Clock();
}
return _instance;
}
}
}
} 내가 응용 프로그램만큼 생활이 시계의 sigleton 인스턴스를 생성 시작에서
가 실행 중입니다.
public class Startup
{
public void Configuration(IAppBuilder app)
{
var inst = Clock.Instance;
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
,
내 허브 :
public class ClockHub : Hub<IClockHub>
{
}
서버가 호출 할 수있는 방법을 정의
허브 인터페이스 :
public interface IClockHub
{
void sendTime(string actualTime);
}
이 클라이언트 부분입니다 : hubcontext를 주입하는 방법
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="timeLabel" ></div>
<script src="scripts/jquery-1.6.4.min.js"></script>
<script src="scripts/jquery.signalR-2.2.0.js"></script>
<script src="signalr/hubs"></script>
<script>
$(function() { // I use jQuery in this example
var ticker = $.connection.clockHub;
function init() {
}
ticker.client.sendTime = function (h) {
$("#timeLabel").html(h);
}
$.connection.hub.start().done(init);
});
</script>
</body>
</html>
asp.net 코어 2.x에서 Call SignalR Core Hub method from Controller
asp.net 코어와 호환되지 않습니다! GlobalHost.ConnectionManager.GetHubContext .... 현재 SignalR 코어에 존재하지 않습니다. :/ GlobalHost의 IConnectionManager 인스턴스로 다른 솔루션을 찾았지만 IConnectionManager도 signalr 핵심 프레임 워크에 존재하지 않습니다. – Hannes
@Hannes 귀하는 귀하의 질문에이를 적지 않았습니다. 내 대답을 업데이트했습니다. – Tester