Sockets asp.net核心与signalR<;-&燃气轮机;静态插座
用例: 我有一个asp.net core web应用程序,带有用于消息传递的signalR core.:) 问题: 我必须通过套接字连接[通过System.Net.Sockets](具有自己套接字通信的机器)接收消息 有没有办法将socket客户端集成到web应用程序中(可能是Progamm.cs或Startup.cs?) 我如何访问信号机,将接收到的消息转发到信号机集线器Sockets asp.net核心与signalR<;-&燃气轮机;静态插座,sockets,static,signalr,.net-core,asp.net-core-2.0,Sockets,Static,Signalr,.net Core,Asp.net Core 2.0,用例: 我有一个asp.net core web应用程序,带有用于消息传递的signalR core.:) 问题: 我必须通过套接字连接[通过System.Net.Sockets](具有自己套接字通信的机器)接收消息 有没有办法将socket客户端集成到web应用程序中(可能是Progamm.cs或Startup.cs?) 我如何访问信号机,将接收到的消息转发到信号机集线器 thx我建议您阅读以下股票代码示例: 我在这里向您展示了一个小样本,您可以根据自己的应用进行调整。您必须从自己的套接字通信
thx我建议您阅读以下股票代码示例: 我在这里向您展示了一个小样本,您可以根据自己的应用进行调整。您必须从自己的套接字通信订阅消息,然后才能将这些消息转发到连接的客户端 下面是一个如何将时间从服务器发送到客户端的小示例。 (您感兴趣的部分是行
GlobalHost.ConnectionManager.GetHubContext().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;
}
}
}
我的中心:
public class ClockHub : Hub<IClockHub>
{
}
这是客户端部分:
<!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>
$(function(){//在本例中我使用jQuery
var ticker=$.connection.clockHub;
函数init(){
}
ticker.client.sendTime=函数(h){
$(“#timeLabel”).html(h);
}
$.connection.hub.start().done(init);
});
如何在asp.net core 2.x中注入hubcontext
我建议您阅读stockticker样本: 我在这里向您展示了一个小示例,您可以根据自己的应用程序进行调整。您必须从自己的套接字通信订阅消息,然后您可以将这些消息转发到连接的客户端 下面是一个如何将时间从服务器发送到客户端的小示例。 (您感兴趣的部分是行
GlobalHost.ConnectionManager.GetHubContext().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;
}
}
}
我的中心:
public class ClockHub : Hub<IClockHub>
{
}
这是客户端部分:
<!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>
$(function(){//在本例中我使用jQuery
var ticker=$.connection.clockHub;
函数init(){
}
ticker.client.sendTime=函数(h){
$(“#timeLabel”).html(h);
}
$.connection.hub.start().done(init);
});
如何在asp.net core 2.x中注入hubcontext
它与asp.net core!GlobalHost.ConnectionManager.GetHubContext…不兼容。目前在Signal core中不存在。:/我找到了另一个使用GlobalHost的IConnectionManager instant的解决方案,但IConnectionManager也不存在于Signal core框架中。@Hannes您在问题中没有这样写。我更新了我的解决方案答:它与asp.net core!GlobalHost.ConnectionManager.GetHubContext…不兼容。目前在SignalR core中不存在。:/我找到了另一个解决方案,该解决方案使用GlobalHost的IConnectionManager instant,但IConnectionManager也不存在于SignalR core框架中。@Hannes您在问题中没有这样写。我更新了m不确定的回答