websocket

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package websocket provides WebSocket capability provider for gorp framework. This file implements broadcaster adapter for WebSocket server.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件实现 WebSocket 服务器的广播适配器。

Package websocket provides WebSocket capability provider for gorp framework. This file implements WebSocket client using gws library.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件使用 gws 库实现 WebSocket 客户端。

Package websocket provides WebSocket capability provider for gorp framework. This file implements cluster mode using Redis Pub/Sub for cross-node broadcast.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件使用 Redis Pub/Sub 实现集群模式的跨节点广播。

Package websocket provides WebSocket capability provider for gorp framework. This file implements connection adapter for WebSocket server.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件实现 WebSocket 服务器的连接适配器。

Package websocket provides WebSocket capability provider for gorp framework. This file implements event handler adapter for WebSocket server.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件实现 WebSocket 服务器的事件处理适配器。

Package websocket provides WebSocket capability provider for gorp framework. This file implements Prometheus metrics for WebSocket connections.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件实现 WebSocket 连接的 Prometheus 指标。

Package websocket provides WebSocket capability provider for gorp framework. This file implements the provider registration for WebSocket capability.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件实现 WebSocket 能力的 provider 注册。

Package websocket provides WebSocket capability provider for gorp framework. This file implements WebSocket server using gws library.

WebSocket 包提供 gorp 框架的 WebSocket 能力 provider。 本文件使用 gws 库实现 WebSocket 服务器。

Index

Constants

View Source
const (
	// TextMessage indicates a text message frame.
	// TextMessage 表示文本消息帧。
	TextMessage = 1

	// BinaryMessage indicates a binary message frame.
	// BinaryMessage 表示二进制消息帧。
	BinaryMessage = 2

	// CloseMessage indicates a close frame.
	// CloseMessage 表示关闭帧。
	CloseMessage = 8
)

Variables

This section is empty.

Functions

func DefaultParallelGolimit

func DefaultParallelGolimit() int

DefaultParallelGolimit returns the default parallel goroutine limit.

DefaultParallelGolimit 返回默认并行 goroutine 限制。

func NewClient

NewClient creates a new WebSocket client connection.

NewClient 创建新的 WebSocket 客户端连接。

func NewClientWithTLS

NewClientWithTLS creates a new WebSocket client with TLS config.

NewClientWithTLS 使用 TLS 配置创建新的 WebSocket 客户端。

Types

type Client

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

Client implements WebSocketClient interface using gws.

Client 使用 gws 实现 WebSocketClient 接口。

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

Close 关闭客户端连接。

func (*Client) Conn

Conn returns the underlying connection.

Conn 返回底层连接。

func (*Client) WriteBinary

func (c *Client) WriteBinary(data []byte) error

WriteBinary sends a binary message to the server.

WriteBinary 发送二进制消息到服务器。

func (*Client) WriteString

func (c *Client) WriteString(message string) error

WriteString sends a text message to the server.

WriteString 发送文本消息到服务器。

type ClusterMessage

type ClusterMessage struct {
	Type      string `json:"type"`      // "broadcast", "room", "user"
	RoomID    string `json:"room_id"`   // for room broadcast
	UserID    string `json:"user_id"`   // for user-specific message
	Message   string `json:"message"`   // text message
	Binary    []byte `json:"binary"`    // binary message (base64)
	IsBinary  bool   `json:"is_binary"` // whether this is a binary message
	SenderID  string `json:"sender_id"` // node ID of sender
	Timestamp int64  `json:"timestamp"` // unix timestamp
}

ClusterMessage represents a message broadcast across nodes.

ClusterMessage 表示跨节点广播的消息。

type ClusterServer

type ClusterServer struct {
	*Server // embed single-node server
	// contains filtered or unexported fields
}

ClusterServer implements WebSocketClusterServer with Redis Pub/Sub.

ClusterServer 使用 Redis Pub/Sub 实现 WebSocketClusterServer。

func NewClusterServer

func NewClusterServer(wsConfig *transportcontract.WebSocketConfig, clusterConfig *transportcontract.WebSocketClusterConfig) (*ClusterServer, error)

NewClusterServer creates a new cluster-enabled WebSocket server.

