gateway

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorParseError     = -32700
	ErrorInvalidRequest = -32600
	ErrorMethodNotFound = -32601
	ErrorInvalidParams  = -32602
	ErrorInternalError  = -32603
)

Error codes

View Source
const ProtocolVersion = "1.0"

ProtocolVersion 当前协议版本

Variables

This section is empty.

Functions

func EncodeResponse

func EncodeResponse(resp *JSONRPCResponse) ([]byte, error)

EncodeResponse 编码响应

Types

type Connection

type Connection struct {
	*websocket.Conn
	ID string
	// contains filtered or unexported fields
}

Connection WebSocket 连接

func NewConnection

func NewConnection(ws *websocket.Conn, cfg *WebSocketConfig) *Connection

NewConnection 创建连接

func (*Connection) Close

func (c *Connection) Close() error

Close 关闭连接

func (*Connection) SendJSON

func (c *Connection) SendJSON(v interface{}) error

SendJSON 发送 JSON 消息

func (*Connection) SendMessage

func (c *Connection) SendMessage(messageType int, data []byte) error

SendMessage 发送消息

type Handler

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

Handler WebSocket 消息处理器

func NewHandler

func NewHandler(messageBus *bus.MessageBus, sessionMgr *session.Manager, channelMgr *channels.Manager) *Handler

NewHandler 创建处理器

func (*Handler) BroadcastNotification

func (h *Handler) BroadcastNotification(method string, data interface{}) ([]byte, error)

BroadcastNotification 广播通知

func (*Handler) HandleRequest

func (h *Handler) HandleRequest(sessionID string, req *JSONRPCRequest) *JSONRPCResponse

HandleRequest 处理请求

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string                 `json:"jsonrpc"`
	ID      string                 `json:"id,omitempty"` // 通知可以没有ID
	Method  string                 `json:"method"`
	Params  map[string]interface{} `json:"params,omitempty"`
}

JSONRPCRequest JSON-RPC 请求

func ParseRequest

func ParseRequest(data []byte) (*JSONRPCRequest, error)

ParseRequest 解析请求

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      string      `json:"id,omitempty"`
	Result  interface{} `json:"result,omitempty"`
	Error   *RPCError   `json:"error,omitempty"`
}

JSONRPCResponse JSON-RPC 响应

func NewErrorResponse

func NewErrorResponse(id string, code int, message string) *JSONRPCResponse

NewErrorResponse 创建错误响应

func NewSuccessResponse

func NewSuccessResponse(id string, result interface{}) *JSONRPCResponse

NewSuccessResponse 创建成功响应

type MessageType

type MessageType string

MessageType 消息类型

const (
	// Request 请求
	MessageTypeRequest MessageType = "request"
	// Response 响应
	MessageTypeResponse MessageType = "response"
	// Notification 通知
	MessageTypeNotification MessageType = "notification"
)

type MethodHandler

type MethodHandler func(sessionID string, params map[string]interface{}) (interface{}, error)

MethodHandler 方法处理器

type MethodRegistry

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

MethodRegistry 方法注册表

func NewMethodRegistry

func NewMethodRegistry() *MethodRegistry

NewMethodRegistry 创建方法注册表

func (*MethodRegistry) Call

func (r *MethodRegistry) Call(method string, sessionID string, params map[string]interface{}) (interface{}, error)

Call 调用方法

func (*MethodRegistry) Register

func (r *MethodRegistry) Register(method string, handler MethodHandler)

Register 注册方法

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    string `json:"data,omitempty"`
}

RPCError RPC 错误

type Server

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

Server HTTP 网关服务器

func NewServer

func NewServer(cfg *config.GatewayConfig, messageBus *bus.MessageBus, channelMgr *channels.Manager, sessionMgr *session.Manager) *Server

NewServer 创建网关服务器

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning 检查是否运行中

func (*Server) SetWebSocketConfig

func (s *Server) SetWebSocketConfig(cfg *WebSocketConfig)

SetWebSocketConfig 设置 WebSocket 配置

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start 启动服务器

func (*Server) Stop

func (s *Server) Stop() error

Stop 停止服务器

type WebSocketConfig

type WebSocketConfig struct {
	Host           string
	Port           int
	Path           string
	EnableAuth     bool
	AuthToken      string
	PingInterval   time.Duration
	PongTimeout    time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	MaxMessageSize int64
	// TLS 配置
	EnableTLS bool
	CertFile  string
	KeyFile   string
}

WebSocketConfig WebSocket 配置

Jump to

Keyboard shortcuts

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