asp.net core with signalR < - > static socket

2019-08-22 17:16发布

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

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

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

1条回答
beautiful°
2楼-- · 2019-08-22 17:49

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.

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

My Hub:

  public class ClockHub : Hub<IClockHub>
    {

    }

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

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

This is the clients part:

<!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>

How to inject hubcontext in asp.net core 2.x Call SignalR Core Hub method from Controller

查看更多
登录 后发表回答