Help us shape the future of Go! Take the 2026 Go Developer Survey

ws_server

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IWsTcpSvrEventHandler added in v1.0.4

type IWsTcpSvrEventHandler interface {
	OnConn(net.Conn)             // 被Listener协程调用,一个WsSvr对应一个Listener协程
	OnRead(net.Conn, []byte) int // 被Read协程调用,每个Connection对应一个Read协调
	OnClose(net.Conn)            // 被Read协程调用,每个Connection对应一个Read协调
}

type TcpConnInfo added in v1.0.4

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

type WsTcpSvr added in v1.0.4

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

func (*WsTcpSvr) Close added in v1.0.4

func (s *WsTcpSvr) Close(conn net.Conn) error

func (*WsTcpSvr) InitAndRun added in v1.0.4

func (s *WsTcpSvr) InitAndRun(implType, mod string, port int, handler IWsTcpSvrEventHandler) error

func (*WsTcpSvr) Quiesce added in v1.2.5

func (s *WsTcpSvr) Quiesce()

Quiesce 停止接受新 WS Upgrade,保留既有连接处理在途工作。幂等。

关闭 HTTP listener 以停止新 Upgrade(gin 的 Serve 会退出),但保留已升级的 WebSocket 连接;它们的拆除留给 Stop 强制执行。

func (*WsTcpSvr) RunGinWs added in v1.0.4

func (s *WsTcpSvr) RunGinWs(mode string, wsPort int) error

RunGinWs 同步绑定 HTTP 监听器并启动 Gin WebSocket 服务。

关键修复:

  • 同步 net.Listen 使端口冲突在 Start 期(而非稍后的 goroutine 内)返回。
  • 删除包级全局 gin.Engine,改为实例字段,支持同进程多 WS 实例。
  • 保存 listener / http.Server,使 Quiesce 能关闭 listener 停止新 Upgrade、Stop 能 强制 Close 残留连接。
  • Serve goroutine 的非预期退出错误送入 serveErr(容量 1),供上层监督(如 RuntimeErrorSource)感知监听器死亡;预期的 http.ErrServerClosed 不上报。

func (*WsTcpSvr) ServeErrors added in v1.2.5

func (s *WsTcpSvr) ServeErrors() <-chan error

ServeErrors 返回 ws gin server 的运行期错误 channel(容量 1)。非预期退出(非 http.ErrServerClosed)会送入此 channel,供上层监督监听器死亡。

func (*WsTcpSvr) Stop added in v1.2.5

func (s *WsTcpSvr) Stop(_ context.Context) error

Stop 拒绝新 Upgrade 并关闭全部已建立连接,用于排空超时后的强制关停。幂等。

先在锁内快照连接列表并释放锁,再在锁外逐个 Close。禁止在连接表锁内执行 网络 Close。同时关闭 HTTP server 强制拆除底层监听器。

ctx 当前未使用(关闭是同步的),保留作为未来受 context 约束强制关闭的扩展点; 返回 error 使关闭结果可观测。

func (*WsTcpSvr) WriteData added in v1.0.4

func (s *WsTcpSvr) WriteData(conn net.Conn, data1 []byte, data2 []byte) error

Jump to

Keyboard shortcuts

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