update socket
This commit is contained in:
parent
1449e003de
commit
bc9f82b8fb
@ -340,17 +340,7 @@ final class SocketService {
|
||||
private func handleNewPrivateMessage(_ data: [Any]) {
|
||||
guard let payload = data.first else { return }
|
||||
|
||||
let messageData: Data
|
||||
if let dictionary = payload as? [String: Any],
|
||||
JSONSerialization.isValidJSONObject(dictionary),
|
||||
let json = try? JSONSerialization.data(withJSONObject: dictionary, options: []) {
|
||||
messageData = json
|
||||
} else if let string = payload as? String,
|
||||
let data = string.data(using: .utf8) {
|
||||
messageData = data
|
||||
} else {
|
||||
return
|
||||
}
|
||||
guard let messageData = normalizeMessagePayload(payload) else { return }
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||
@ -373,6 +363,31 @@ final class SocketService {
|
||||
}
|
||||
}
|
||||
|
||||
private func normalizeMessagePayload(_ payload: Any) -> Data? {
|
||||
// Server can wrap the actual message in an { event, payload } envelope.
|
||||
if let dictionary = payload as? [String: Any] {
|
||||
let messageBody = dictionary["payload"] ?? dictionary
|
||||
if let messageDict = messageBody as? [String: Any],
|
||||
JSONSerialization.isValidJSONObject(messageDict) {
|
||||
return try? JSONSerialization.data(withJSONObject: messageDict, options: [])
|
||||
}
|
||||
}
|
||||
|
||||
if let string = payload as? String,
|
||||
let data = string.data(using: .utf8) {
|
||||
if let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
|
||||
let nested = jsonObject["payload"] {
|
||||
return normalizeMessagePayload(nested)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
if let data = payload as? Data {
|
||||
return data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func handleHeartbeatSuccess() {
|
||||
consecutiveHeartbeatMisses = 0
|
||||
heartbeatAckInFlight = false
|
||||
@ -453,7 +468,3 @@ final class SocketService {
|
||||
extension Notification.Name {
|
||||
static let socketDidReceivePrivateMessage = Notification.Name("socketDidReceivePrivateMessage")
|
||||
}
|
||||
|
||||
|
||||
//[SocketService] Failed to decode new message: typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "messageId", intValue: nil)], debugDescription: "Expected to decode String or number for key messageId", underlyingError: nil))
|
||||
//[SocketService] payload={"event":"chat_private:new_message","payload":{"is_viewed":false,"content":"Ttyijfff","message_id":241,"message_type":["text"],"chat_id":"838146ed-1251-42df-b529-da7870101fa3","media_link":null,"created_at":"2025-11-27T23:10:09.039724+00:00","updated_at":null,"sender_data":{"full_name":"Системный Админ 1","is_verified":true,"is_system":false,"rating":{"status":"fine","rating":5},"custom_name":null,"login":"admin","created_at":"2025-10-20T18:19:04.911483Z","stories":[],"user_id":"7a319996-8e6a-4cc4-a808-091eda7cea6f","permissions":{"you_can_call_permission":true,"you_can_public_invite_permission":true,"you_can_send_message":true,"you_can_group_invite_permission":true},"bio":null,"profile_permissions":{"allow_messages_from_non_contacts":true,"max_message_auto_delete_seconds":null,"force_auto_delete_messages_in_private":false,"is_searchable":true,"allow_message_forwarding":true,"allow_server_chats":true},"last_seen":3300664,"relationship":{"is_current_user_in_contacts_of_target":false,"is_current_user_in_blacklist_of_target":false,"is_target_user_blocked_by_current_user":false}},"forward_metadata":null,"sender_id":"7a319996-8e6a-4cc4-a808-091eda7cea6f"}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user