/// <summary> /// Handler for incoming <see cref="AuthResponsePacket"/>s. /// </summary> /// <param name="connectionId">Original connection ID</param> /// <param name="packet">Incoming <see cref="AuthResponsePacket"/></param> private void authResponsePacketHandler(string connectionId, AuthResponsePacket packet) { // Was authentication successful? if (packet.Success) { // Set authenticated state, session ID, and expiry Authenticated = true; SessionId = packet.SessionId; sessionExpiry = packet.Expiry.ToDateTime(); // Reset the session extension timer for halfway between now and the expiry sessionExtendTimer.Stop(); sessionExtendTimer.Interval = (sessionExpiry - DateTime.UtcNow).TotalMilliseconds / 2; sessionExtendTimer.Start(); // Raise successful auth event OnAuthenticationSuccess?.Invoke(this, new AuthenticationEventArgs()); } else { // Set authenticated state Authenticated = false; // Raise failed auth event OnAuthenticationFailure?.Invoke(this, new AuthenticationEventArgs(packet.FailureReason, packet.FailureMessage)); } }
/// <summary> /// Handler for incoming <see cref="AuthResponsePacket"/>s. /// </summary> /// <param name="connectionId">Original connection ID</param> /// <param name="packet">Incoming <see cref="AuthResponsePacket"/></param> private void authResponsePacketHandler(string connectionId, AuthResponsePacket packet) { // Was authentication successful? if (packet.Success) { // Set authenticated state and session ID Authenticated = true; SessionId = packet.SessionId; double timerInterval; if (packet.Expiry != null) { // COMPAT: Maintains backward-compatibility for 0.6.0 servers // This requires that the clock be properly synchronised or the interval may be calculated as negative timerInterval = (packet.Expiry.ToDateTime() - DateTime.UtcNow).TotalMilliseconds / 2; if (timerInterval < 0) { RaiseLogEntry(new LogEventArgs("Got a negative interval until session expiry. Check your system's clock is set correctly and try connecting again.", LogLevel.ERROR)); } } else { timerInterval = packet.ExpiresIn / 2; } // Reset the session extension timer for halfway between now and the expiry sessionExtendTimer.Stop(); sessionExtendTimer.Interval = timerInterval; sessionExtendTimer.Start(); // Raise successful auth event OnAuthenticationSuccess?.Invoke(this, new AuthenticationEventArgs()); } else { // Set authenticated state Authenticated = false; // Raise failed auth event OnAuthenticationFailure?.Invoke(this, new AuthenticationEventArgs(packet.FailureReason, packet.FailureMessage)); } }
private void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { OnReceivedRawMessage?.Invoke(client, e.Message); // there's a number at the start of every message, figure out what it is, and remove it var raw = e.Message; if (raw.Contains("\"")) { var number = e.Message.Split('"')[0].Substring(0, e.Message.Split('"')[0].Length - 1); raw = e.Message.Substring(number.Length); } if (e.Message.StartsWith("40")) { handleAuthentication(); return; } if (e.Message.StartsWith("0{\"sid\"")) { handlePingInitialization(Parsing.Internal.handleSessionMetadata(JObject.Parse(raw))); } if (e.Message.StartsWith("42[\"authenticated\"")) { OnAuthenticated?.Invoke(client, Parsing.Internal.handleAuthenticated(JArray.Parse(raw))); return; } if (e.Message.StartsWith("42[\"unauthorized\"")) { OnAuthenticationFailure?.Invoke(client, null); } if (e.Message.StartsWith("42[\"event\",{\"_id\"")) { handleComplexObject(JArray.Parse(raw)); return; } if (e.Message.StartsWith("42[\"event:update\",{\"name\"")) { handleSimpleUpdate(JArray.Parse(raw)); return; } }