与信号员群聊。如何发送给特定用户?

时间:2016-09-27 12:48:27

标签: .net model-view-controller signalr

这些是我在数据库中添加的表。

Database table 1

Database table 2

    //Group Chat Hub
    public void GroupChatSend(string name, string message)
    {
        //AddMessageinCacheWallChat(name, message);
        Clients.All.addNewMessageToGroupPage(name, message);
    }

和     // JavaScript

var GroupChat = 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.addNewMessageToGroupPage = function (name, message) {
        // Add the message to the page. 
        $('#discussionGroupChat').append('<li><strong>' + htmlEncode(name)
            + '</strong>: ' + htmlEncode(message) + '</li>');
    };
    // Set initial focus to message input box.  
    $('#messageGroupChat').focus();
    // Start the connection.
    $.connection.hub.start().done(function () {
        var name = $('#userName').val();
        chat.server.connectWallChat(name);
        $('#sendmessageGroupChat').click(function () {
            // Call the Send method on the hub. 
            chat.server.groupChatSend(name, $('#messageGroupChat').val());
            // Clear text box and reset focus for next comment. 
            $('#messageGroupChat').val('').focus();
        });
    });
    // This optional function html-encodes messages for display in the page.
    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;

    }

}

如何仅向群组发送消息。还有成员表,其中包含id,username等。我没有使用owin

1 个答案:

答案 0 :(得分:0)

Clients.Group(groupName)。[应在此组上调用的方法](名称,消息);

http://www.asp.net/signalr/overview/guide-to-the-api/working-with-groups