예제 #1
0
        public void GetQueueData(int?userId = null, int?roomNo = null)
        {
            IQueueMessageCallback registeredUser = OperationContext.Current.GetCallbackChannel <IQueueMessageCallback>();
            var queueData = new QueueData();

            if (userId != null)
            {
                queueData = QueueDatabase.FindQueue(userId);
            }
            if (roomNo != null)
            {
                queueData = QueueDatabase.FindQueueByRoomNo(roomNo);
            }

            try
            {
                _callbackList.Where(q => q.QueueData.RoomNo == queueData.RoomNo).Select(c => c.RegisteredUser).ToList().ForEach(
                    delegate(IQueueMessageCallback callback) {
                    callback.NotifyClientWithQueueData(queueData);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR in callback NotifyOfReceivedQueueNo: ");
                Console.WriteLine(ex.Message);
            }
        }
        //User is connecting to the service, add user to list of registered users
        public int Connect(int userId, int roomNo, string userName, bool isSender = false)
        {
            IQueueMessageCallback registeredUser = OperationContext.Current.GetCallbackChannel <IQueueMessageCallback>();

            Console.WriteLine("User {0} connected at {1}", userName, DateTime.Now.ToShortTimeString());

            //Add checking some credentails

            if (!_callbackList.Select(u => u.RegisteredUser).Contains(registeredUser))
            {
                var connectingUser = new ConnectedUser(userId, registeredUser);

                if (isSender)
                {
                    var queue = QueueDatabase.FindQueue(userId);
                    if (queue != null)
                    {
                        connectingUser.QueueData           = queue;
                        connectingUser.QueueData.Timestamp = DateTime.Now;
                    }
                    connectingUser.QueueData.UserId = userId;
                    connectingUser.QueueData.RoomNo = roomNo;
                    connectingUser.QueueData.Owner  = userName;

                    _callbackList.Add(connectingUser);
                    QueueDatabase.AddQueue(connectingUser.QueueData);
                }
                else
                {
                    //NOT TESTED
                    var queue = QueueDatabase.FindQueueByRoomNo(roomNo);
                    connectingUser.QueueData = queue;

                    _callbackList.Add(connectingUser);
                }
            }

            try
            {
                _callbackList.Select(c => c.RegisteredUser).ToList().ForEach(
                    delegate(IQueueMessageCallback callback) {
                    callback.NotifyOfEstablishedConnection(userName);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR in callback NotifyOfEstablishedConnection: ");
                Console.WriteLine(ex.Message);
            }

            return(_registeredUsers++);
        }