-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Codable messages #62
Comments
@lmcd Take a look at https://github.com/MihaelIsaev/AwesomeWS |
This looks great, but I think I'd rather have support built right in as is in other parts of Vapor. |
I agree, this would be a nice feature to have. Do you have any ideas on what a good API for that might look like @lmcd? |
Something like (pseudocode): extension WebSocket {
func send(_ data: T, encoder: JSONEncoder = JSONEncoder()) where T: Codable {
guard let data = try? encoder.encode(data), let dataString = String(data: data, encoding: .utf8) else {
return
}
send(dataString)
}
} |
I'm interested in this bit of functionality, @tanner0101 is this something you are open to accepting contributions on? Do we need to nail down an API first? |
Yeah any ideas you have for API would be appreciated. |
I've been trying my hand at this, and I have an implementation that works for encoding a message when sending (can make a PR if this in itself is valuable enough), but am finding it much harder to implement some kind of automatic decoding. I'd imagine the API within Vapor to be something like: app.webSocket("socket") { req, ws in
ws.onMessage(Todo.self) { ws, todo in
todo.isCompleted.toggle()
ws.send(todo)
}
// fallback
ws.onText { ws, text in
let todo = Todo(name: text, completed: false)
ws.send(todo)
}
} Where you could then register multiple More importantly, I haven't been able to figure out a way to keep a collection of types and callbacks without losing type information. |
We'd probably need to use a serialization format which includes a type identifier, like: {
"type": "todo",
"data": { "completed": false, ... }
} Then that could automatically lookup the |
And now it is the same as |
Would be great to be able to specify a generic
Message
type onclient.connect
, that would auto-encode/decode ifCodable
conformance is present. This type could default toString
.onText
could also be renamedonMessage
.Just a thought.
The text was updated successfully, but these errors were encountered: