void Update() { try { // Get an instance of the SessionManager if one does not exist. if (sessionManager == null && SharingStage.Instance != null && SharingStage.Instance.Manager != null) { this.sessionManager = SharingStage.Instance.Manager.GetSessionManager(); } // If we are a Primary Client and can join sessions... if (this.sessionManager != null && sessionManager.GetSessionCount() > 0) { // Check to see if we aren't already in the desired session Session currentSession = this.sessionManager.GetCurrentSession(); if (currentSession == null || // We aren't in any session currentSession.GetName().GetString() != this.SessionName || // We're in the wrong session currentSession.GetMachineSessionState() == MachineSessionState.DISCONNECTED) // We aren't joined or joining the right session { Debug.Log("Session conn " + sessionManager.IsServerConnected() + " sessions: " + sessionManager.GetSessionCount()); Debug.Log("Looking for " + SessionName); bool sessionFound = false; for (int i = 0; i < this.sessionManager.GetSessionCount(); ++i) { Session s = this.sessionManager.GetSession(i); Debug.Log(string.Format("session {0}", s.GetName().GetString())); if (s.GetName().GetString() == this.SessionName) { s.Join(); sessionFound = true; break; } } if (sessionManager.IsServerConnected() && !sessionFound) { Debug.Log("Didn't find session, making a new one"); sessionManager.CreateSession(new XString(SessionName)); for (int i = 0; i < this.sessionManager.GetSessionCount(); ++i) { Session s = this.sessionManager.GetSession(i); if (s.GetName().GetString() == this.SessionName) { s.Join(); Debug.Log("Joining our new session"); sessionFound = true; break; } } } } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); } }
void Update() { // Get an instance of the SessionManager if one does not exist. if (sessionManager == null && SharingStage.Instance != null && SharingStage.Instance.Manager != null) { this.sessionManager = SharingStage.Instance.Manager.GetSessionManager(); } // Only poll every second. if (Time.frameCount % pollingFrequency == 0 && this.sessionManager != null && sessionManager.GetSessionCount() > 0) { Session currentSession = this.sessionManager.GetCurrentSession(); if (currentSession != null) { int userCount = currentSession.GetUserCount(); // If we have fewer users in the current session than are // tracked locally then one or more users have exited. // We need to figure out which ones. if (userCount < userIds.Count) { // Gather all of the new users into a new array. List <long> updatedUserIds = new List <long>(); for (int index = 0; index < userCount; index++) { User user = currentSession.GetUser(index); long userId = user.GetID(); updatedUserIds.Add(userId); Debug.Log(index + ": id: " + user.GetID() + " or: " + userId); // It's an edge case, but if a user joins and a user exits at the same // time, we need to handle that. if (userIds.Contains(userId) == false) { SendJoinEvent(user); } } // Now check to see which IDs are in the old userIds list, but not in the new updatedUserIds list. for (int index = 0; index < userIds.Count; index++) { if (updatedUserIds.Contains(userIds[index]) == false) { SendLeaveEvent(userIds[index]); } } userIds = updatedUserIds; } else // Same or more users in the session. { for (int index = 0; index < userCount; index++) { User user = currentSession.GetUser(index); long userId = user.GetID(); if (userIds.Contains(userId) == false) { userIds.Add(userId); SendJoinEvent(user); } } } } } }