websocket

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 17 Imported by: 0

README

WebSocket Manager for Go

Go Reference License

中文

A lightweight WebSocket manager for Go, built with gin-gonic and gorilla/websocket. This library provides a robust WebSocket server implementation with features like connection management, message routing, and validation.

Features

  • WebSocket Management: Handle multiple WebSocket connections with ease.
  • Message Routing: Route messages based on custom protocols.
  • Validation: Built-in support for struct validation using go-playground/validator.
  • Heartbeat Detection: Automatically detect and close inactive connections.
  • Customizable: Easily extend and customize the library for your needs.

Installation

To install the library, use go get:

go get -u github.com/gin-generator/websocket

Examples

You can find usage examples in the example folder.

Documentation

Index

Constants

View Source
const (
	SendLimit = 100
	BreakTime = 600  // Heartbeat breakTime in seconds
	Interval  = 1000 // Heartbeat interval in milliseconds
)
View Source
const (
	RegisterLimit   = 1000
	ReadBufferSize  = 4096
	WriteBufferSize = 4096
	MaxConnections  = 100000
)

Variables

View Source
var (
	Once sync.Once
)

Functions

func Connect

func Connect(opts ...Option) gin.HandlerFunc

func NewManagerWithOptions added in v1.0.3

func NewManagerWithOptions(opts ...ManagerOption)

func ValidateStructWithOutCtx

func ValidateStructWithOutCtx(s interface{}) (err error)

ValidateStructWithOutCtx check struct

Types

type Client added in v1.0.3

type Client struct {
	context.Context
	// contains filtered or unexported fields
}

func NewClientWithOptions added in v1.0.3

func NewClientWithOptions(conn *websocket.Conn, opts ...Option) *Client

func (*Client) Close added in v1.0.3

func (c *Client) Close()

Close logout

func (*Client) Deadline added in v1.0.3

func (c *Client) Deadline() (deadline time.Time, ok bool)

Deadline SetDeadline Set the deadline

func (*Client) Done added in v1.0.3

func (c *Client) Done() <-chan struct{}

Done returns a channel that is closed when the context is done.

func (*Client) Err added in v1.0.3

func (c *Client) Err() error

Err returns a non-nil error value after the context is done.

func (*Client) Heartbeat added in v1.0.3

func (c *Client) Heartbeat()

Heartbeat detection

func (*Client) Read added in v1.0.3

func (c *Client) Read()

Read message

func (*Client) SendMessage added in v1.0.3

func (c *Client) SendMessage(message Send)

SendMessage Send message

func (*Client) SetValue added in v1.0.3

func (c *Client) SetValue(key, value any)

SetValue sets the value associated with key in the context.

func (*Client) Value added in v1.0.3

func (c *Client) Value(key any) any

Value returns the value associated with key in the context, if any.

func (*Client) Write added in v1.0.3

func (c *Client) Write()

Write Send message

type CommandRouter

type CommandRouter struct {
	// contains filtered or unexported fields
}
var Router *CommandRouter

func NewCommandRouter

func NewCommandRouter() *CommandRouter

func (*CommandRouter) ProtoHandle

func (r *CommandRouter) ProtoHandle(client *Client, send Send) (err error)

func (*CommandRouter) RegisterProto

func (r *CommandRouter) RegisterProto(command string, handler func(*m.Message))

func (*CommandRouter) RegisterText

func (r *CommandRouter) RegisterText(command string, handler func(*Message))

func (*CommandRouter) TextHandle

func (r *CommandRouter) TextHandle(client *Client, send Send) (err error)

type JSONSerializer

type JSONSerializer[T Message] struct {
	// contains filtered or unexported fields
}

func NewJSONSerializer

func NewJSONSerializer[T Message](data T) *JSONSerializer[T]

func (*JSONSerializer[T]) Data

func (j *JSONSerializer[T]) Data() T

func (*JSONSerializer[T]) Deserialize

func (j *JSONSerializer[T]) Deserialize(data []byte, v *T) error

func (*JSONSerializer[T]) Serialize

func (j *JSONSerializer[T]) Serialize(data T) ([]byte, error)

type Manager

type Manager struct {
	Register        chan *Client
	Unset           chan *Client
	MaxConn         uint32
	ReadBufferSize  int
	WriteBufferSize int
	Errs            chan error
	// contains filtered or unexported fields
}

func (*Manager) GetAllClient

func (m *Manager) GetAllClient() (pool map[string]*Client)

GetAllClient Get all clients

func (*Manager) GetClient

func (m *Manager) GetClient(id string) (client *Client, err error)

GetClient Get client by id

func (*Manager) SendBroadcast

func (m *Manager) SendBroadcast(message Send)

SendBroadcast Send broadcast message

type ManagerOption added in v1.0.3

type ManagerOption interface {
	// contains filtered or unexported methods
}

func WithMaxConn added in v1.0.3

func WithMaxConn(maxConn uint32) ManagerOption

func WithReadBufferSize added in v1.0.3

func WithReadBufferSize(size int) ManagerOption

func WithRegisterLimit added in v1.0.3

func WithRegisterLimit(registerLimit int) ManagerOption

func WithWriteBufferSize added in v1.0.3

func WithWriteBufferSize(size int) ManagerOption

type Message

type Message struct {
	RequestId string `json:"request_id" validate:"required"`
	Command   string `json:"command" validate:"required"`
	Code      int32  `json:"code,omitempty"`
	Message   string `json:"message,omitempty"`
	Data      []byte `json:"data,omitempty"`
}

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithBreakTime added in v1.0.3

func WithBreakTime(breakTime int64) Option

func WithClientValues added in v1.0.3

func WithClientValues(values map[any]any) Option

func WithInterval added in v1.0.3

func WithInterval(interval int64) Option

func WithSendLimit added in v1.0.3

func WithSendLimit(sendLimit int) Option

type ProtoHandler

type ProtoHandler func(message *m.Message)

Handler function type

type ProtocolSerializer

type ProtocolSerializer[T proto.Message] struct {
	// contains filtered or unexported fields
}

func NewProtocolSerializer

func NewProtocolSerializer[T proto.Message](data T) *ProtocolSerializer[T]

func (*ProtocolSerializer[T]) Data

func (p *ProtocolSerializer[T]) Data() T

func (*ProtocolSerializer[T]) Deserialize

func (p *ProtocolSerializer[T]) Deserialize(data []byte, v *T) error

func (*ProtocolSerializer[T]) Serialize

func (p *ProtocolSerializer[T]) Serialize(data T) ([]byte, error)

type Send

type Send struct {
	Protocol int
	Message  []byte
}

type Serializer

type Serializer[T any] interface {
	Serialize(data T) ([]byte, error)
	Deserialize(data []byte, v *T) error
	Data() T
}

type TextHandler

type TextHandler func(message *Message)

Handler function type

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

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