Closed aarons2222 closed 1 year ago
Please provide logs and show the JSON you are trying to process. The error message indicates that the type
property is missing but possibly it shows up because it is just the first property in the struct.
Thanks for getting back to me so promptly.
{
"type": 1,
"target": "SendMessage",
"arguments": [
{
"_id": {
"timestamp": 1673433892,
"machine": 13501675,
"pid": 27108,
"increment": 11139807,
"creationTime": "2023-01-11T10:44:52Z"
},
"pin": 888888,
"pan": "07447000599",
"body": {
"text": "Hi there",
"fromInside": true,
"timestamp": "2023-01-11T10:44:52.321681Z",
"messageId": "b0ae4cdb-5094-4315-847f-466eb8d8367c",
"status": 100,
"read": false
}
}
]
}
This is the payload the client receives and not the payload you get. You should not try deserialize it. You should only worry about arguments your server is sending. You can take a look at the tests. The server defines a struct:
https://github.com/moozzyk/SignalR-Client-Swift/blob/0e7a67e2a41e3c2f769406d344ef1f72720ed98f/Examples/TestServer/Person.cs#L5-L12 and sends it to the client:
and the client only defines a counterpart struct: https://github.com/moozzyk/SignalR-Client-Swift/blob/0e7a67e2a41e3c2f769406d344ef1f72720ed98f/Tests/SignalRClientTests/HubConnectionTests.swift#L809-L815
In other words you only need to take care of what is in the Arguments
array from the payload.
Also, you don't need to use argumentExtractor (even though the test does). This is a lower level API. You should be able to get away with this (I am using the name Argument here because this is how your struct is named - you would probably want to rename your struct to something more meaningful):
self.chatHubConnection!.on(method: "SendMessage", callback: { (arg: Argument) in
print(arg.pin)
// ...
})
Thank you, that makes sense.
Always getting a decoding error as values are empty, despite receiving message as printed in debug lo g. Am I misunderstanding the use of this?
Struct
//ERROR
keyNotFound(CodingKeys(stringValue: "type", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"type\", intValue: nil) (\"type\").", underlyingError: nil))