/// <summary> /// Handles Messages sent from the client /// </summary> /// <param name="response">the message from the client</param> public virtual void HandleResponse(NotificationResponse response) { try { if (TraceMessagesClient && response.NotificationType != NotificationType.KeepAlive && response.NotificationType != NotificationType.Acknowledge) { Logger.LogTrace("WebSocket: Server << Client: " + NotificationResponse.LogNotification(response)); } if (response.IsMessage || (response.NotificationType == NotificationType.KeepAlive && NotifyForKeepalives)) { OnNotification(response); } } catch (Exception e) { if (TraceMessagesClient) { try { response.Reply(Notification.Invalid()); } catch (Exception) { return; } } Logger.LogError("Invalid WebSocket Response from Client\n" + e.SafeToString()); } }
/// <summary> /// Parses the given notificationResponse to string to be used for logging purposes /// </summary> /// <param name="response">the notificationResponse</param> /// <returns>the notificationResponse as string</returns> public static string LogNotification(NotificationResponse response) { string ret = response.Message; if (response._values.Count > 2) { foreach (var value in response._values) { if (value.Key != "ssid" && value.Key != "type") { ret += $"\n\t'{value.Key}' : '{value.Value}'"; } } } return(ret); }
/// <summary> /// Parses a Notification response from string /// </summary> /// <param name="input">the response string</param> /// <param name="response">the current response</param> /// <param name="URL">the url of the request</param> public static void ParseNotificationResponse(string input, NotificationResponse response, string URL) { if (input.Length <= 0) { return; } JsonNotificationPacket packet; try { packet = new JsonNotificationPacket(input); } catch (Exception) { if (input.Contains(NotificationType.Invalid.ToString())) { return; } response.Reply(Notification.Invalid()); return; } response._values = packet.Values; response.NotificationType = packet.NotificationType; response._noreply = packet.noreply; string ssid = packet.SSID; response.SessionData = new SessionIdentificatorSlim(URL, ssid); switch (response.NotificationType) { case NotificationType.Message: response.IsMessage = true; packet.Values.TryGetValue("msg", out response.Message); break; default: response.IsMessage = false; break; } }