//User is disconnecting, remove user from list of registered users public int Disconnect(int userId, string userName) { IQueueMessageCallback registeredUser = OperationContext.Current.GetCallbackChannel <IQueueMessageCallback>(); _queueData = new QueueDataBuilder().Build(); if (_callbackList.Select(u => u.Id).Contains(userId)) { var leavingUser = _callbackList.Where(u => u.Id == userId).FirstOrDefault(); _callbackList.Remove(leavingUser); leavingUser.QueueData.Owner = string.Empty; QueueDatabase.UpdateUsersQueue(leavingUser.QueueData); Console.WriteLine("User {0} disconnected at {1}", userName, DateTime.Now.ToShortTimeString()); } try { _callbackList.Select(c => c.RegisteredUser).ToList().ForEach( delegate(IQueueMessageCallback callback) { callback.NotifyClientDisconnected(userName); }); } catch (Exception ex) { Console.WriteLine("ERROR in callback NotifyClientDisconnected: "); Console.WriteLine(ex.Message); } return(_registeredUsers--); }
//Pass additional message and call it back to the user public void ReceiveAdditionalMessage(int userId, string additionalMessage) { IQueueMessageCallback registeredUser = OperationContext.Current.GetCallbackChannel <IQueueMessageCallback>(); var queueData = QueueDatabase.FindQueue(userId); queueData.AdditionalMessage = additionalMessage; QueueDatabase.UpdateUsersQueue(queueData); var connectedUsers = _callbackList.Where(u => u.QueueData.UserId == userId).ToList(); foreach (var c in connectedUsers) { c.QueueData.AdditionalMessage = additionalMessage; } Console.WriteLine("Additional message: {0}", additionalMessage); try { _callbackList.Where(q => q.QueueData.RoomNo == queueData.RoomNo).Select(c => c.RegisteredUser).ToList().ForEach( delegate(IQueueMessageCallback callback) { callback.NotifyOfReceivedAdditionalMessage(additionalMessage); }); } catch (Exception ex) { Console.WriteLine("ERROR in callback NotifyOfReceivedAdditionalMessage: "); Console.WriteLine(ex.Message); } }
//Pass new queue number and call it back to the user public void ReceiveQueueNo(int userId, int queueNo, string userInitials) { IQueueMessageCallback registeredUser = OperationContext.Current.GetCallbackChannel <IQueueMessageCallback>(); var queueData = QueueDatabase.FindQueue(userId); //if queueNo > 0 update queueNo and turn off break, then update database if (queueNo > 0) { queueData.QueueNo = queueNo; queueData.isBreak = false; } //else if queueNo = -1 set break to true and keep do nothing with queueNo, then update database else if (queueNo == -1) { queueData.isBreak = true; } queueData.UserInitials = userInitials; QueueDatabase.UpdateUsersQueue(queueData); //update queue of each connected user containing same queue userid var connectedUsers = _callbackList.Where(u => u.QueueData.UserId == userId).ToList(); foreach (var c in connectedUsers) { c.QueueData = queueData; } try { _callbackList.Where(q => q.QueueData.RoomNo == queueData.RoomNo).Select(c => c.RegisteredUser).ToList().ForEach( delegate(IQueueMessageCallback callback) { callback.NotifyOfReceivedQueueNo(queueData.QueueNoMessage); }); } catch (Exception ex) { Console.WriteLine("ERROR in callback NotifyOfReceivedQueueNo: "); Console.WriteLine(ex.Message); } }