コード例 #1
0
ファイル: Hub.cs プロジェクト: Guuugol/Messenger
        public override Task OnDisconnected(bool stopCalled)
        {
            var item = Users.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
            if (item != null)
            {

                Guid userGuid=Guid.Parse(item.Guid);
                using (var db = new MessengerEntities1())
                {
                    foreach (var reciever in (from u in db.User
                        from c in db.Contact
                        where ((u.ID == c.UserID) && (c.ContactID == userGuid))
                        select u))
                    {
                        string recieverGuid = reciever.ID.ToString();
                        var recieverMapRecord = Users.FirstOrDefault(u => u.Guid == recieverGuid);
                        if (recieverMapRecord != null)
                        {
                            string recieverConnectionId = recieverMapRecord.ConnectionId;
                            var recieverHubDescriptor= Clients.Client(recieverConnectionId);
                            if (recieverHubDescriptor != null)
                            {
                                recieverHubDescriptor.onContactDisconnected(item.Guid);
                            }
                        }
                    }
                }
                Users.Remove(item);
            }
            return base.OnDisconnected(stopCalled);
        }
コード例 #2
0
ファイル: Hub.cs プロジェクト: Guuugol/Messenger
        public void Connect(string guid)
        {
            var id = Context.ConnectionId;

            if ((Users.All(x => x.ConnectionId != id)) && (Users.All(x => x.Guid != guid)))
            {
                Users.Add(new UserMap { ConnectionId = id, Guid = guid });
            }
            Clients.Caller.onConnected(id, guid, Users);
            Guid userGuid = Guid.Parse(guid);
            using (var db = new MessengerEntities1())
            {
                foreach (var reciever in (from u in db.User
                                          from c in db.Contact
                                          where ((u.ID == c.UserID) && (c.ContactID == userGuid))
                                          select u))
                {
                    string recieverGuid = reciever.ID.ToString();
                    var recieverMapRecord = Users.FirstOrDefault(u => u.Guid == recieverGuid);
                    if (recieverMapRecord != null)
                    {
                        string recieverConnectionId = recieverMapRecord.ConnectionId;
                        var recieverHubDescriptor = Clients.Client(recieverConnectionId);
                        if (recieverHubDescriptor != null)
                        {
                            recieverHubDescriptor.onContactConnected(guid);
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: Hub.cs プロジェクト: Guuugol/Messenger
        public void Send(string senderGuid,string recieverGuid, string message)
        {
            var user = Users.FirstOrDefault(u => u.Guid == recieverGuid);
            bool recieved;
            if (user != null) {
            var client=Clients.Client(user.ConnectionId);
                client.addMessage(senderGuid, message);
                recieved = true;
            }
            else
                recieved = false;
            using (var db=new MessengerEntities1())
            {
                db.Message.Add(new Message
                {
                    FromID = Guid.Parse(senderGuid),
                    ToID = Guid.Parse(recieverGuid),
                    Text = message,
                    ID = Guid.NewGuid(),
                    Recieved = (byte)(recieved?1:0),
                    Date_time = DateTime.Now
                });
                db.SaveChanges();

            }
        }