jquery - Asp.net 信号 R 库...我看不到我的聊天记录,我只想与我和另一个用户聊天

标签 jquery asp.net-mvc-4 chat signalr asp.net-web-api

我正在使用 asp.net Signal R 库..以及如何使我只能与一个人聊天而不是群聊是否可以使用 asp.net Signal R 库?

我还想在不保存数据库的情况下查看聊天历史记录是否可以将聊天历史记录广播给特定用户?

///类文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace Chat.Hubs
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.
            Clients.All.addNewMessageToPage(name, message);
        }
    }
}

///查看

@{
    ViewBag.Title = "Chat";
}

<h2>Chat</h2>

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion">
    </ul>
</div>

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="@Url.Content("~/Scripts/jquery.signalR-1.1.3.min.js")"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(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.addNewMessageToPage = function (name, message) {
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(name) 
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // 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 () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

最佳答案

要向特定用户发送消息,您可以使用 Clients.Client 方法,例如:

Clients.Client(someClientsConnectionId).foo();

要检索客户端连接 ID,您可以捕获它 Context.ConnectionId。因此,您的中心可能看起来像这样来跟踪用户并专门发送给他们。

public MyHub: Hub 
{
    private static readonly ConcurrentDictionary<string, string> _users = new ConcurrentDictionary<string, string>();
...
    public void Join(string userName)
    {
        _users[theUsersUserName] = Context.ConnectionId;
    }

    public void SendToUser(string userName, string message)
    {
        Clients.Client(_users[userName]).foo(message).
    }
...
}

关于jquery - Asp.net 信号 R 库...我看不到我的聊天记录,我只想与我和另一个用户聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18933674/

相关文章:

c# - Autofac 属性注入(inject)在属性上失败

.net - 使用TCP或UDP套接字的VB 2008在线聊天应用程序

azure - Blazor Server SignalR Chat 适用于本地,不适用于 Azure

javascript - 如何使用 jquery 中止所有待处理的 ajax 请求的列表?

jquery - 滚动魔法 : Add offset to anchor scrolling?

PHP 添加字符到 jQuery.AJAX 发布的字符串

javascript - 使用 AJAX 删除项目而不刷新页面

ajax - 通过 ASP.NET MVC 4 中的查询字符串传递特殊字符

c# - 基于 MVC4 WebApi 的 LINQPad

javascript - 如何正确处理聊天页面中的 "chat updating"?