NewClusterServer 创建新的支持集群的 WebSocket 服务器。

func (*ClusterServer) BroadcastGlobal

func (s *ClusterServer) BroadcastGlobal(message string) error

BroadcastGlobal broadcasts message to all nodes in the cluster.

BroadcastGlobal 向集群所有节点广播消息。

func (*ClusterServer) BroadcastGlobalBinary

func (s *ClusterServer) BroadcastGlobalBinary(data []byte) error

BroadcastGlobalBinary broadcasts binary message to all nodes in the cluster.

BroadcastGlobalBinary 向集群所有节点广播二进制消息。

func (*ClusterServer) BroadcastToRoom

func (s *ClusterServer) BroadcastToRoom(roomID string, message string) error

BroadcastToRoom broadcasts message to a room across all nodes.

BroadcastToRoom 向房间广播消息(跨节点)。

func (*ClusterServer) GetNodeID

func (s *ClusterServer) GetNodeID() string

GetNodeID returns this node's ID.

GetNodeID 返回本节点 ID。

func (*ClusterServer) GlobalCount

func (s *ClusterServer) GlobalCount() int

GlobalCount returns total connection count across all nodes.

GlobalCount 返回所有节点的总连接数。

func (*ClusterServer) JoinRoom

func (s *ClusterServer) JoinRoom(roomID string, conn transportcontract.WebSocketConn)

JoinRoom adds a connection to a room.

JoinRoom 将连接加入房间。

func (*ClusterServer) LeaveRoom

func (s *ClusterServer) LeaveRoom(roomID string, conn transportcontract.WebSocketConn)

LeaveRoom removes a connection from a room.

LeaveRoom 将连接移出房间。

func (*ClusterServer) ListNodes

func (s *ClusterServer) ListNodes() []string

ListNodes returns all active node IDs.

ListNodes 返回所有活跃节点 ID。

func (*ClusterServer) NodeCount

func (s *ClusterServer) NodeCount(nodeID string) int

NodeCount returns connection count for a specific node.

NodeCount 返回指定节点的连接数。

func (*ClusterServer) SendToUser

func (s *ClusterServer) SendToUser(userID string, message string) error

SendToUser sends message to a specific user across all nodes.

SendToUser 向特定用户发送消息(跨节点)。

func (*ClusterServer) Shutdown

func (s *ClusterServer) Shutdown(ctx context.Context) error

Shutdown gracefully closes all connections and stops cluster goroutines.

Shutdown 优雅关闭所有连接并停止集群 goroutine。

func (*ClusterServer) Upgrade

Upgrade upgrades HTTP connection to WebSocket and tracks in cluster.

Upgrade 将 HTTP 连接升级为 WebSocket 并在集群中跟踪。

type Provider

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

Provider provides WebSocket capability.

Provider 提供 WebSocket 能力。

func NewProvider

func NewProvider() *Provider

NewProvider creates a new WebSocket provider with default config.

NewProvider 使用默认配置创建新的 WebSocket provider。

func NewProviderWithCluster

func NewProviderWithCluster(config *transportcontract.WebSocketConfig, clusterConfig *transportcontract.WebSocketClusterConfig) *Provider

NewProviderWithCluster creates a new WebSocket provider with cluster support.

NewProviderWithCluster 创建支持集群的 WebSocket provider。

func NewProviderWithConfig

func NewProviderWithConfig(config *transportcontract.WebSocketConfig) *Provider

NewProviderWithConfig creates a new WebSocket provider with custom config.

NewProviderWithConfig 使用自定义配置创建新的 WebSocket provider。

func (*Provider) Boot

Boot initializes the WebSocket service.

Boot 初始化 WebSocket 服务。

func (*Provider) DependsOn

func (p *Provider) DependsOn() []string

DependsOn returns the keys that this provider depends on.

DependsOn 返回该 provider 依赖的 key 列表。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer reports whether the provider should be deferred.

IsDefer 返回该 provider 是否应延迟加载。

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name.

Name 返回 provider 名称。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides returns the keys that this provider provides.

Provides 返回该 provider 提供的 key 列表。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register registers WebSocket service into container. Also registers a closer to gracefully shutdown connections on container destroy.

