13个不稳定版本 (5个破坏性更新)

0.6.0 2024年7月21日
0.5.0 2023年12月6日
0.4.0 2023年8月11日
0.3.1 2023年7月24日
0.1.0 2022年3月11日

#44 in WebAssembly

Download history 39453/week @ 2024-05-01 45503/week @ 2024-05-08 45408/week @ 2024-05-15 41488/week @ 2024-05-22 50519/week @ 2024-05-29 47900/week @ 2024-06-05 47065/week @ 2024-06-12 46497/week @ 2024-06-19 47661/week @ 2024-06-26 47947/week @ 2024-07-03 47844/week @ 2024-07-10 55055/week @ 2024-07-17 60467/week @ 2024-07-24 62994/week @ 2024-07-31 61278/week @ 2024-08-07 63744/week @ 2024-08-14

每月下载量258,667
337 个crate中使用 (直接使用39个)

MIT/Apache

84KB
1.5K SLoC

gloo-net

Crates.io version Download docs.rs docs

API文档 | 贡献 | 聊天

由 🦀🕸 构建,由 Rust和WebAssembly工作组 支持

WASM应用程序的HTTP请求库。它为 web_sys 中的 fetchWebSocketEventSource API提供了惯用的Rust绑定。

示例

HTTP

let resp = Request::get("/path")
    .send()
    .await
    .unwrap();
assert_eq!(resp.status(), 200);

WebSocket

use gloo_net::websocket::{Message, futures::WebSocket};
use wasm_bindgen_futures::spawn_local;
use futures::{SinkExt, StreamExt};

let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap();
let (mut write, mut read) = ws.split();

spawn_local(async move {
    write.send(Message::Text(String::from("test"))).await.unwrap();
    write.send(Message::Text(String::from("test 2"))).await.unwrap();
});

spawn_local(async move {
    while let Some(msg) = read.next().await {
        console_log!(format!("1. {:?}", msg))
    }
    console_log!("WebSocket Closed")
})

EventSource

use gloo_net::eventsource::futures::EventSource;
use wasm_bindgen_futures::spawn_local;
use futures::{stream, StreamExt};

let mut es = EventSource::new("http://api.example.com/ssedemo.php").unwrap();
let stream_1 = es.subscribe("some-event-type").unwrap();
let stream_2 = es.subscribe("another-event-type").unwrap();

spawn_local(async move {
    let mut all_streams = stream::select(stream_1, stream_2);
    while let Some(Ok((event_type, msg))) = all_streams.next().await {
        console_log!(format!("1. {}: {:?}", event_type, msg))
    }
    console_log!("EventSource Closed");
})

依赖项

~8–10MB
~192K SLoC