protocol

package
v0.0.0-...-46ed21b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package protocol 提供 NODE 和 Dpanel 之间的通信协议

Index

Constants

View Source
const AuthTimeout = 5 * time.Second

AuthTimeout 认证超时时间

Variables

View Source
var (
	ErrAuthFailed  = errors.New("authentication failed")
	ErrAuthTimeout = errors.New("authentication timeout")
)

错误定义

View Source
var ErrSessionNotFound = channel.ErrChannelNotFound

错误定义

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 协议客户端

func NewClient

func NewClient(config *ClientConfig) *Client

NewClient 创建客户端

func (*Client) ActiveTransport

func (c *Client) ActiveTransport() transport.TransportType

ActiveTransport 获取当前活跃传输协议

func (*Client) Broadcast

func (c *Client) Broadcast(ctx context.Context, msgType message.MessageType, data interface{}) error

Broadcast 广播消息

func (*Client) Close

func (c *Client) Close() error

Close 关闭连接

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect 连接到服务器

func (*Client) GetChannel

func (c *Client) GetChannel(msgType message.MessageType) *channel.Channel

GetChannel 获取消息通道

func (*Client) Request

func (c *Client) Request(ctx context.Context, data interface{}) (*message.Envelope, error)

Request 发送请求并等待响应

func (*Client) Send

func (c *Client) Send(ctx context.Context, msgType message.MessageType, data interface{}) error

Send 发送数据

func (*Client) SendTo

func (c *Client) SendTo(ctx context.Context, to string, msgType message.MessageType, data interface{}) error

SendTo 发送给指定目标

func (*Client) SessionID

func (c *Client) SessionID() string

SessionID 获取会话ID

func (*Client) Subscribe

func (c *Client) Subscribe(msgType message.MessageType, handler channel.Handler)

Subscribe 订阅消息类型

type ClientConfig

type ClientConfig struct {
	WSEndpoint     string        // WebSocket 端点
	GRPCEndpoint   string        // gRPC 端点
	HTTPEndpoint   string        // HTTP 端点
	ClientID       string        // 客户端ID
	AuthToken      string        // 认证令牌
	NodeType       string        // 节点类型 (node/panel)
	Version        string        // 客户端版本
	ConnectTimeout time.Duration // 连接超时
	RequestTimeout time.Duration // 请求超时
}

ClientConfig 客户端配置

func DefaultClientConfig

func DefaultClientConfig() *ClientConfig

DefaultClientConfig 默认客户端配置

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 协议服务端

func NewServer

func NewServer(config *ServerConfig) *Server

NewServer 创建服务端

func (*Server) Broadcast

func (s *Server) Broadcast(envelope *message.Envelope)

Broadcast 广播消息给所有客户端

func (*Server) BroadcastToGroup

func (s *Server) BroadcastToGroup(group string, envelope *message.Envelope)

BroadcastToGroup 广播消息给指定分组

func (*Server) GetSession

func (s *Server) GetSession(sessionID string) (*Session, bool)

GetSession 获取会话

func (*Server) JoinGroup

func (s *Server) JoinGroup(sessionID, group string)

JoinGroup 将会话加入分组

func (*Server) LeaveGroup

func (s *Server) LeaveGroup(sessionID, group string)

LeaveGroup 将会话移出分组

func (*Server) OnMessage

func (s *Server) OnMessage(msgType message.MessageType, h channel.Handler)

OnMessage 注册消息处理器

func (*Server) SendTo

func (s *Server) SendTo(sessionID string, envelope *message.Envelope) error

SendTo 发送给指定会话

func (*Server) SendToClient

func (s *Server) SendToClient(clientID string, envelope *message.Envelope) error

SendToClient 发送给指定客户端ID

func (*Server) Start

func (s *Server) Start() error

Start 启动服务端

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务端

type ServerConfig

type ServerConfig struct {
	Address            string
	Path               string
	HeartbeatInterval  time.Duration
	ReadBufferSize     int
	WriteBufferSize    int
	CompressionEnabled bool
}

ServerConfig 服务端配置

func DefaultServerConfig

func DefaultServerConfig() *ServerConfig

DefaultServerConfig 默认服务端配置

type Session

type Session struct {
	ID       string
	ClientID string
	Conn     *websocket.Conn
	SendChan chan *message.Envelope
	// contains filtered or unexported fields
}

Session 客户端会话

func (*Session) Close

func (sess *Session) Close()

Close 关闭会话

func (*Session) IsAuthenticated

func (sess *Session) IsAuthenticated() bool

IsAuthenticated 是否已认证

func (*Session) Send

func (sess *Session) Send(envelope *message.Envelope) error

Send 发送消息

Directories

Path Synopsis
Package channel 提供基于 Channel 的消息分发机制
Package channel 提供基于 Channel 的消息分发机制
Package compress 提供数据压缩和解压缩功能
Package compress 提供数据压缩和解压缩功能
Package message 定义通信协议的消息结构体
Package message 定义通信协议的消息结构体
Package transport 定义传输层接口
Package transport 定义传输层接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL