Files
imks/socket/adapter/mod.rs
T
zhenyi 06e8ee96a5 feat(auth): add authentication protocol definitions and build configuration
- Add TokenClaims message for JWT payload structure with user id, issuer, timestamps, and scopes
- Implement IssueTokenRequest/Response for creating access and refresh tokens with TTL support
- Create RefreshTokenRequest/Response for token rotation functionality
- Define RevokeTokenRequest/Response with support for single token or user-wide revocation
- Add VerifyTokenRequest/Response for validating JWT tokens with detailed claims information
- Implement signing key distribution system with GetSigningKeysRequest/Response
- Create TokenService gRPC service with IssueToken, RefreshToken, RevokeToken, VerifyToken, and GetSigningKeys methods
- Add build.rs configuration to compile proto files using tonic_prost_build
- Include channel, channel_settings, member, and permission protocol definitions for IM services
- Generate Rust code bindings through pb/core.rs and pb/im.rs modules
2026-06-10 23:45:40 +08:00

99 lines
2.9 KiB
Rust

pub mod local;
pub mod redis;
pub mod nats;
use std::collections::HashSet;
use async_trait::async_trait;
use thiserror::Error;
use crate::socket::packet::Packet;
#[derive(Error, Debug)]
pub enum AdapterError {
#[error("Redis error: {0}")]
Redis(String),
#[error("NATS error: {0}")]
Nats(String),
#[error("Message bus error: {0}")]
MessageBus(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Room error: {0}")]
Room(String),
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct BroadcastOptions {
pub rooms: HashSet<String>,
pub except: HashSet<String>,
pub flags: BroadcastFlags,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct BroadcastFlags {
pub local_only: bool,
pub broadcast: bool,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SocketInfo {
pub sid: String,
pub namespace: String,
pub rooms: HashSet<String>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub enum BusMessage {
Broadcast {
namespace: String,
packet: String,
opts: BroadcastOptions,
server_id: String,
},
SocketJoin {
namespace: String,
sid: String,
room: String,
server_id: String,
},
SocketLeave {
namespace: String,
sid: String,
room: String,
server_id: String,
},
SocketDisconnect {
namespace: String,
sid: String,
server_id: String,
},
}
#[async_trait]
pub trait Adapter: Send + Sync + 'static {
async fn broadcast(&self, packet: &Packet, opts: &BroadcastOptions) -> Result<(), AdapterError>;
async fn add(&self, sid: &str, room: &str, ns: &str) -> Result<(), AdapterError>;
async fn del(&self, sid: &str, room: &str, ns: &str) -> Result<(), AdapterError>;
async fn del_all(&self, sid: &str, ns: &str) -> Result<(), AdapterError>;
async fn fetch_sockets(&self, opts: &BroadcastOptions) -> Result<Vec<SocketInfo>, AdapterError>;
async fn socket_rooms(&self, sid: &str) -> Result<HashSet<String>, AdapterError>;
fn server_id(&self) -> &str;
async fn close(&self) -> Result<(), AdapterError>;
/// Register a socket SID → engine SID mapping in the adapter.
/// Must be called when a socket first connects, before any room operations.
/// The `ns` parameter is the namespace path this socket belongs to.
async fn register(&self, _socket_sid: &str, _engine_sid: &str, _ns: &str) -> Result<(), AdapterError> {
Ok(())
}
/// Unregister a socket from the adapter, removing all local mappings.
async fn unregister(&self, _socket_sid: &str, _ns: &str) -> Result<(), AdapterError> {
Ok(())
}
}
pub use local::LocalAdapter;
pub use redis::RedisAdapter;
pub use nats::NatsAdapter;