javascript 在 SignalR 中调用服务器和客户端方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20471468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:34:34  来源:igfitidea点击:

Calling server and client method in SignalR

c#javascriptasp.netsignalr-hub

提问by Suman Biswas

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.

下面是使用 ASP.Net SignalR Hub 技术的代码。这些代码没有按预期工作。当我点击 Hello Butting 时它工作正常,但是当我点击 UserList 时什么也没有发生。我已经发出警报,发现服务器方法正在调用,但之后什么也没有发生。

JavaScript

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

和 C# 代码

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

和 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.

请帮助找出问题所在。

回答by scheien

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.

我猜生成的 javascript 有命名的 java 约定。这是我从这两种方法中看到的唯一区别。

回答by scheien

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.

正如@scheien 所提到的,您需要使用小写的第一个字母来调用该方法。或者,您可以指定中心方法名称,详见此处

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

and then call server functions using that new name

然后使用该新名称调用服务器函数

chat.server.MyNewMethodName();

回答by user3071591

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

but...... userListsdoes not exist on the hub:

但是...... userLists在集线器上不存在:

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

这是一回事,另一件让我大吃一惊的是 javascript 部分不是以大写字母开头

So you must also change Friends, Hello, OnlineFriendsinto friends, helloonlineFriends

所以你也必须把Friends, Hello,OnlineFriends变成friends,helloonlineFriends