Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access raw message data #215

Open
ream88 opened this issue Feb 6, 2022 · 2 comments
Open

Access raw message data #215

ream88 opened this issue Feb 6, 2022 · 2 comments

Comments

@ream88
Copy link

ream88 commented Feb 6, 2022

I'm currently rewriting an APP of ours from a classic HTTP-based API to using web sockets. We already have a ton of different models and decoders for that reason, and I would love to reuse as much code as possible. Therefore my question: Is there a way to get the raw payload from a Message without parsing it as JSON?

@dsrees
Copy link
Collaborator

dsrees commented Feb 7, 2022

You can implement your own decoder (see Defaults.swift) but the client is expecting that decoder will return [Any] which will be parsed into a Message. If you don't decode the Data into the correct format then you wont receive any messages to your channels

guard
      let data = rawMessage.data(using: String.Encoding.utf8),
      let json = decode(data) as? [Any?],
      let message = Message(json: json)
      else {
        self.logItems("receive: Unable to parse JSON: \(rawMessage)")
        return }

Do your models convert Data to MyModel and you're wanting to convert Message.payload into MyModel without using JSON serialization?

@ream88
Copy link
Author

ream88 commented Feb 7, 2022

Do your models convert Data to MyModel and you're wanting to convert Message.payload into MyModel without using JSON serialization?

Basically yes, I already have my decoding logic written, and it's accepting Data and converts it to MyModel. Serializing [Any] back to JSON to then convert it to MyModel just feels wrong, therefore I think some kind of low-level API to access the raw Data would be awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants