SignalR отправить с консоли сервера приложение клиенту - PullRequest
0 голосов
/ 15 ноября 2018

возможно ли даже отправить сообщение всем (или выбранным) подключенным клиентам, подключенным к концентратору? Я имею в виду со стороны сервера для всех клиентов. Я могу получить данные о клиентах в окне сервера, но когда я вхожу и пытаюсь отправить со стороны сервера, в окне клиентов ничего не происходит. Как отправить уведомления всем прямо из серверного приложения?

Сервер:

namespace SignalRHub
{


    class Program
    {
        static void Main(string[] args)
        {
            string url = @"http://localhost:8080/";
            using (WebApp.Start<Startup>(url))
            {
                Console.WriteLine(string.Format("Server running at {0}", url));
                Console.ReadLine();

                while (true)
                {
                    // get text to send
                    Console.WriteLine("Enter your message:");
                    string line = Console.ReadLine();

                    // Get hub context 
                    IHubContext ctx = GlobalHost.ConnectionManager.GetHubContext<TestHub>();

                    // call addMessage on all clients of context
                    ctx.Clients.All.addMessage(line);

                    // pause to allow clients to receive
                    Console.ReadLine();

                }
            }
        }

        class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();

            }
        }

        [HubName("TestHub")]
        public class TestHub : Hub
        {
            public void SendMsg(string message)
            {
                Console.WriteLine(message);
            }
        }

    }
}

Клиент:

namespace SignalRClient
{
    class Program
    {
        static void Main(string[] args)
        {
            IHubProxy _hub;

             string url = @"http://localhost:8080/";
            try
            {
                Console.WriteLine("Connecting to: " +url);

                var connection = new HubConnection(url);
                _hub = connection.CreateHubProxy("TestHub");
                connection.Start().Wait();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("---------- Connection OK.");
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("------------ Connection FAILED: ");
                Console.WriteLine(e);
                throw;
            }




            string line = null;
            while ((line = System.Console.ReadLine()) != null)
            {
                _hub.Invoke("SendMsg", line).Wait();
            }

            Console.Read();
        }

    }
}
...