Register 将 WebSocket 服务注册到容器。 同时注册 closer,在容器销毁时优雅关闭连接。

type Server

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

Server implements WebSocketServer interface using gws.

Server 使用 gws 实现 WebSocketServer 接口。

func NewServer

func NewServer() *Server

NewServer creates a new WebSocket server with default config.

NewServer 使用默认配置创建新的 WebSocket 服务器。

func NewServerWithConfig

func NewServerWithConfig(config *transportcontract.WebSocketConfig) *Server

NewServerWithConfig creates a new WebSocket server with custom config.

NewServerWithConfig 使用自定义配置创建新的 WebSocket 服务器。

func (*Server) Connections

func (s *Server) Connections() []transportcontract.WebSocketConn

Connections returns all active connections.

Connections 返回所有活跃连接。

func (*Server) Count

func (s *Server) Count() int

Count returns the number of active connections.

Count 返回活跃连接数量。

func (*Server) NewBroadcaster

func (s *Server) NewBroadcaster() transportcontract.WebSocketBroadcaster

NewBroadcaster creates a new broadcaster for batch message sending.

NewBroadcaster 创建新的广播器用于批量消息发送。

func (*Server) Shutdown

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

Shutdown gracefully closes all connections. Respects context cancellation/timeout and does not hold lock during I/O.

Shutdown 优雅关闭所有连接。 尊重上下文取消/超时,不在 I/O 期间持锁。

func (*Server) Upgrade

Upgrade upgrades an HTTP connection to WebSocket.

Upgrade 将 HTTP 连接升级为 WebSocket。

type WSMetricsRecorder

type WSMetricsRecorder struct{}

WSMetricsRecorder records WebSocket metrics. Use this to record connection and message events.

WSMetricsRecorder 记录 WebSocket 指标。 用于记录连接和消息事件。

func NewWSMetricsRecorder

func NewWSMetricsRecorder() *WSMetricsRecorder

NewWSMetricsRecorder creates a new WebSocket metrics recorder.

NewWSMetricsRecorder 创建新的 WebSocket 指标记录器。

func (*WSMetricsRecorder) OnBroadcast

func (r *WSMetricsRecorder) OnBroadcast(messageType string)

OnBroadcast records a broadcast message. messageType: "text" or "binary"

OnBroadcast 记录广播消息。 messageType: "text" 或 "binary"

func (*WSMetricsRecorder) OnConnect

func (r *WSMetricsRecorder) OnConnect()

OnConnect records a new WebSocket connection.

OnConnect 记录新的 WebSocket 连接。

func (*WSMetricsRecorder) OnDisconnect

func (r *WSMetricsRecorder) OnDisconnect()

OnDisconnect records a WebSocket disconnection.

OnDisconnect 记录 WebSocket 断开连接。

func (*WSMetricsRecorder) OnError

func (r *WSMetricsRecorder) OnError(errorType string)

OnError records an error. errorType: "read", "write", "upgrade", etc.

OnError 记录错误。 errorType: "read", "write", "upgrade" 等。

func (*WSMetricsRecorder) OnMessageReceived

func (r *WSMetricsRecorder) OnMessageReceived(messageType string, latency time.Duration)

OnMessageReceived records a received message. messageType: "text" or "binary"

OnMessageReceived 记录接收的消息。 messageType: "text" 或 "binary"

func (*WSMetricsRecorder) OnMessageSent

func (r *WSMetricsRecorder) OnMessageSent(messageType string)

OnMessageSent records a sent message. messageType: "text" or "binary"

OnMessageSent 记录发送的消息。 messageType: "text" 或 "binary"

func (*WSMetricsRecorder) SetConnections

func (r *WSMetricsRecorder) SetConnections(count float64)

SetConnections sets the current number of connections. Use this for cluster mode where connections are tracked globally.

SetConnections 设置当前连接数。 用于集群模式下全局跟踪连接数。

Directories

Path Synopsis
Package noop provides a no-op WebSocket service implementation for gorp.
Package noop provides a no-op WebSocket service implementation for gorp.

Jump to

Keyboard shortcuts

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