Esempio n. 1
0
        // A specific client identified by connection ID.
        public async Task Client(string userId, string message)
        {
            var name = Context.User.Identity.Name;

            using (var db = new UserContext())
            {
                var user = db.Users.Find(userId);
                if (user == null)
                {
                    await Clients.Caller.showErrorMessage("Could not find that user.");
                }
                else
                {
                    db.Entry(user)
                        .Collection(u => u.Connections)
                        .Query()
                        .Where(c => c.Connected == true)
                        .Load();

                    if (user.Connections == null)
                    {
                        await Clients.Caller.showErrorMessage("The user is no longer connected.");
                    }
                    else
                    {
                        foreach (var connection in user.Connections)
                        {
                            await Clients.Client(connection.ConnectionId)
                                .addChatMessage(name + ": " + message);
                        }
                    }
                }
            }
        }