-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace websocket communication with IPC/evaluate_script
- Loading branch information
Showing
11 changed files
with
121 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
# About | ||
|
||
'bevy_wry' is a [bevy](https://github.com/bevyengine/bevy/) plugin that provides integration with [wry](https://github.com/tauri-apps/wry) - cross platform webview rendering library written in rust. | ||
'bevy_wry' is a [bevy](https://github.com/bevyengine/bevy/) plugin that provides integration with [wry](https://github.com/tauri-apps/wry) - a cross platform webview rendering library written in rust. | ||
|
||
'bevy_wry' enables [bevy::Event](https://docs.rs/bevy/latest/bevy/ecs/event/trait.Event.html) based communication with WebView through [websocket](https://github.com/snapview/tungstenite-rs/). | ||
BevyWry allows for [bevy::Event](https://docs.rs/bevy/latest/bevy/ecs/event/trait.Event.html) based communication with WebView: | ||
- Out events are required to implement [OutWryEvent]. This allows for [wry::WebView::evaluate_script](https://docs.rs/wry/latest/wry/struct.WebView.html#method.evaluate_script) communication with WebView | ||
- Incoming events are received via IPC channel registered with [wry::WebViewBuilder::with_ipc_handler](https://docs.rs/wry/latest/wry/struct.WebViewBuilder.html#method.with_ipc_handler) | ||
|
||
It is still in very early stages, however I think it is good enough for some experimentation. | ||
|
||
Each client is simply reading/writing to websocket in a thread through [`MessageBus`](https://github.com/PawelBis/bevy_wry/blob/main/src/communication.rs#L62). The 'websocket.read()' call is non blocking - current version is relying on [`TcpStream::set_non_clocking(true)`](https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.set_nonblocking), however this will be improved in the future, as current implementation is quite expensive. | ||
|
||
You can read events incoming from websocket with [`EventReader<InEvent<T>>`](https://docs.rs/bevy/latest/bevy/ecs/event/struct.EventReader.html) and write events with [`EventWriter<OutEvent<T>>`](https://docs.rs/bevy/latest/bevy/ecs/event/struct.EventWriter.html). | ||
This plugin is still in very early stages, but it should be good enough for somne experimental work. | ||
|
||
# Example | ||
|
||
Check the [simple](https://github.com/PawelBis/bevy_wry/blob/main/examples/simple.rs) example for a quick reference. | ||
`cargo run --example simple --features="bincode bevy/bevy_core_pipeline bevy/bevy_render bevy/bevy_sprite"` | ||
|
||
# Features | ||
- `bincode` - default bincode `SerializeMessage` and `DeserializeMessage` for types that implement/derive `serde::Serialize` and `serde::Deserialize`. | ||
`cargo run --example simple --features="simple-example"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
#[derive(Debug)] | ||
pub enum Error { | ||
#[cfg(feature = "bincode")] | ||
Bincode(bincode::Error), | ||
Deserialize, | ||
BadMessageType, | ||
CloseRequested, | ||
EvaluateScript, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
pub mod types; | ||
|
||
#[cfg(feature = "bincode")] | ||
pub mod bincode; | ||
|
||
pub mod error; | ||
use bevy::prelude::{Event, EventReader, EventWriter, NonSend, ResMut}; | ||
pub use error::Error; | ||
use wry::WebView; | ||
|
||
use self::types::{MessageBus, OutWryEvent}; | ||
|
||
pub fn consume_in_events<T: Event>(message_bus: ResMut<MessageBus<T>>, mut events: EventWriter<T>) { | ||
let messages = { message_bus.lock().split_off(0) }; | ||
for msg in messages { | ||
events.send(msg); | ||
} | ||
} | ||
|
||
pub fn send_out_events<T: Event + OutWryEvent>( | ||
webview: NonSend<WebView>, | ||
mut events: EventReader<T>, | ||
) -> Result<(), Error> { | ||
for event in events.read() { | ||
webview | ||
.evaluate_script(&event.to_script()) | ||
.map_err(|_| Error::EvaluateScript)?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.