SignalR:如何从服务器调用.Net客户端方法?

时间:2013-04-14 21:12:14

标签: signalr signalr-hub signalr.client

我想将数据发送到与我的“someHub”连接的控制台应用程序。我尝试按a link中的示例所述进行,但没有结果。 服务器端代码:

[HubName("somehub")]
public class SomeHub : Hub
{
    public override Task OnConnected()
    {
        //Here I want to send "hello" on my sonsole application
        Clients.Caller.sendSomeData("hello");

        return base.OnConnected();
    }
}

Clien边码:

public class Provider
{
    protected HubConnection Connection;
    private IHubProxy _someHub;

    public Provider()
    {
        Connection = new HubConnection("http://localhost:4702/");
        _someHub = Connection.CreateHubProxy("somehub");
        Init();
    }

    private void Init()
    {
        _someHub.On<string>("sendSomeData", s =>
        {
            //This code is not reachable
            Console.WriteLine("Some data from server({0})", s);
        });

        Connection.Start().Wait();
    }
}

实现此问题的最佳解决方案是什么?我无法调用客户端方法的原因是什么?

1 个答案:

答案 0 :(得分:9)

您是否正在尝试与Hub以外的客户交谈?如果是,则必须在Hub之外获取HubContext。然后你可以和所有客户交谈。

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

使用Owin Self Host的SignalR服务器

class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8081/";

            using (WebApplication.Start<Startup>(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(3000);
                    context.Clients.All.addMessage("Current integer value : " + i.ToString());
                }
                Console.ReadLine();
            }

        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Turn cross domain on 
            var config = new HubConfiguration { EnableCrossDomain = true };
            config.EnableJavaScriptProxies = true;

            // This will map out to http://localhost:8081/signalr by default
            app.MapHubs(config);
        }
    }
    [HubName("MyHub")]
    public class MyHub : Hub
    {
        public void Chatter(string message)
        {
            Clients.All.addMessage(message);
        }
    }

信号器客户端控制台应用程序正在使用Signalr Hub。

class Program
    {
        static void Main(string[] args)
        {  
            var connection = new HubConnection("http://localhost:8081/");

            var myHub = connection.CreateHubProxy("MyHub");

            connection.Start().Wait();
            // Static type
            myHub.On<string>("addMessage", myString =>
            {
                Console.WriteLine("This is client getting messages from server :{0}", myString);
            });


            myHub.Invoke("Chatter",System.DateTime.Now.ToString()).Wait();


            Console.Read();
        }
    }

要运行此代码,请创建两个单独的应用程序,然后首先运行服务器应用程序,然后运行客户端控制台应用程序,然后只需在服务器控制台上按键,它就会开始向客户端发送消息。