void clientPing_Elapsed(object sender, ElapsedEventArgs e) { try { foreach (var conn in clientConnections) { if (conn.Value > 2) { var socket = sockets.FirstOrDefault(d => d.ConnectionInfo.Id == conn.Key); if (socket != null) { lock (socketSync) { sockets.Remove(socket); socket.Close(); QuizGame.Instance.UserQuit(socket.ConnectionInfo.Id); } } } } foreach (var socket in sockets) { if (!clientConnections.Any(d => d.Key == socket.ConnectionInfo.Id)) clientConnections.Add(socket.ConnectionInfo.Id, 0); QuizMessage msg = new QuizMessage() { Type = "Ping", Message = socket.ConnectionInfo.Id.ToString() }; string pingClient = new JavaScriptSerializer().Serialize(msg); socket.Send(pingClient); clientConnections[socket.ConnectionInfo.Id]++; } } catch (Exception ex) { logger.DebugException("clientPing_Elapsed", ex); } }
private void AddMessage(QuizMessage message) { lock (syncMessage) { int index = 1; if (messages.Any()) index = messages.Keys.Max() + 1; if (messages.Count > MAX_MESSAGES) messages.Remove(messages.Keys.Min()); message.Id = index; var jsonMessage = new JavaScriptSerializer().Serialize(message); messages.Add(index, jsonMessage); if (MessageArrived != null) MessageArrived(this, new EventArgs()); } }
private void AuthenticateUser(IDictionary<string, string> SocketCookies, IWebSocketConnection socket) { try { if (SocketCookies.Any(d => d.Key == ".ASPXAUTH")) { var userData = System.Web.Security.FormsAuthentication.Decrypt(SocketCookies[".ASPXAUTH"]); if (!userData.Expired) { QuizMessage msg = new QuizMessage() { Type = "SetName", Message = userData.Name }; string setUser = new JavaScriptSerializer().Serialize(msg); socket.Send(setUser); using (UnitOfWork uow = new UnitOfWork()) { var userProfile = uow.uc.UserProfiles.FirstOrDefault(d => d.UserName == userData.Name); if (userProfile != null) { var gameProfile = uow.qg.UserGameProfiles.FirstOrDefault(d => d.AccountModelUserId == userProfile.UserId); if (gameProfile == null) { UserGameProfile newProfile = new UserGameProfile() { AccountModelUserId = userProfile.UserId, Attempts = 0, CorrectAnswers = 0, LastTimeSeen = DateTime.Now, Points = 0 }; uow.qg.UserGameProfiles.Add(newProfile); uow.qg.SaveChanges(); gameProfile = newProfile; } else { gameProfile.LastTimeSeen = DateTime.Now; } uow.qg.SaveChanges(); } } QuizGame.Instance.UserJoin(socket.ConnectionInfo.Id, userData.Name); } } else { Random random = new Random(); int userid = random.Next(0, 1000); while (QuizGame.Instance.Users.Any(d => d.UserName == "Guest" + userid.ToString())) userid = random.Next(0, 1000); string username = "******" + userid.ToString(); QuizMessage msg = new QuizMessage() { Type = "SetName", Message = username }; string setUser = new JavaScriptSerializer().Serialize(msg); socket.Send(setUser); QuizGame.Instance.UserJoin(socket.ConnectionInfo.Id, username); } } catch (Exception ex) { logger.DebugException("AuthenticateUser", ex); } }
private void AddMessage(string message, string FromUser, string ToUser) { var quizmessage = new QuizMessage() { Message = message, From = FromUser, To = ToUser, Type = "Message" }; AddMessage(quizmessage); }
private void AddMessage(string message) { var quizmessage = new QuizMessage() { Message = message, To = "All", From = "Judge", Type = "Message" }; AddMessage(quizmessage); }
public void UserQuit(Guid id) { if (users.Any(d => d.SocketId == id)) { users.Remove(users.First(d => d.SocketId == id)); QuizMessage userList = new QuizMessage() { Type = "SetUsers", Users = users.Select(d => d.UserName).OrderBy(d => d).ToArray() }; AddMessage(userList); } }
public void UserJoin(Guid id, string username) { if (!users.Any(d => d.SocketId == id)) { if (!users.Any(d => d.UserName == username)) AddMessage("<span style='color:#a64b3d'>A new player has joined. Welcome </span>" + username); else users.Remove(users.First(d => d.UserName == username)); QuizProfile newProfile = new QuizProfile() { SocketId = id, UserName = username, Answered = 0, Attempts = 0, ThisGamePoints = 0 }; var userProfile = uow.uc.UserProfiles.FirstOrDefault(d => d.UserName == username); if (userProfile != null) { var gameProfile = uow.qg.UserGameProfiles.FirstOrDefault(d => d.AccountModelUserId == userProfile.UserId); if (gameProfile != null) newProfile.GameProfileId = gameProfile.Id; else newProfile.GameProfileId = 0; } else newProfile.GameProfileId = 0; users.Add(newProfile); QuizMessage userList = new QuizMessage() { Type = "SetUsers", Users = users.Select(d => d.UserName).OrderBy(d => d).ToArray() }; AddMessage(userList); } }
public void SendPoints(string user) { if (users.Any(d => d.UserName == user)) { var userProfile = users.First(d => d.UserName == user); QuizMessage message = new QuizMessage() { SocketId = userProfile.SocketId, Type = "SetStats", Answered = userProfile.Answered, Attempts = userProfile.Attempts, Points = userProfile.ThisGamePoints, Leader = CurrentLeader, TopPoints = TopPoints }; AddMessage(message); } }