Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile error #9

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:
jobs:
rustfmt:
name: rustfmt
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -33,7 +33,7 @@ jobs:

clippy:
name: clippy
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -45,14 +45,18 @@ jobs:
toolchain: stable
profile: minimal
components: clippy
- name: install protoc
uses: taiki-e/install-action@v2
with:
tool: protoc
- name: Clippy
uses: actions-rs/cargo@v1
with:
command: clippy

build-test-x86_64-unknown-linux-gnu:
name: build and test (x86_64-unknown-linux-gnu)
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -64,6 +68,10 @@ jobs:
toolchain: stable
profile: minimal
components: rustfmt
- name: install protoc
uses: taiki-e/install-action@v2
with:
tool: protoc
- name: Build
uses: actions-rs/cargo@v1
with:
Expand All @@ -76,11 +84,11 @@ jobs:
build-test-x86_64-apple-darwin:
name: build and test (x86_64-apple-darwin)
runs-on: macos-latest
env:
SELECT_XCODE: /Applications/Xcode_12.2.app
# env:
# SELECT_XCODE: /Applications/Xcode_12.2.app
steps:
- name: Select XCode version
run: sudo xcode-select -s "${SELECT_XCODE}"
# - name: Select XCode version
# run: sudo xcode-select -s "${SELECT_XCODE}"
- name: Checkout repository
uses: actions/checkout@v2
with:
Expand All @@ -90,6 +98,10 @@ jobs:
with:
toolchain: stable
profile: minimal
- name: install protoc
uses: taiki-e/install-action@v2
with:
tool: protoc
- name: Build
uses: actions-rs/cargo@v1
with:
Expand All @@ -101,7 +113,7 @@ jobs:

buuild-client-wasm32-unknown-unknown:
name: build client (wasm32-unknown-unknown)
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -113,7 +125,11 @@ jobs:
toolchain: stable
profile: minimal
components: rustfmt
- name: install protoc
uses: taiki-e/install-action@v2
with:
tool: protoc
- name: Install wasm-pack
uses: jetli/[email protected]
- name: Build client
run: cd examples/wasm-client && wasm-pack build --target web
run: cd examples/wasm-client && RUSTFLAGS=--cfg=tokio_unstable wasm-pack build --target web
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ python3 -m http.server
// open http://0.0.0.0:8000/ in browser
```

## Usage
You can currently use this as a git dependency. You will need to enable the `tokio_unstable` config flag for tonic to compile.
Update your `.cargo/config.toml`:
```toml
[build]
# This is necessary because tonic needs the tower `make` feature which in turn needs tokios io-std feature which doesn't
# compile on wasm unless tokio_unstable is active
rustflags = ["--cfg=tokio_unstable"]
```

## License

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "wasm-client"
version = "0.1.0"
authors = ["boxdot <[email protected]>"]
edition = "2018"
edition = "2021"
publish = false

[lib]
Expand Down
9 changes: 7 additions & 2 deletions transport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Debug;
use std::future::Future;
use std::io;
use std::pin::Pin;
#[cfg(feature = "native")]
use std::sync::Arc;
use std::task::{Context, Poll};

Expand Down Expand Up @@ -50,6 +51,7 @@ impl From<wasm_bindgen::JsValue> for Error {

#[derive(Clone, Default)]
pub struct WsConnector {
#[cfg(feature = "native")]
resolve_bearer_token: Option<Arc<dyn Fn() -> String + Sync + Send + 'static>>,
}

Expand All @@ -64,6 +66,7 @@ impl WsConnector {
Default::default()
}

#[cfg(feature = "native")]
pub fn with_bearer_resolver(
resolve_token: impl Fn() -> String + Send + Sync + 'static,
) -> Self {
Expand Down Expand Up @@ -91,10 +94,12 @@ impl WsConnector {
let mut request = dst.into_client_request()?;
if let Some(resolver) = self.resolve_bearer_token.as_ref() {
let token = resolver();
request.headers_mut().typed_insert(Authorization::bearer(&token)?);
request
.headers_mut()
.typed_insert(Authorization::bearer(&token)?);
}

let (ws_stream, _) = tokio_tungstenite::connect_async(dst).await?;
let (ws_stream, _) = tokio_tungstenite::connect_async(request).await?;
Ok(WsConnection::from_combined_channel(ws_stream))
}
}
Expand Down
2 changes: 1 addition & 1 deletion transport/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl WsConnection {
io::ErrorKind::ConnectionAborted,
TungsteniteError::ConnectionClosed,
))),
Ok(Message::Frame(_)) => {None}
Ok(Message::Frame(_)) => None,
Err(e) => Some(Err(io::Error::new(io::ErrorKind::Other, e))),
})
});
Expand Down
18 changes: 11 additions & 7 deletions transport/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use web_sys::{MessageEvent, WebSocket};

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::rc::Rc;
use std::task::{Context, Poll};

#[cfg(not(feature = "native"))]
pub async fn connect(dst: http::Uri) -> Result<super::WsConnection, Error> {
use futures_util::{future, stream::TryStreamExt, SinkExt};

let ws = Ws(Arc::new(WebSocket::new(&dst.to_string())?));
let ws = Ws(Rc::new(WebSocket::new(&dst.to_string())?));
(*ws).set_binary_type(web_sys::BinaryType::Arraybuffer);
let client = WebConnection { ws, wake_fn: None }.await?;

Expand Down Expand Up @@ -47,7 +47,7 @@ pub async fn connect(dst: http::Uri) -> Result<super::WsConnection, Error> {
}

#[derive(Debug, Clone)]
struct Ws(Arc<WebSocket>);
struct Ws(Rc<WebSocket>);

unsafe impl Send for Ws {}

Expand Down Expand Up @@ -127,9 +127,12 @@ impl Drop for WakeFn {

#[derive(Debug)]
pub struct WebClient {
#[allow(dead_code)]
ws: Ws,
#[allow(dead_code)]
rx: UnboundedReceiver<Result<Vec<u8>, Error>>,
handlers: Arc<Handlers>, // keeps the callbacks alive
#[allow(dead_code)]
handlers: Rc<Handlers>, // keeps the callbacks alive
}

impl WebClient {
Expand All @@ -147,7 +150,7 @@ impl WebClient {
}
}) as Box<dyn FnOnce()>);

let handlers = Arc::new(Handlers::register(
let handlers = Rc::new(Handlers::register(
ws.clone(),
Some(message_fn),
Some(close_fn),
Expand Down Expand Up @@ -203,13 +206,14 @@ impl Drop for Handlers {
#[derive(Debug)]
struct WebClientSink {
ws: Ws,
handlers: Arc<Handlers>, // keeps the callbacks alive
#[allow(dead_code)]
handlers: Rc<Handlers>, // keeps the callbacks alive
}

#[pin_project]
struct WebClientStream {
ws: Ws,
handlers: Arc<Handlers>, // keeps the callbacks alive
handlers: Rc<Handlers>, // keeps the callbacks alive
#[pin]
rx: UnboundedReceiver<Result<Vec<u8>, Error>>,
}
Expand Down
Loading