1个稳定版本
新 2.0.0 | 2024年8月21日 |
---|
#478 在 异步
在 cbsk_tokio 中使用
89KB
1.5K SLoC
cbsk_socket是一个socket回调工具
您可以使用cbsk_socket创建TCP/WebSocket服务器或客户端,您无需关注TCP/WebSocket的读写,只需关注业务处理即可
最低支持的Rust版本
Rust 1.80.0
现在支持的套接字
- tcp客户端 √
- tcp服务器 √
- ws客户端 √
- ws服务器 √
tcp服务器示例
tcp服务器示例
Cargo.toml文件
fast_log = "1.7.3"
cbsk_base = "2.0.0"
cbsk_socket_tokio = { version = "2.0.0", default-features = false, features = ["tcp_server"] }
main.rs文件
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use cbsk_base::{log, tokio};
use cbsk_base::async_trait::async_trait;
use cbsk_base::log::LevelFilter;
use cbsk_socket_tokio::cbsk_socket::tcp::server::config::TcpServerConfig;
use cbsk_socket_tokio::tcp::common::tcp_write_trait::TcpWriteTrait;
use cbsk_socket_tokio::tcp::server::callback::TcpServerCallBack;
use cbsk_socket_tokio::tcp::server::client::TcpServerClient;
use cbsk_socket_tokio::tcp::server::TcpServer;
#[tokio::main]
pub async fn main() {
fast_log::init(fast_log::config::Config::default().level(LevelFilter::Info).console()).unwrap();
let addr = SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 8080);
let conf = TcpServerConfig::new("".into(), addr, false);
let tcp_server = TcpServer::new(conf.into(), Cb {});
tcp_server.start().await;
}
struct Cb {}
#[async_trait]
impl TcpServerCallBack for Cb {
async fn recv(&self, bytes: Vec<u8>, client: Arc<TcpServerClient>) -> Vec<u8> {
log::info!("recv is {bytes:?}");
client.send_bytes(b"hello client").await;
Vec::with_capacity(1)
}
}
tcp客户端示例
tcp客户端示例
Cargo.toml文件
fast_log = "1.7.3"
cbsk_base = "2.0.0"
cbsk_socket_tokio = "2.0.0"
main.rs文件
use std::net::{IpAddr, SocketAddr};
use std::sync::LazyLock;
use std::time::Duration;
use cbsk_base::{log, tokio};
use cbsk_base::async_trait::async_trait;
use cbsk_base::log::LevelFilter;
use cbsk_socket_tokio::cbsk_socket::config::re_conn::SocketReConn;
use cbsk_socket_tokio::cbsk_socket::tcp::client::config::TcpClientConfig;
use cbsk_socket_tokio::tcp::client::callback::TcpClientCallBack;
use cbsk_socket_tokio::tcp::client::TcpClient;
use cbsk_socket_tokio::tcp::common::tcp_write_trait::TcpWriteTrait;
#[allow(non_upper_case_globals)]
static addr: LazyLock<SocketAddr> = LazyLock::new(|| { SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 8080) });
#[allow(non_upper_case_globals)]
static tcp_client: LazyLock<TcpClient> = LazyLock::new(|| {
let conf = TcpClientConfig::new("tcp client".into(), *addr, SocketReConn::enable(Duration::from_secs(3)));
TcpClient::new(conf.into(), Cb {})
});
#[tokio::main]
pub async fn main() {
fast_log::init(fast_log::config::Config::default().level(LevelFilter::Info).console()).unwrap();
tcp_client.start().await;
}
struct Cb {}
#[async_trait]
impl TcpClientCallBack for Cb {
async fn conn(&self) {
tcp_client.send_bytes(b"hello server").await;
}
async fn recv(&self, bytes: Vec<u8>) -> Vec<u8> {
log::info!("bytes is {bytes:?}");
tcp_client.send_bytes(b"hello server").await;
Vec::with_capacity(1)
}
}
依赖项
~4–13MB
~135K SLoC