asp.net核心,其中signalR <-&gt;静态插座 [英] asp.net core with signalR &lt; - &gt; static socket

查看:94
本文介绍了asp.net核心,其中signalR <-&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?

thx

推荐答案

我建议您阅读以下网站上的股票报价器示例:

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

}

在启动时,我创建了此时钟的sigleton实例,该实例在应用程序运行时一直存在.

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 从Controller调用SignalR Core Hub方法

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

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