private void OnChatroomAcceptance(ChatroomAcceptanceMessage payload) { if (payload.Accepted == 1) { _informationHolding.ActiveUsers = payload.ActiveUsers; _informationHolding.Username = Username; _mainDispatcher.Invoke(() => { _regionManager.RequestNavigate("MainRegion", "ChatRoom"); }); } else { ErrorMessage = payload.ReasonForDecline; } }
private void TakeAction(ChatroomAcceptanceMessage message) { // Set heartbeat going _eventAggregator.GetEvent <ChatroomAcceptanceEvent>().Publish(message); if (message.Accepted == 1) { lock (_heartbeatLock) { _alive = true; } Task.Run(() => Heartbeater()); } }
public void HandleMessage(byte[] message, Socket socket) { IMessage decodedMessage; switch ((MessageIds)message[0]) { case MessageIds.ChatroomAcceptance: decodedMessage = new ChatroomAcceptanceMessage(); decodedMessage.Unpack(message); TakeAction((ChatroomAcceptanceMessage)decodedMessage); break; case MessageIds.RegularUpdate: decodedMessage = new RegularUpdateMessage(); decodedMessage.Unpack(message); TakeAction((RegularUpdateMessage)decodedMessage); break; default: break; } }
private void TakeAction(JoinChatroomMessage message, Socket socket, NetworkStream stream) { var newUser = new User() { Socket = socket, Stream = stream, Username = message.Username, MissedHeartbeats = 0 }; // Send accept/decline message var msg = new ChatroomAcceptanceMessage() { Accepted = 1, MotD = "MotD", WelcomeMessage = "Welcome to the server!" }; lock (_userTracker.TrackerLock) { if (_userTracker.Users.Any(r => r.Value.Username == message.Username)) { msg.Accepted = 0; msg.ReasonForDecline = "Already a user with that username."; } else { _userTracker.Users.Add(socket, newUser); } msg.ActiveUsers = _userTracker.Users.Select(r => r.Value.Username).ToList(); } var buffer = msg.Pack(); stream.Write(buffer, 0, buffer.Length); }