Signalr client app not connecting to hub

signalr

What I am trying to achieve is a console app capable of connecting to and sending messages to a specific hub hosted on a website using SignalR.

I have a console app that tries to connect to the "Chat" hub on the website. Console will try to connect and then send a message and close .

    static void Main(string[] args)
    {
        var connection = new HubConnection("http://mysite:101/");
        IHubProxy myHub = connection.CreateProxy("Chat");


        connection.Start().ContinueWith(task =>
        {

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

                Console.WriteLine("Client Connected");
            }

        }).Wait();

        myHub.Invoke("Send", "Console App is Online!. ").ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("There was an error calling send: {0}", task.Exception.GetBaseException());
                Console.WriteLine(task.Status.ToString());
            }
            else
            {
                Console.WriteLine("Send Complete.");
            }
        }).Wait();

Everything works fine locally, (which I guess it would being the same app domain.)

My asp hub is just based of the chat example in the wiki https://github.com/SignalR/SignalR/wiki/

 using SignalR.Hosting.AspNet;
 using System.Threading.Tasks;

     namespace HubSignalrTest
     {

   //[HubName("Hubtest")]
   public class Chat : Hub , IDisconnect , IConnected
   {

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

The Jscript is pretty much the same as in the example as well.

    <script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
 <script type="text/javascript" src="Scripts/jquery.signalR-0.5.1.js"></script>

<script src='<%= ResolveClientUrl("~/signalr/hubs") %>'> type="text/javascript">   </script>
<script type="text/javascript">

  jQuery.support.cors = true;

  $(function () {
      // Proxy created on the fly
      var chat = $.connection.chat;

      // Declare a function on the chat hub so the server can invoke it
      chat.addMessage = function (message) {
          $('#messages').append('<li>' + message + '</li>');
      };



      $("#broadcast").click(function () {
          // Call the chat method on the server
          chat.send($('#msg').val())
              .done(function () {
                  console.log('Success!');
              })
              .fail(function (e) {
                  console.warn(e);
              });
      });

      // Start the connection
      $.connection.hub.start();
  });

I tried setting jQuery.support.cors = true; omitting or adding a hub name, and I have investigated running the console app on the same server with the correct settings (still does not connect to hub) and does not seem to be a firewall issue. Has anyone had luck with a similar project or know what I am doing wrong?. I would really like to achieve this basic communication to expand on a lot of ideas that SignalR brings.

Best Answer

If you're trying to go cross domain, then read here https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs. Scroll to the bottom

Related Topic