bevy_eventwork icon indicating copy to clipboard operation
bevy_eventwork copied to clipboard

Custom Serialization

Open NoahShomette opened this issue 1 year ago • 6 comments

fixes #15 .

Adds custom serialization which allows users to supply custom serialization functions for messages to utilize different serialization formats with minimal change to current user ergonomics.

Changes

  • Network is now a system param, Network was changed to NetworkInstance. Users need to interact with the network through the Network system param now.
  • Sending messages now utilizes observers to enable different serialization functions for each message type.
  • recv_loop and send_loop now take a hashmap of serialization functions to enable serialization and deserialization of messages according to different settings.
  • messages must be registered on BOTH the sender and the receiver
  • listen_for_message is now register_message
  • listen_for_request_message is now register_receive_request_message
  • listen_for_response_message is now register_send_request_message

Custom Serialization

See BEMW JSON for an example. Recommend making a trait to help with registration. You need a generic serialization and deserialization function each and then one each for serializing and deserializing network packets. See BEMW for an example of how it should be implemented.

Limitations

  • Crate only supports sending Text or Binary over the network.
  • Mixing different serialization methods works rn on native but between wasm and native is intermittently working (Might be more of a BEMW issue but its weird enough that it might be on eventworks side messing up registrations for messages or something).

Todo

  • [ ] Cleanup
  • [ ] Add docs to serialization
  • [ ] Make sure Readme and stuff is updated to new format
  • [ ] Fix different serialization methods not working on Wasm
  • [ ] I accidentally made this branch off an older 0.13 branch :( (I need to update everything so it matches main or rebase it. Probably override main since I'm allergic to rebasing.)

NoahShomette avatar Oct 09 '24 02:10 NoahShomette

BEMW branch: https://github.com/NoahShomette/bevy_eventwork_mod_websockets/tree/serialization

@davi4046 If you are still using this and the custom serialization can you test if this works for you?

@mraof I saw you forked the custom serialization repos also so can you test this and see if it works for you as well?

NoahShomette avatar Oct 09 '24 02:10 NoahShomette

I will try to have a look at it tommorow👍

davi4046 avatar Oct 09 '24 16:10 davi4046

I have two errors when attempting to make this work.

Firstly, bevy::tasks::TaskPool is said to not implement bevy_eventwork:Runtime.

example-1

Secondly, the registration functions are not available on App in the example you provided. I guess it's because at the point of implementation, the app has not had the necessary plugins added yet.

example-2

Let me know if I'm doing anything wrong and I'll try again.

davi4046 avatar Oct 11 '24 14:10 davi4046

Just to make sure are you on bevy 0.14? I forgot you were working from 0.13 so that might be a pain for you to test sorry :(

I have two errors when attempting to make this work.

Firstly, bevy::tasks::TaskPool is said to not implement bevy_eventwork:Runtime.

example-1

Secondly, the registration functions are not available on App in the example you provided. I guess it's because at the point of implementation, the app has not had the necessary plugins added yet.

example-2

Let me know if I'm doing anything wrong and I'll try again.

NoahShomette avatar Oct 11 '24 16:10 NoahShomette

Ah, I totally forgot about the bevy version. Compilation errors are gone now, thanks!

Unfortunately I get bevy_eventwork_mod_websockets::native_websocket: Failed to decode network packet. No deserialization fn found when sending a message to the server.

Server to client seems to work fine, although I had to change how the client handles received messages.

I might still be doing something wrong, sorry. You can have a look at the repo: https://github.com/davi4046/bevy_eventwork_websocket_json_demo

davi4046 avatar Oct 11 '24 19:10 davi4046

Thanks for the link to the repo that is very helpful! It's unfortunately running into some issues that I couldn't resolve over the weekend so theres definitely a good amount of more work to fix this pr up

NoahShomette avatar Oct 15 '24 18:10 NoahShomette