![]() This approach works when you have multiple web servers because each web server can interact with the same data repository. This topic shows how to use either a database or Azure table storage for storing connection information. using Ĭlients.Group(who).addChatMessage(name + ": " + message) The following example shows how to implement single-user groups. This action is automatically performed by the SignalR framework. You should not manually remove the user from the group when the user disconnects. If a user has more than one connection, each connection id is added to the user's group. The name of each group is the name of the user. You can create a group for each user, and then send a message to that group when you want to reach only that user. If (!_connections.GetConnections(name).Contains(Context.ConnectionId)) _connections.Remove(name, Context.ConnectionId) Public override Task OnDisconnected(bool stopCalled) _connections.Add(name, Context.ConnectionId) String name = įoreach (var connectionId in _connections.GetConnections(who))Ĭlients.Client(connectionId).addChatMessage(name + ": " + message) Public void SendChatMessage(string who, string message) Private readonly static ConnectionMapping _connections = The instance of the class is stored in a variable name _connections. The next example shows how to use the connection mapping class from a hub. Public void Remove(T key, string connectionId) If (_connections.TryGetValue(key, out connections)) If (!_connections.TryGetValue(key, out connections)) Public void Add(T key, string connectionId) Private readonly Dictionary> _connections = The key for the HashSet will be the user's name. The first example shows a class that manages the mapping of users to connections. In-memory storage does not work if your environment includes more than one web server because each server would have a separate collection of connections. ![]() If the application shuts down, all of the information is lost, but it will be re-populated as the users re-establish their connections. For example, a user who is connected through multiple devices or more than one browser tab would have more than one connection id. At any time a user could have more than one connection to the SignalR application. The dictionary uses a HashSet to store the connection id. The following examples show how to retain connection and user information in a dictionary that is stored in memory. Public void Send(string userId, string message) Sending a message to a specific user public class MyHub : Hub To change this, register your implementation of IUserIdProvider with the global host when your application starts: (typeof(IUserIdProvider), () => new MyIdProvider()) įrom within a hub, you'll be able to send messages to these users via the following API: The IUserIdProvider public interface IUserIdProviderīy default, there will be an implementation that uses the user's as the user name. This feature allows users to specify what the userId is based on an IRequest via a new interface IUserIdProvider. The following table shows which approach works for these considerations. Whether the latency of calling an external server is an issue.Whether you need to persist group and user information when the application or server restarts. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |