5

I want to send data to my console application wich have a connection to my "someHub". I tried to do as described in example from a link but got no result. Server side code:

[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 side code:

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

What is the best solution for implementing this and what is the reason why i am not able to invoke the client method?

Noel Nemeth
  • 646
  • 11
  • 21
Denis
  • 833
  • 3
  • 12
  • 22
  • Did you call MapHubs? – halter73 Apr 15 '13 at 01:20
  • Yes I did. Calls from the client to the server works fine like: myHub.Invoke("GetValue").ContinueWith(task => Console.WriteLine("Value from server {0}", task.Result)); , but from the server to the client - no – Denis Apr 15 '13 at 08:31
  • Possible duplicate of [SignalR + posting a message to a Hub via an action method](http://stackoverflow.com/questions/7549179/signalr-posting-a-message-to-a-hub-via-an-action-method) – Liam Apr 21 '17 at 13:34

1 Answers1

12

Are you trying to talk to clients outside of Hub? If yes then you will have to get a HubContext outside of Hub. And then you can talk all the clients.

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

SignalR Server using Owin Self Host

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 Client Console Application consuming Signalr Hubs.

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

To run this code, create two separate applications, then first run server application and then client console application, then just hit key on server console and it will start sending messages to the client.

Mitul
  • 9,734
  • 4
  • 43
  • 60
  • 2
    Thanks. I changed Clients.Caller.sendSomeData("hello"); to Clients.Client(Context.ConnectionId).sendOrders("hello"); and everything working. – Denis Apr 15 '13 at 19:10
  • 3
    This answer does not answer the question, it is unrelated and more of a general information answer. Lastly changing Clients.Caller.sendSomeData("hello"); to Clients.Client(Context.ConnectionId).sendOrders("hello"); should not make a difference. I'd recommend un-marking this answer as correct and further investigating the issue – N. Taylor Mullen Apr 15 '13 at 21:23