gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:51:53 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com