Suman Biswas
Suman Biswas

Reputation: 169

Calling server and client method in SignalR

Below code with ASP.Net SignalR Hub technology. These codes are not working as expected. When I am clicking on Hello Butting it is working fine but when I am clicking on UserList nothing is happening. I have placed an alert and found server method is invoking but after that nothing happening.

JavaScript

$(function () {
    // Declare a proxy to reference the hub.
    var chat = $.connection.chatHub;

    chat.client.OnlineFriends = function (userLists) {
        alert(userLists);
    };

    chat.client.Hello = function (message) {
        alert(message);
    };

    // Start the connection.
    $.connection.hub.start().done(function () {

        $('#btnGetUser').click(function () {
            chat.server.Friends();
        });

        $('#btnHello').click(function () {                    
            chat.server.test("message to server from client");                    
            //
        });
    });
});

and C# code

public class ChatHub : Hub
{
    public void Test(string str)
    {
        Clients.Caller.Hello(str + " | Message received at server reply from server.");
    }
    public void Friends()
    {
        Clients.Caller.OnlineFriends("myid");
    }
}

and HTML

<div>

    <input type="button" id="btnHello" value="Hello" />
    <input type="button" id="btnGetUser" value="UserList" />
</div>

Please help to find out what is the problem.

Upvotes: 1

Views: 15536

Answers (3)

user6929072
user6929072

Reputation:

As mentioned by @scheien you need to call the method using a lower case first letter. Optionally, you can specify the hub method name as detailed here.

[HubMethodName("MyNewMethodName")]
public void Friends()

and then call server functions using that new name

chat.server.MyNewMethodName();

Upvotes: 3

user3071591
user3071591

Reputation: 83

chat.client.onlineFriends = function (userLists) {
        alert(userLists);
    };

but...... userLists does not exist on the hub:

Clients.CalleronlineFriends("myid");

This was one thing, and another thing that cought my eye was that the javascript part doesn't start with a capital letter

So you must also change Friends , Hello , OnlineFriends into friends, hello onlineFriends

Upvotes: 0

schei1
schei1

Reputation: 2487

Could you try to change the line

chat.server.Friends();

to:

chat.server.friends();

I guess the generated javascript has the java convention on naming. That is the only difference I can see from those two methods.

Upvotes: 3

Related Questions