feat: Make Buttplug work with WASM

We don't need the connectors/webbluetooth directly in Buttplug, since
it'll be compiled as its own thing. But will need to make the library
work with WASM so that will compile.
This commit is contained in:
Kyle Machulis 2023-09-01 18:04:58 -07:00
parent 155338218a
commit 9f54f0cf52
5 changed files with 38 additions and 8 deletions

View file

@ -2,7 +2,7 @@
resolver = "2"
members = [
"buttplug",
"buttplug_derive",
"buttplug_derive"
]
[profile.release]

View file

@ -17,6 +17,7 @@ path = "src/lib.rs"
test = true
doctest = true
doc = true
crate-type = ["cdylib", "rlib"]
# Only build docs on one platform (linux)
[package.metadata.docs.rs]
@ -26,7 +27,7 @@ features = ["default", "unstable"]
[features]
# Basic features
default=["tokio-runtime", "client", "server", "serialize-json", "websockets", "btleplug-manager", "xinput-manager", "serial-manager", "lovense-dongle-manager", "lovense-connect-service-manager", "websocket-server-manager"]
default=["tokio-runtime", "jsonschema/resolve-file", "client", "server", "serialize-json", "websockets", "btleplug-manager", "xinput-manager", "serial-manager", "lovense-dongle-manager", "lovense-connect-service-manager", "websocket-server-manager"]
client=[]
server=[]
serialize-json=[]
@ -40,8 +41,9 @@ lovense-dongle-manager=["server", "serialport", "hidapi"]
lovense-connect-service-manager=["server","reqwest"]
websocket-server-manager=["server", "websockets"]
# Runtime managers
tokio-runtime=["tokio/rt-multi-thread", "async-tungstenite/tokio-runtime", "async-tungstenite/tokio-native-tls"]
tokio-runtime=["tokio/rt", "async-tungstenite/tokio-runtime", "async-tungstenite/tokio-native-tls"]
wasm-bindgen-runtime=["wasm-bindgen", "wasm-bindgen-futures"]
wasm = ["server", "wasm-bindgen-runtime", "serialize-json", "wasm-bindgen", "uuid/wasm-bindgen", "wasmtimer"]
dummy-runtime=[]
# Compiler config
unstable=[]
@ -76,7 +78,7 @@ tracing-futures = "0.2.5"
tracing-subscriber = { version = "0.3.17", features = ["json"] }
dashmap = "5.4.0"
displaydoc = "0.2.4"
wasm-bindgen = { version = "0.2.87", optional = true }
wasm-bindgen = { version = "0.2.87", features = ["serde-serialize"], optional = true }
tokio = { version = "1.29.1", features = ["sync", "macros", "io-util"] }
async-stream = "0.3.5"
prost = "0.11.9"
@ -85,7 +87,7 @@ reqwest = { version = "0.11.18", default-features = false, optional = true, feat
serde-aux = "4.2.0"
getset = "0.1.2"
os_info = "3.7.0"
jsonschema = { version = "0.17.1", default-features = false, features = ["resolve-file"] }
jsonschema = { version = "0.17.1", default-features = false }
derivative = "2.2.0"
tokio-stream = "0.1.14"
wasmtimer = { version = "0.2.0", optional = true }
@ -118,3 +120,29 @@ serialport = { version = "4.2.1", optional = true }
# Linux hidraw is needed here in order to work with the lovense dongle. libusb breaks it on linux.
# Other platforms are not affected by the feature changes.
hidapi = { version = "2.4.0", default-features = false, features = ["linux-static-hidraw", "illumos-static-libusb"], optional = true }
[dependencies.web-sys]
version = "0.3.64"
# path = "../../wasm-bindgen/crates/web-sys"
#git = "https://github.com/rustwasm/wasm-bindgen"
optional = true
features = [
"Navigator",
"Bluetooth",
"BluetoothDevice",
"BluetoothLeScanFilterInit",
"BluetoothRemoteGattCharacteristic",
"BluetoothRemoteGattServer",
"BluetoothRemoteGattService",
"BinaryType",
"Blob",
"console",
"ErrorEvent",
"Event",
"FileReader",
"MessageEvent",
"ProgressEvent",
"RequestDeviceOptions",
"WebSocket",
"Window"
]

View file

@ -63,6 +63,7 @@
//! There are slightly more useful situations like device forwarders where this work comes in also,
//! but that Windows 7/Android example is where the idea originally came from.
#[cfg(all(feature = "server", feature = "client", not(feature = "wasm")))]
mod in_process_connector;
pub mod remote_connector;
pub mod transport;
@ -73,7 +74,7 @@ use crate::{
};
use displaydoc::Display;
use futures::future::{self, BoxFuture, FutureExt};
#[cfg(all(feature = "server", feature = "client"))]
#[cfg(all(feature = "server", feature = "client", not(feature = "wasm")))]
pub use in_process_connector::{
ButtplugInProcessClientConnector,
ButtplugInProcessClientConnectorBuilder,

View file

@ -14,6 +14,7 @@ pub use json_serializer::{
ButtplugClientJSONSerializer,
ButtplugClientJSONSerializerImpl,
ButtplugServerJSONSerializer,
vec_to_protocol_json
};
use serde::{Deserialize, Serialize};

View file

@ -43,7 +43,7 @@ pub mod xinput;
use crate::{
core::{errors::ButtplugDeviceError, ButtplugResultFuture},
server::device::hardware::HardwareConnector,
util::async_manager,
util::{async_manager, sleep}
};
use async_trait::async_trait;
use futures::future::{self, FutureExt};
@ -158,7 +158,7 @@ impl<T: TimedRetryCommunicationManagerImpl> HardwareCommunicationManager
break;
}
tokio::select! {
_ = tokio::time::sleep(duration) => continue,
_ = sleep(duration) => continue,
_ = child_token.cancelled() => break,
}
}