コード例 #1
0
ファイル: ChatHub.cs プロジェクト: jorwalk/MvcChatR
        // 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);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: PresentorHub.cs プロジェクト: jorwalk/MvcChatR
        //
        // OVERRIDES
        public override Task OnConnected()
        {
            // Add your own code here.
            // For example: in a chat application, record the association between
            // the current connection ID and user name, and mark the user as online.
            // After the code in this method completes, the client is informed that
            // the connection is established; for example, in a JavaScript client,
            // the start().done callback is executed.
            var version = Context.QueryString["version"];
            if (version != "1.0")
            {
                Clients.Caller.notifyWrongVersion();
            }

            var name = Context.User.Identity.Name;
            // create 
            using (var db = new UserContext())
            {
                var user = db.Users
                    .Include(u => u.Connections)
                    .SingleOrDefault(u => u.UserName == name);

                if (user == null)
                {
                    user = new User
                    {
                        UserName = name,
                        Connections = new List<Connection>()
                    };
                    db.Users.Add(user);
                }

                user.Connections.Add(new Connection
                {
                    ConnectionId = Context.ConnectionId,
                    UserAgent = Context.Request.Headers["User-Agent"],
                    Connected = true
                });
                db.SaveChanges();
            }
            Connections.Add(name, Context.ConnectionId);
            return base.OnConnected();
        }
コード例 #3
0
ファイル: ChatHub.cs プロジェクト: jorwalk/MvcChatR
        public override Task OnDisconnected()
        {
            var name = Context.User.Identity.Name;
            using (var db = new UserContext())
            {
                var connection = db.Connections.Find(Context.ConnectionId);
                connection.Connected = false;
                db.SaveChanges();
            }

            Connections.Remove(name, Context.ConnectionId);
            return base.OnDisconnected();
        }