ws

package
v0.0.0-...-25cf60e Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDeviceType = errors.New("invalid device type")
View Source
var WsFxModule = fx.Module(
	"ws",
	fx.Provide(
		fx.Annotate(
			newWebSocketUpgrader,
			fx.As(new(synp.Upgrader)),
		),
		fx.Annotate(
			newWebSocketServer,
			fx.As(new(synp.Server)),
		),
	),
)

Functions

func ServerWithAuthTimeout

func ServerWithAuthTimeout(authTimeout time.Duration) option.Opt[Server]

ServerWithAuthTimeout 设置连接建立后等待首帧 AUTH 的超时时间。

func ServerWithConnLimiter

func ServerWithConnLimiter(connLimiter *limiter.ConnLimiter) option.Opt[Server]

ServerWithConnLimiter 设置连接准入限流器。

func ServerWithDrainTimeout

func ServerWithDrainTimeout(drainTimeout time.Duration) option.Opt[Server]

ServerWithDrainTimeout 设置优雅关闭时等待存量连接排空的超时时间。

func ServerWithIdleScan

func ServerWithIdleScan(scanInterval, idleTimeout time.Duration) option.Opt[Server]

ServerWithIdleScan 设置空闲连接扫描参数。

Types

type Config

type Config struct {
	Host            string // IP 地址,默认 0.0.0.0
	Port            int    // 端口号,默认 10071
	Network         string // 网络协议,默认 tcp4
	EnableLocalMode bool   // 是自动获取 IP 地
}

Config 为 WebSocket 的相关配置。

func DefaultConfig

func DefaultConfig() *Config

func (Config) Address

func (cfg Config) Address() string

type Server

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

func NewServer

func NewServer(
	config *Config,
	upgrader synp.Upgrader,
	connHandler synp.Handler,
	connManager synp.ConnManager,
	consumers map[string]*gateway.Consumer,
	opts ...option.Opt[Server],
) *Server

func (*Server) GracefulShutdown

func (s *Server) GracefulShutdown() error

GracefulShutdown 优雅关闭 WebSocket 服务器。

顺序: 停止接受新连接 -> 停止 Kafka 消费 -> 等待存量连接排空 ( 带超时 ) -> 强制关闭剩余连接 -> 取消服务器 context。 注意不能先等待 ctx.Done 再取消 ctx——那会永久阻塞 ( 自锁 )。

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown 关闭 WebSocket 服务器。

func (*Server) Start

func (s *Server) Start() error

Start 启动 WebSocket 服务器。

type Upgrader

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

Upgrader 将 HTTP 连接升级为 WebSocket 连接。

注意:鉴权不在升级阶段完成——新协议要求连接建立后的第一帧必须为 AUTH, 携带后端签发的 JWT ( 契约见 api/proto/auth/v1/auth.proto )。 升级阶段只协商压缩扩展并解析设备类型等非敏感参数。

func NewUpgrader

func NewUpgrader(compressionConfig compression.Config) *Upgrader

func (*Upgrader) Name

func (u *Upgrader) Name() string

func (*Upgrader) Upgrade

func (u *Upgrader) Upgrade(conn net.Conn) (session.Session, *compression.State, error)

Directories

Path Synopsis
lifecycle
Package lifecycle 提供了和连接生命周期相关事件的回调接口实现。
Package lifecycle 提供了和连接生命周期相关事件的回调接口实现。
Package gateway 提供了 WebSocket 网关的业务事件处理实现。
Package gateway 提供了 WebSocket 网关的业务事件处理实现。

Jump to

Keyboard shortcuts

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