Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huoyijie/rust-socketio
Adds a layer of abstraction over udpack to provide secure and frame-based data transfer
https://github.com/huoyijie/rust-socketio
tcp udp udpack
Last synced: 4 days ago
JSON representation
Adds a layer of abstraction over udpack to provide secure and frame-based data transfer
- Host: GitHub
- URL: https://github.com/huoyijie/rust-socketio
- Owner: huoyijie
- License: mit
- Created: 2022-04-26T07:19:32.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-12-02T06:51:51.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T22:37:41.036Z (3 months ago)
- Topics: tcp, udp, udpack
- Language: Rust
- Homepage: https://huoyijie.cn/article/399d47e0717311ed8812b9a35e7c2efa/
- Size: 2.46 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-socketio
Adds a layer of abstraction over Udpack to provide secure and frame-based data transfer.## Examples
### server.rs
```rust
use rust_socketio::SocketIO;
use std::io;const SECRET_KEY: &[u8; 32] = b"ade2ff15798d44959d2846974bbf0bb3";
const SECRET_IV: &[u8; 16] = b"bd3c01bfb8c2edca";#[tokio::main]
async fn main() -> io::Result<()> {
let mut io = SocketIO::new("0.0.0.0:8080", *SECRET_KEY, *SECRET_IV).await?;
loop {
tokio::select! {
res = io.accept() => {
let mut socket = res.unwrap();
tokio::spawn(async move {
loop {
match socket.read().await {
Ok(Some(bytes)) => {
if let Err(e) = socket.write(bytes).await {
eprintln!("socket.write failed; err = {:?}", e);
}
}
Ok(None) => return,
Err(e) => {
eprintln!("socket.read failed; err = {:?}", e);
return;
}
};
}
});
}
_ = tokio::signal::ctrl_c() => {
println!("ctrl-c received!");
io.shutdown().await?;
break;
}
}
}
Ok(())
}
```### client.rs
```rust
use bytes::Bytes;
use rust_socketio::SocketIO;
use std::io;
use tokio::time;
use tokio::time::Duration;const SECRET_KEY: &[u8; 32] = b"ade2ff15798d44959d2846974bbf0bb3";
const SECRET_IV: &[u8; 16] = b"bd3c01bfb8c2edca";#[tokio::main]
async fn main() -> io::Result<()> {
let io: SocketIO = SocketIO::new("0.0.0.0:0", *SECRET_KEY, *SECRET_IV).await?;
let mut socket = io.connect("127.0.0.1:8080").await?;
let mut interval = time::interval(Duration::from_secs(3));
loop {
tokio::select! {
res = socket.read() => {
if let Some(bytes) = res? {
eprintln!("received {:?}", bytes);
} else {
println!("EOF");
io.shutdown().await?;
break;
}
}
_ = interval.tick() => {
socket.write(Bytes::copy_from_slice(&[1u8; 2048])).await?;
}
_ = tokio::signal::ctrl_c() => {
println!("ctrl-c received!");
io.shutdown().await?;
break;
}
}
}
Ok(())
}
```