0

I am trying to send a message from console app to a MVC application using SignalR, following is the code:

static void Main(string[] args)
    {

        string url = "http://localhost:8080";
        string line=null;
        MyHub obj = new MyHub();

        using (WebApp.Start(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();

            Console.WriteLine("Enter your message:");
            line = Console.ReadLine();
            obj.Send(line);

        }
        
    }
}
class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}
public class MyHub : Hub
{
    public void Send(string message)
    {
        Clients.All.addMessage(message);
    }
}

and here is how iam trying to get the message in the website :

<script type="text/javascript">
    $(function () {
        //Set the hubs URL for the connection
        $.connection.hub.url = "http://localhost:8080/signalr";

        // Declare a proxy to reference the hub.
        var chat = $.connection.myHub;

        // Create a function that the hub can call to broadcast messages.
        chat.client.addMessage = function (message) {
            // Html encode display name and message.
            //var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page.
            $('#discussion').append('<li><strong>'
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        //$('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        //$('#message').focus();
        // Start the connection.

        
    });
</script>

The thing is that iam getting the following exception:

Exception

It feels like i cant directly instantiate an object of the class Myhub, any idea on how to fix this, remember that i need to send the message from the console to the webpage.. Any recommendations???

Community
  • 1
  • 1
moji
  • 267
  • 5
  • 15

1 Answers1

1

With SignalR you do not instantiate the hub classes, SignalR does and it won't necessarily use the same hub instance twice. Never store state data in the hub class itself.

SignalR uses a hub context to track clients and allow you to interact with them. You need to get this context from the SignalR library before you can send information, etc. The IHubContext gives you the Clients and Groups members that you use in the hub, allowing you to do the same things you would do in the hub.

Try this:

static void Main(string[] args)
{
    string url = "http://localhost:8080";
    using (WebApp.Start(url))
    {
        Console.WriteLine("Server running on {0}", url);

        // get text to send
        Console.WriteLine("Enter your message:");
        string line = Console.ReadLine();

        // Get hub context 
        IHubContext ctx = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

        // call addMessage on all clients of context
        ctx.Clients.All.addMessage(line);

        // pause to allow clients to receive
        Console.ReadLine();
    }
}
Corey
  • 15,524
  • 2
  • 35
  • 68
  • at this line `// call addMessage on all clients of context ctx.Clients.All.addMessage(message);` do you mean line instead of message?? – moji Dec 02 '15 at 01:30
  • @moji Yes, my test code didn't use your variable names. Sorry, updated. – Corey Dec 02 '15 at 02:01
  • What is a webapp here, how can i get it working. Please help. Do you have the full code without any errors and libraries referenced at the top ? – BlackEagle Apr 23 '21 at 10:47
  • @BlackEagle This answer is woefully out of date. SignalR has evolved a lot in the last 5+ years. – Corey Apr 23 '21 at 12:22
  • @Corey : Can you guide me to an working example for sending streaming text from a console application to a web page client. I have searched for many but could not find anything concrete using SignalR. – BlackEagle Apr 23 '21 at 13:59