最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Calling server and client method in SignalR - Stack Overflow

programmeradmin11浏览0评论

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.

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.

Share Improve this question asked Dec 9, 2013 at 13:09 Suman BiswasSuman Biswas 1693 gold badges7 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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();
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

发布评论

评论列表(0)

  1. 暂无评论