Skip to content

Commit

Permalink
feat: use bitcode in production mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBANCHEREAU committed Nov 5, 2023
1 parent cb93c18 commit d274894
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serde_json = "1.0.96"
serde = "1.0.171"
yew-websocket = "1.0.1"
futures-util = { version = "0.3.28", features = ["sink"] }
bitcode = { version = "0.5.0", features = ["serde"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
Expand Down
16 changes: 14 additions & 2 deletions client/src/utils/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::marker::PhantomData;
use gloo::{events::EventListener, utils::window};
use serde::{de::DeserializeOwned, Serialize};
use wasm_bindgen::JsCast;
use web_sys::{Event, MessageEvent, WebSocket};
use web_sys::{BinaryType, Event, MessageEvent, WebSocket};
use yew::Callback;

pub struct Socket<In: Serialize, Out: DeserializeOwned> {
Expand All @@ -19,11 +19,18 @@ impl<In: Serialize + 'static, Out: DeserializeOwned + 'static> Socket<In, Out> {
url.set_protocol(url.protocol().replace("http", "ws").as_str());
url.set_pathname(pathname);
let socket = WebSocket::new(url.href().as_str()).unwrap();
socket.set_binary_type(BinaryType::Arraybuffer);
let listener = EventListener::new(&socket, "message", move |event: &Event| {
let event = event.dyn_ref::<MessageEvent>().unwrap();
#[cfg(debug_assertions)]
callback.emit(
serde_json::from_str::<Out>(event.data().as_string().unwrap().as_str()).unwrap(),
)
);
#[cfg(not(debug_assertions))]
callback.emit(
bitcode::deserialize::<Out>(Uint8Array::new(&event.data()).to_vec().as_ref())
.unwrap(),
);
});

Self {
Expand All @@ -34,8 +41,13 @@ impl<In: Serialize + 'static, Out: DeserializeOwned + 'static> Socket<In, Out> {
}
}
pub fn send(&mut self, payload: In) {
#[cfg(debug_assertions)]
self.socket
.send_with_str(serde_json::to_string(&payload).unwrap().as_str())
.unwrap();
#[cfg(not(debug_assertions))]
self.socket
.send_with_u8_array(bitcode::serialize(&payload).unwrap().as_ref())
.unwrap();
}
}
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ actix = "0.13.0"
core = { path = "../core", features=["server"] }
model = { path = "../model" }
nanoid = "0.4.0"
bitcode = { version = "0.5.0", features = ["serde"] }
24 changes: 18 additions & 6 deletions server/src/lobby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,32 @@ impl Actor for WsActor {
impl Handler<GenericServerMessageWrapper> for WsActor {
type Result = ();
fn handle(&mut self, item: GenericServerMessageWrapper, ctx: &mut Self::Context) {
#[cfg(debug_assertions)]
ctx.text(serde_json::to_string(&item.0).unwrap());

#[cfg(not(debug_assertions))]
ctx.binary(bitcode::serialize(&item.0).unwrap());
}
}

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsActor {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, _: &mut Self::Context) {
// let Ok(msg) = msg else { return };
match msg.unwrap() {
ws::Message::Text(text) => self
.lobby
.lock()
.unwrap()
.handle_message(serde_json::from_str(text.to_string().as_str()).unwrap()),
ws::Message::Binary(_) => todo!(),
ws::Message::Text(text) => {
#[cfg(debug_assertions)]
self.lobby
.lock()
.unwrap()
.handle_message(serde_json::from_str(text.to_string().as_str()).unwrap());
}
ws::Message::Binary(binary) => {
#[cfg(not(debug_assertions))]
self.lobby
.lock()
.unwrap()
.handle_message(bitcode::deserialize(binary.as_ref()).unwrap());
}
ws::Message::Continuation(_) => todo!(),
ws::Message::Ping(_) => todo!(),
ws::Message::Pong(_) => todo!(),
Expand Down

0 comments on commit d274894

Please sign in to comment.