SignalR Console应用程序示例 [英] SignalR Console app example

查看:122
本文介绍了SignalR Console应用程序示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a small example of a console or winform app using signalR to send a message to a .net hub?. I have tried the .net examples and have looked at the wiki but it is not making sense to me the relationship between the hub(.net) and client(console app) (could not find an example of this). Does the app just need the address and name of hub to connect?.

如果有人可以提供一小段代码来显示该应用程序连接到集线器并发送 Hello世界还是.net集线器收到的内容?

If someone could provide a small tidbit of code showing the app connecting to a hub and sending "Hello World" or something that the .net hub receives?.

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

PS. I have a standard hub chat example which works well , if I try to assign a hub name in Cs to it , it stops working i.e [HubName("test")] , do you know the reason for this?.

谢谢。

当前控制台应用代码。

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.");
        }
    });
 }

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

Hub Server. (different project workspace)

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

推荐答案

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

First of all, you should install SignalR.Host.Self on the server application and SignalR.Client on your client application by nuget :


PM>安装软件包SignalR.Hosting。自我版本0.5.2

PM> Install-Package SignalR.Hosting.Self -Version 0.5.2

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

PM> Install-Package Microsoft.AspNet.SignalR.Client

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

Then add the following code to your projects ;)

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

(run the projects as administrator)

服务器控制台应用程序:

Server console app:

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

这篇关于SignalR Console应用程序示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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