Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a server client environment with SignalR. I read a lot of information on the internet and also a lot on Stack Overflow but I am totaly stuck :(

What I have tried:

Here is what I did:

I created a ASP.NET web application .NET framework (publised as ISS website port 50999, acting as my server). I added Microsoft.AspNet.SignalR, Microsoft.AspNet.SignalR.Core, Microsoft.AspNet.SignalR.JS and Microsoft.AspNet.SignalR.SystemWeb to my solution with the packet manager console.

I created a Owin startup class:

C#
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(TestSignalR.Startup))]

namespace TestSignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
            app.MapSignalR();
        }
    }
}


I created a hub, with a return message to all clients:

C#
[HubName("mart")]
    public class MartHub : Hub
    {
        public Task SendMessage(string user, string message)
        {
            using (StreamWriter file = new StreamWriter(@"C:\temp\logServer.txt", true))
            {
                file.WriteLine("Test");
            }

            // Clients must have an eventhandler for ReceiveMessage
            return Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }


For my client I created a simple console app with my receive message handler

C#
static void Main(string[] args)
        {
            var connection = new HubConnection("http://127.0.0.1:50999/");
            var myHub = connection.CreateHubProxy("mart");

            myHub.On<string, string>("ReceiveMessage", (user, message) =>
            {
                Console.WriteLine(user, message);
                Console.Read();
            });

            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>("SendMessage","mart","test").ContinueWith(task => {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine(task.Result);
                }
            });


When I run my client, the Sendmessage method on my server runs great. The test line is placed into my txt file. But.. I do not receive my message back from the server.

I also tried it from a button on my default.aspx page on the server but that did not work either

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<MartHub>();
            context.Clients.All.Send("ReceiveMessage", "Mart", "Test");
        }


Can somebody tell me what is going wrong here?
Posted
Comments
[no name] 5-Nov-20 19:45pm    
You're trying to send a message to yourself using broadcasting? Is that a normal thing?

1 solution

Gerry,

I'm encountering the same issues. I don.t have an answer yet.

There is very little info with no complete solutions on Google from Microsoft. There are also no modern books.

It's all too bad because its a good concept. I almost have it running as a console client but not quite yet.

Jim Lennane
 
Share this answer
 
Comments
Richard Deeming 22-Jan-21 4:31am    
"Me too" is not a solution to this question.

If you want to add a comment, click the "Have a Question or Comment?" button under the question and post a comment. Do not post your comment as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900