如何通过SignalR实现"谁在打字"功能?

Sia*_* Ys 10 javascript c# asp.net-mvc asp.net-mvc-4

基本上我正在我的网站上实现SignalR聊天.我已经可以向所有连接的用户发送消息,现在我希望添加"谁正在打字"功能.我正在尝试将其添加到$('#message').keypress函数中并且它可以工作,但现在我无法向用户发送消息.

我做错了什么?

删除$('#message')后,keypress可以发送消息

没有删除$('#message').按键无法发送消息

在此输入图像描述

我的HTML {

<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" class="btn btn-default" />
<input type="hidden" id="displayname" />
<label id="isTyping" />
<ul id="discussion"></ul>
Run Code Online (Sandbox Code Playgroud)

}

以下是脚本:

<!--SignalR script to update the chat page and send messages.-->
<script type="text/javascript">
    $(function () {
        // Reference the auto-generated proxy for the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call back to display messages.

        chat.client.broadcastMessage = function (name, message) {
            $('#discussion').append('<li><strong>' + name
                + '</strong>:&nbsp;&nbsp;' + message + '</li>');
        };

        chat.client.sayWhoIsTyping = function (name) {
            $('#isTyping').html('<em>' + name + ' is typing...</em>');
            setTimeout(function () {
                $('#isTyping').html('&nbsp;');
            }, 5000);
        };

        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        $('#message').focus();

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

            $('#sendmessage').click(function () {
                var encodedName = $('<div />').text($('#displayname').val()).html();
                var encodedMsg = $('<div />').text($('#message').val()).html();
                chat.server.sendPublic(encodedName, encodedMsg);
                $('#message').val('').focus();
            });

            $('#message').keypress(function (e) {
                if (e.which == 13) {
                    var encodedName = $('<div />').text($('#displayname').val()).html();
                    var encodedMsg = $('<div />').text($('#message').val()).html();
                    chat.server.sendPublic(encodedName, encodedMsg);
                    $('#message').val('').focus();
                } else {
                    var encodedName = $('<div />').text($('#displayname').val()).html();
                    chat.server.isTyping(encodedName);
                }
            });
        });



    // This optional function html-encodes messages for display in the page.
    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }
Run Code Online (Sandbox Code Playgroud)

以下是我的Hub代码:

    public void SendPublic(string name, string message)
    {
        // Call the addNewMessageToPage method to update clients
        Clients.All.broadcastMessage(name, message);
    }

    public void IsTyping(string name)
    {
        SayWhoIsTyping(name);
    }

    public void SayWhoIsTyping(string name)
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        context.Clients.All.sayWhoIsTyping(name);
    }
Run Code Online (Sandbox Code Playgroud)

Luc*_*rei 10

在您的服务器上,您必须ChatHub在名称中包含两个方法:

public void IsTyping (string html) 
{
    // do stuff with the html
    SayWhoIsTyping(html); //call the function to send the html to the other clients
}

public void SayWhoIsTyping (string html)
{
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
    context.Clients.All.sayWhoIsTyping (html);
}
Run Code Online (Sandbox Code Playgroud)

这个过程如何运作?

您可以捕获您的某个客户正在键入的事件,即使用javascript的按键功能.然后,使用所需的html从服务器调用一个函数,其中包含名称和其他信息.服务器接收信息,处理它,然后从客户端调用一个函数,该函数显示谁正在键入.

在您的情况下,IsTyping来自服务器的方法将处理来自客户端的信息,并且SayWhoIsTyping来自服务器的方法将调用客户端来处理它所需的信息.

编辑你的评论:

我建议从这样的javascript修改函数:

chat.client.sayWhoIsTyping = function (name) {
    $('#isTyping').html('<em>' + name + ' is typing...</em>');
    setTimeout(function () {
        $('#isTyping').html('&nbsp;');
    }, 5000);
};

$('#message').keypress(function(e) {
    if (e.which == 13) {
        $('#sendmessage').trigger('click');
    } else {
        var encodedName = $('<div />').text($('#displayname').val()).html();
        chat.server.isTyping(encodedName);
    }
});
Run Code Online (Sandbox Code Playgroud)

并删除该 $(document).keypress功能.