[英]Can not send message to specific user By ConnectionId in SignalR
[英]SignalR send message to single connectionId
我有一個asp.net經典網站。 我已經有了SignalR基本功能(一個客戶端向其他客戶端發送消息)。 但現在我只想將消息發送到特定的connectionID。
我的中心:
** [HubName("chatHub")]
public class ChatHub : Hub
{
public static List<string> messages = new List<string>();
public void GetServiceState()
{
Clients.updateMessages(messages);
}
public void UpdateServiceState()
{
messages.Add(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
Clients.updateMessages(messages);
}
}**
Asp代碼:
<script type="text/javascript">
$(function () {
// creates a proxy to the health check hub
var healthCheckHub = $.connection.chatHub;
console.log($.connection.hub)
// handles the callback sent from the server
healthCheckHub.updateMessages = function (data) {
$("li").remove();
$.each(data, function () {
$('#messages').append('<li>' + this + '</li>');
console.log($.connection);
});
};
$("#trigger").click(function () {
healthCheckHub.updateServiceState();
});
// Start the connection and request current state
$.connection.hub.start(function () {
healthCheckHub.getServiceState();
});
});
問題是我真的不知道如何通過集群發送到一個特定的ConnectionID,因為Clients.updateMessages(messages); 向所有人發送消息。 我該如何解決這個問題?
PS:我已經看過了: 用Signalr / PersistentConnection向連接的客戶端發送服務器消息
和http://riba-escapades.blogspot.dk/2012/05/signalr-send-messages-to-single-client.html
那不起作用。
好吧,您可以從集線器向單個客戶端發送消息,如下所示:
Clients.Client(someConnectionIdIWantToSendToSpecifically).doSomething();
訣竅是您需要知道要將消息發送到的連接ID。 更具體地說,您可能想知道您要發送消息的事物的邏輯身份,因為該邏輯身份可能具有多個連接,或者已經在完全不同的連接ID下丟棄和重新連接。 將連接映射到邏輯身份是SignalR留給應用程序本身的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.