Signalr 存储Context.connectionID的信号r

Signalr 存储Context.connectionID的信号r,signalr,signalr-hub,Signalr,Signalr Hub,我是Signal的新手,我想生成特定客户机的连接ID并将其存储在数据库中,一旦任何客户机完成任何更新,将通知所有客户机,但将context.connectionID存储到数据库是否是良好做法,如果不是,我想知道维护所有客户机之间的连接是否需要帮助 当客户端调用服务器端的函数时,您可以通过Context.ConnectionId检索它们的连接ID。现在,如果您希望通过集线器外部的机制访问该连接Id,您可以: 只需让中心调用传入连接id的外部方法。 管理连接的客户端列表,如公共静态Concurren

我是Signal的新手,我想生成特定客户机的连接ID并将其存储在数据库中,一旦任何客户机完成任何更新,将通知所有客户机,但将context.connectionID存储到数据库是否是良好做法,如果不是,我想知道维护所有客户机之间的连接是否需要帮助

当客户端调用服务器端的函数时,您可以通过
Context.ConnectionId
检索它们的连接ID。现在,如果您希望通过集线器外部的机制访问该连接Id,您可以:

只需让中心调用传入连接id的外部方法。 管理连接的客户端列表,如
公共静态ConcurrentDictionary
。。。通过在
OnConnected
中添加到字典,并在
OnDisconnected
中从字典中删除。一旦你有了你的用户列表,你就可以通过你的外部机制来查询它

Ex 1:

    public class MyHub : Hub
    {
        public void AHubMethod(string message)
        {
            // Send the current clients connection id to your external service
            MyExternalSingleton.InvokeAMethod(Context.ConnectionId); 
        }
    }
EX : 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
ex1:
公共类MyHub:Hub
{
公共void AHubMethod(字符串消息)
{
//将当前客户端连接id发送到外部服务
MyExternalSingleton.InvokeAMethod(Context.ConnectionId);
}
}
例:2
公共类MyHub:Hub
{
公共静态ConcurrentDictionary MyUsers=新ConcurrentDictionary();
已连接的公用覆盖任务()
{
MyUsers.TryAdd(Context.ConnectionId,新的MyUserType(){ConnectionId=Context.ConnectionId});
返回base.OnConnected();
}
公共覆盖任务OnDisconnected()
{
MyUserType垃圾;
TryRemove(Context.ConnectionId,out垃圾);
返回base.OnDisconnected();
}
公共数据(){
//值是读取时复制的,但是Clients.Clients需要IList,因此ToList()是
Clients.Clients(MyUsers.Keys.ToList()).ClientBoundEvent(数据);
}
}
公共类MyUserType
{
公共字符串连接ID{get;set;}
//你想要什么就吃什么
}
//然后,您的外部过程可以通过MyHub.MyUsers访问所有用户

当客户端调用服务器端的函数时,您可以通过
Context.ConnectionId
检索它们的连接ID。现在,如果您希望通过集线器外部的机制访问该连接Id,您可以:

只需让中心调用传入连接id的外部方法。 管理连接的客户端列表,如
公共静态ConcurrentDictionary
。。。通过在
OnConnected
中添加到字典,并在
OnDisconnected
中从字典中删除。一旦你有了你的用户列表,你就可以通过你的外部机制来查询它

Ex 1:

    public class MyHub : Hub
    {
        public void AHubMethod(string message)
        {
            // Send the current clients connection id to your external service
            MyExternalSingleton.InvokeAMethod(Context.ConnectionId); 
        }
    }
EX : 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
ex1:
公共类MyHub:Hub
{
公共void AHubMethod(字符串消息)
{
//将当前客户端连接id发送到外部服务
MyExternalSingleton.InvokeAMethod(Context.ConnectionId);
}
}
例:2
公共类MyHub:Hub
{
公共静态ConcurrentDictionary MyUsers=新ConcurrentDictionary();
已连接的公用覆盖任务()
{
MyUsers.TryAdd(Context.ConnectionId,新的MyUserType(){ConnectionId=Context.ConnectionId});
返回base.OnConnected();
}
公共覆盖任务OnDisconnected()
{
MyUserType垃圾;
TryRemove(Context.ConnectionId,out垃圾);
返回base.OnDisconnected();
}
公共数据(){
//值是读取时复制的,但是Clients.Clients需要IList,因此ToList()是
Clients.Clients(MyUsers.Keys.ToList()).ClientBoundEvent(数据);
}
}
公共类MyUserType
{
公共字符串连接ID{get;set;}
//你想要什么就吃什么
}
//然后,您的外部过程可以通过MyHub.MyUsers访问所有用户

@客户端重新连接时,connectionID是否一直在更改?@客户端重新连接时,connectionID是否一直在更改。。?