Sora
Sora

Reputation: 2551

Tracking user action when using SignalR

I am using SignalR library to create a chat room but now I want to track the user action for example if client 1 is talking with client 2 I want to show ( ... ) to tell the client 2 that the client 1 is writing a message is their anyway to do this?

Upvotes: 3

Views: 508

Answers (1)

amit_g
amit_g

Reputation: 31250

You can handle the KeyPress event and every so often (say every 10 characters or so), you can call the server side message informing the "UserTyping" action.

Client Side

<textbox id="message"></textbox>
<span id="userTyping"></span>

var keyPressCount = 0;

$("#message").on("keypress", function () {
    // Throttle the server call with some logic
    // Don't want to call the server on every keypress
    if (keyPressCount++ % 10 == 0) {
        chatHub.server.userTyping("myChatGroup");
    }
});

chatHub.client.OtherUserIsTyping = function (userName) {
    $("#userTyping").html(userName + " is typing...");
};

Server Side

public void UserTyping(groupName)
{
    var userName = "Get current user's name";

    Clients.OthersInGroup(groupName).OtherUserIsTyping(userName);
}

Upvotes: 7

Related Questions