pub struct Gateway { /* private fields */ }Expand description
Gateway API 客户端 — 对齐 PHP GatewayWorker\Gateway
提供 WebSocket 客户端管理、群组广播、消息推送等能力。
通过 GatewayTransport trait 抽象底层通信。
§PHP 对齐
PHP Gateway 全部为静态方法,Rust 通过实例方法表达,配置与传输层通过构造函数注入,
便于测试和多实例隔离。
§用法
ⓘ
use sz_rust_auth_facade::gateway::{
Gateway, GatewayConfig, MemoryGatewayTransport, GatewayTransport,
};
use std::sync::Arc;
let transport = Arc::new(MemoryGatewayTransport::new());
let gateway = Gateway::new(GatewayConfig::new("127.0.0.1:1238"), transport.clone());
transport.register_client("7f00000108fc00000001");
gateway.send_to_client("7f00000108fc00000001", "hello").unwrap();Implementations§
Source§impl Gateway
impl Gateway
Sourcepub fn new(
config: GatewayConfig,
transport: Arc<dyn GatewayTransport>,
) -> Gateway
pub fn new( config: GatewayConfig, transport: Arc<dyn GatewayTransport>, ) -> Gateway
Sourcepub fn send_to_client(
&self,
client_id: &str,
message: &str,
) -> Result<(), GatewayError>
pub fn send_to_client( &self, client_id: &str, message: &str, ) -> Result<(), GatewayError>
向指定 client_id 发送消息 — 对齐 Gateway::sendToClient()
Sourcepub fn send_to_all(&self, message: &str) -> Result<(), GatewayError>
pub fn send_to_all(&self, message: &str) -> Result<(), GatewayError>
向所有在线 client_id 发送广播 — 对齐 Gateway::sendToAll()
Sourcepub fn send_to_group(
&self,
group: &str,
message: &str,
) -> Result<(), GatewayError>
pub fn send_to_group( &self, group: &str, message: &str, ) -> Result<(), GatewayError>
向指定群组发送消息 — 对齐 Gateway::sendToGroup()
Sourcepub fn join_group(
&self,
client_id: &str,
group: &str,
) -> Result<(), GatewayError>
pub fn join_group( &self, client_id: &str, group: &str, ) -> Result<(), GatewayError>
将 client_id 加入群组 — 对齐 Gateway::joinGroup()
Sourcepub fn leave_group(
&self,
client_id: &str,
group: &str,
) -> Result<(), GatewayError>
pub fn leave_group( &self, client_id: &str, group: &str, ) -> Result<(), GatewayError>
将 client_id 离开群组 — 对齐 Gateway::leaveGroup()
Sourcepub fn is_online(&self, client_id: &str) -> Result<bool, GatewayError>
pub fn is_online(&self, client_id: &str) -> Result<bool, GatewayError>
判断 client_id 是否在线 — 对齐 Gateway::isOnline()
Sourcepub fn get_client_count(&self) -> Result<usize, GatewayError>
pub fn get_client_count(&self) -> Result<usize, GatewayError>
获取在线 client_id 数量 — 对齐 Gateway::getClientCount()
Sourcepub fn get_client_count_by_group(
&self,
group: &str,
) -> Result<usize, GatewayError>
pub fn get_client_count_by_group( &self, group: &str, ) -> Result<usize, GatewayError>
获取指定群组的在线 client_id 数量 — 对齐 Gateway::getClientCountByGroup()
Sourcepub fn get_all_client_ids(&self) -> Result<Vec<String>, GatewayError>
pub fn get_all_client_ids(&self) -> Result<Vec<String>, GatewayError>
获取所有在线 client_id — 对齐 Gateway::getAllClientIds()
Sourcepub fn get_client_id_list_by_group(
&self,
group: &str,
) -> Result<Vec<String>, GatewayError>
pub fn get_client_id_list_by_group( &self, group: &str, ) -> Result<Vec<String>, GatewayError>
获取指定群组中的 client_id — 对齐 Gateway::getClientIdListByGroup()
Sourcepub fn close_client(&self, client_id: &str) -> Result<(), GatewayError>
pub fn close_client(&self, client_id: &str) -> Result<(), GatewayError>
关闭指定 client_id 的连接 — 对齐 Gateway::closeClient()
Sourcepub fn config(&self) -> &GatewayConfig
pub fn config(&self) -> &GatewayConfig
获取 Gateway 配置
Auto Trait Implementations§
impl !RefUnwindSafe for Gateway
impl !UnwindSafe for Gateway
impl Freeze for Gateway
impl Send for Gateway
impl Sync for Gateway
impl Unpin for Gateway
impl UnsafeUnpin for Gateway
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.