Add suport for WebSocket RPC binary protocol
This commit is contained in:
parent
ab4c85bf5d
commit
a074dc9af5
4 changed files with 20 additions and 4 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -3748,9 +3748,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "surrealdb-derive"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "010e7d81b2621bffdbb573cc82de8f6c243c5036017106e2edffae8f95dba48c"
|
||||
checksum = "552bb4f9eb49f493b14d79d989ea9ecb53ee8cee0aad529e289faca72fb78b14"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
|
|
|
@ -42,7 +42,7 @@ async-recursion = "1.0.0"
|
|||
bigdecimal = { version = "0.3.0", features = ["serde", "string-only"] }
|
||||
channel = { version = "1.7.1", package = "async-channel" }
|
||||
chrono = { version = "0.4.22", features = ["serde"] }
|
||||
derive = { version = "0.4.0", package = "surrealdb-derive" }
|
||||
derive = { version = "0.5.0", package = "surrealdb-derive" }
|
||||
deunicode = "1.3.2"
|
||||
dmp = "0.1.1"
|
||||
echodb = { version = "0.3.0", optional = true }
|
||||
|
|
|
@ -114,6 +114,9 @@ impl Rpc {
|
|||
msg if msg.is_text() => {
|
||||
tokio::task::spawn(Rpc::call(rpc.clone(), msg, chn.clone()));
|
||||
}
|
||||
msg if msg.is_binary() => {
|
||||
tokio::task::spawn(Rpc::call(rpc.clone(), msg, chn.clone()));
|
||||
}
|
||||
msg if msg.is_close() => {
|
||||
break;
|
||||
}
|
||||
|
@ -138,11 +141,18 @@ impl Rpc {
|
|||
// Call RPC methods from the WebSocket
|
||||
async fn call(rpc: Arc<RwLock<Rpc>>, msg: Message, chn: Sender<Message>) {
|
||||
// Get the current output format
|
||||
let out = { rpc.read().await.format.clone() };
|
||||
let mut out = { rpc.read().await.format.clone() };
|
||||
// Clone the RPC
|
||||
let rpc = rpc.clone();
|
||||
// Parse the request
|
||||
let req = match msg {
|
||||
// This is a binary message
|
||||
m if m.is_binary() => {
|
||||
// Use binary output
|
||||
out = Output::Full;
|
||||
// Deserialize the input
|
||||
Value::from(m.into_bytes())
|
||||
}
|
||||
// This is a text message
|
||||
m if m.is_text() => {
|
||||
// This won't panic due to the check above
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use serde::Serialize;
|
||||
use std::borrow::Cow;
|
||||
use surrealdb::channel::Sender;
|
||||
use surrealdb::sql::serde::serialize_internal;
|
||||
use surrealdb::sql::Value;
|
||||
use warp::ws::Message;
|
||||
|
||||
|
@ -47,6 +48,11 @@ impl<T: Serialize> Response<T> {
|
|||
let res = Message::binary(res);
|
||||
let _ = chn.send(res).await;
|
||||
}
|
||||
Output::Full => {
|
||||
let res = serialize_internal(|| serde_pack::to_vec(&self).unwrap());
|
||||
let res = Message::binary(res);
|
||||
let _ = chn.send(res).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue