小编典典

SignalR Console应用程序示例

c#

是否有一个使用signalR将消息发送到.net集线器的控制台或winform应用程序的小示例?我尝试了.net示例,并查看了Wiki,但对我而言,集线器(.net)与客户端(控制台应用程序)之间的关系没有任何意义(找不到此类示例)。该应用程序是否仅需要连接集线器的地址和名称?

是否有人可以提供一小段代码来显示应用程序连接到集线器并发送“ Hello World”或.net集线器收到的内容?

PS。我有一个很好的标准集线器聊天示例,如果尝试在Cs中为其指定一个集线器名称,它将停止工作,即[HubName(“ test”)],您知道原因吗?

谢谢。

当前控制台应用程序代码。

static void Main(string[] args)
{
    //Set connection
    var connection = new HubConnection("http://localhost:41627/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateProxy("chat");
    //Start connection
    connection.Start().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Connected");
        }
    }).Wait();

    //connection.StateChanged += connection_StateChanged;

    myHub.Invoke("Send", "HELLO World ").ContinueWith(task => {
        if(task.IsFaulted)
        {
            Console.WriteLine("There was an error calling send: {0}",task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Send Complete.");
        }
    });
 }

集线器服务器。(不同的项目工作区)

public class Chat : Hub
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients
        Clients.addMessage(message);
    }
}

对此的信息Wiki是http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-
guide-net-client


阅读 576

收藏
2020-05-19

共1个答案

小编典典

首先,您应该通过nuget在服务器应用程序上安装SignalR.Host.Self,在客户端应用程序上安装SignalR.Client:

PM>安装包SignalR.Hosting.Self-版本0.5.2

PM>安装包Microsoft.AspNet.SignalR.Client

然后将以下代码添加到您的项目中;)

(以管理员身份运行项目)

服务器控制台应用程序:

using System;
using SignalR.Hubs;

namespace SignalR.Hosting.Self.Samples {
    class Program {
        static void Main(string[] args) {
            string url = "http://127.0.0.1:8088/";
            var server = new Server(url);

            // Map the default hub url (/signalr)
            server.MapHubs();

            // Start the server
            server.Start();

            Console.WriteLine("Server running on {0}", url);

            // Keep going until somebody hits 'x'
            while (true) {
                ConsoleKeyInfo ki = Console.ReadKey(true);
                if (ki.Key == ConsoleKey.X) {
                    break;
                }
            }
        }

        [HubName("CustomHub")]
        public class MyHub : Hub {
            public string Send(string message) {
                return message;
            }

            public void DoSomething(string param) {
                Clients.addMessage(param);
            }
        }
    }
}

客户端控制台应用程序:

using System;
using SignalR.Client.Hubs;

namespace SignalRConsoleApp {
    internal class Program {
        private static void Main(string[] args) {
            //Set connection
            var connection = new HubConnection("http://127.0.0.1:8088/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("CustomHub");
            //Start connection

            connection.Start().ContinueWith(task => {
                if (task.IsFaulted) {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine("Connected");
                }

            }).Wait();

            myHub.Invoke<string>("Send", "HELLO World ").ContinueWith(task => {
                if (task.IsFaulted) {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine(task.Result);
                }
            });

            myHub.On<string>("addMessage", param => {
                Console.WriteLine(param);
            });

            myHub.Invoke<string>("DoSomething", "I'm doing something!!!").Wait();


            Console.Read();
            connection.Stop();
        }
    }
}
2020-05-19