带有signalR <的asp.net核心- &gt;静态套接字 [英] asp.net core with signalR &lt; - &gt; static socket

查看:17
本文介绍了带有signalR <的asp.net核心- &gt;静态套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:我有一个带有用于消息传递的 signalR 核心的 asp.net 核心 Web 应用程序.:)

Use Case: I have a asp.net core web application with signalR core for messaging. :)

问题:我必须从套接字连接[通过 System.Net.Sockets](具有自己的套接字通信的机器)接收消息

Problem: I have to receive messages from a socket connection [via System.Net.Sockets] (machine with own socket communication)

有什么方法可以将套接字客户端集成到 Web 应用程序中(可能是 Progamm.cs 或 Startup.cs?)以及如何访问signalR 以将收到的消息转发到signalR 集线器?

Is there any way to integrate the socket client in the web app (maybe Progamm.cs or Startup.cs?) And how can I get access to the signalR to forward the received message to the signalR Hub?

谢谢

推荐答案

我建议您阅读以下股票代码示例:https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

I suggest you to read the stockticker sample on : https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

我在这里向您展示了一个小示例,您可以根据自己的应用进行调整.您必须从自己的套接字通信中订阅消息,然后才能将此消息转发给连接的客户端.

I show you here a small sample which you can adapt to your application. You have to subscribe the messages from your own socket communication and then you can forward this messages to the connected clients.

这是一个如何从服务器向客户端发送时间的小示例.(对您来说有趣的部分是 GlobalHost.ConnectionManager.GetHubContext<ClockHub>().Clients.All.sendTime(DateTime.UtcNow.ToString());.您可以向其中发送一些内容所有连接的客户端.

Here is a small sample how to send the time from server to the clients. (The interesting part for you is the line GlobalHost.ConnectionManager.GetHubContext<ClockHub>().Clients.All.sendTime(DateTime.UtcNow.ToString());. Which this you can send something to all connected clients.

我的主类是一个时钟,它向所有连接的客户端发送实际时间:

My main class is a clock which sends the actual time to all connected clients:

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;
        }
    }
}

}

在启动时,我创建了这个时钟的单例实例,只要应用程序运行,它就会一直存在.

In the startup I created a sigleton instance of this clock, which lives as long as the application is running.

  public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var inst = Clock.Instance;
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();         
        }
    }
}

我的中心:

  public class ClockHub : Hub<IClockHub>
    {

    }

Hub 接口定义了服务器可以调用的方法:

Hub interface which defines the method, which the server can call:

 public interface IClockHub
    {
        void sendTime(string actualTime);
    }

这是客户部分:

<!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 core 2.x中注入hubcontext从控制器调用 SignalR Core Hub 方法

这篇关于带有signalR <的asp.net核心- &gt;静态套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