如何获取signalR客户端的连接ID在服务器端? [英] How to obtain connection ID of signalR client on the server side?
问题描述
我需要一个客户端的连接ID。我知道你可以从客户端使用 $。connection.hub.id
得到它。我需要的是要得到,而在web服务我有更新的记录在数据库中,又在网页上显示更新。我是新来signalR和计算器,所以任何的建议是AP preciated。在我的客户端的网页我有这样的:
I need to get the connection ID of a client. I know you can get it from the client side using $.connection.hub.id
. What I need is to get in while in a web service I have which updates records in a database, in turn displaying the update on a web page. I am new to signalR and stackoverflow, so any advice would be appreciated. On my client web page I have this:
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var notify = $.connection.notificationHub;
// Create a function that the hub can call to broadcast messages.
notify.client.broadcastMessage = function (message) {
var encodedMsg = $('<div />').text(message).html();// Html encode display message.
$('#notificationMessageDisplay').append(encodedMsg);// Add the message to the page.
};//end broadcastMessage
// Start the connection.
$.connection.hub.start().done(function () {
$('#btnUpdate').click(function () {
//call showNotification method on hub
notify.server.showNotification($.connection.hub.id, "TEST status");
});
});
});//End Main function
</script>
直到我想更新使用signalR页面一切正常了。在我的枢纽节目通知功能是这样的:
everything works up until I want to update the page using signalR. The show notification function in my hub is this:
//hub function
public void showNotification(string connectionId, string newStatus){
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<notificationHub>();
string connection = "Your connection ID is : " + connectionId;//display for testing
string statusUpdate = "The current status of your request is: " + newStatus;//to be displayed
//for testing, you can display the connectionId in the broadcast message
context.Clients.Client(connectionId).broadcastMessage(connection + " " + statusUpdate);
}//end show notification
我怎么能对的ConnectionId发送到我的web服务?
how can I send the connectionid to my web service?
我希望我没有试图做一些事情是不可能的。先谢谢了。
Hopefully I'm not trying to do something impossible. Thanks in advance.
推荐答案
在客户端调用服务器端功能,您可以通过 Context.ConnectionId
检索其连接ID 。现在,如果你想通过一种机制来访问连接ID外的枢纽,你可以:
When a client invokes a function on the server side you can retrieve their connection ID via Context.ConnectionId
. Now, if you'd like to access that connection Id via a mechanism outside of a hub, you could:
- 只是有集线器调用您传递的连接ID外部方法。
- 管理连接的客户端列表又名像
公共静态ConcurrentDictionary&LT;字符串,MyUserType&GT; ...
通过添加到字典OnConnected
,并从它在OnDisconnected
。一旦你有你的用户列表,你可以然后通过外部机构查询。
- Just have the Hub invoke your external method passing in the connection id.
- Manage a list of connected clients aka like
public static ConcurrentDictionary<string, MyUserType>...
by adding to the dictionary inOnConnected
and removing from it inOnDisconnected
. Once you have your list of users you can then query it via your external mechanism.
例1:
public class MyHub : Hub
{
public void AHubMethod(string message)
{
MyExternalSingleton.InvokeAMethod(Context.ConnectionId); // Send the current clients connection id to your external service
}
}
出2:
public class MyHub : Hub
{
public static ConcurrentDictionary<string, MyUserType> MyUsers = new ConcurrentDictionary<string, MyUserType>();
public override Task OnConnected()
{
MyUsers.TryAdd(Context.ConnectionId, new MyUserType() { ConnectionId = Context.ConnectionId });
return base.OnConnected();
}
public override Task OnDisconnected()
{
MyUserType garbage;
MyUsers.TryRemove(Context.ConnectionId, out garbage);
return base.OnDisconnected();
}
public void PushData(){
//Values is copy-on-read but Clients.Clients expects IList, hence ToList()
Clients.Clients(MyUsers.Keys.ToList()).ClientBoundEvent(data);
}
}
public class MyUserType
{
public string ConnectionId { get; set; }
// Can have whatever you want here
}
// Your external procedure then has access to all users via MyHub.MyUsers
希望这有助于!
这篇关于如何获取signalR客户端的连接ID在服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!