MOR_SNOW
MOR_SNOW

Reputation: 831

Contacting the method on the server from client code

I am trying to use SignalR for the first time, wanting to add it to my project.

My problem right now, is that I can't get a message from the server. I am getting this error:

TypeError: signalRhub.Test is not a function

My reference to scripts look like this (which seems to work):

<script src="~/Scripts/jquery.signalR-2.3.0.js"></script>
<script src="~/Scripts/jquery.signalR-2.3.0.min.js"></script>
<script src='~/signalr/js'></script>

Here is my client code:

<script>
    $(function () {
        var signalRhub = $.connection.hubSignalR;
        console.log(signalRhub);
        //Start connection
        $.connection.hub.start(function () {
            signalRhub.Test(function (response) {
                 alert("Response from server: " + response);
                //$("#testDiv").html(response);                
            });            
        });
    });
</script>

Here is my server class definition, and the method I'm trying to call (that cannot be found):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

    namespace AuthSys.SignalRhub
    {
        public class HubSignalR : Hub
        {
            public string Test()
            {
                return "Success from server";
            }
        }
    }

------------------- Additional information --------------- enter image description here

I have attached a screenshot of my Firefox console. In my client code, you will notice this code:

console.log(signalRhub);

That is what you see in the Firefox console. Looking at the child called connection, it looks like the connection is made. Here you see the URL it generates etc. And looking at the serverchild (expanded 2 levels), it looks like it found the Test() method that is on the server.

My Startup.cs looks like this (If of any importance):

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(AuthSys.Startup))]
namespace AuthSys
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            app.MapSignalR();
        }
    }
}

Any one might know what my problem is?

What I am expecting (trying to do) is to get an alert on my screen, with the string:

Success from server

that is seen in the C# class on the server.

Upvotes: 1

Views: 81

Answers (1)

Udara Kasun
Udara Kasun

Reputation: 2216

Change your C# code to publish your message

public string Test() {
   //Clients.Client("ToClientId").TestMessages("Message");//send messages to specific user
   Clients.All.TestMessages("Message"); //send messages to all user
}

Send method Javascript

signalRhub.server.test();//Note you didn't have created parameter, but you can create them

Javascript method like this

signalRhub.client.TestMessages = function (response) {
    alert("Response from server: " + response);
};

Upvotes: 1

Related Questions