websocket

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 24 Imported by: 0

README

WebSocket Engine for Go

Go Reference License

中文

A lightweight WebSocket engine 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.

Acknowledgments

This project is built on the following open-source libraries:

Documentation

Index

Constants

View Source
const (
	SendLimit = 100
	BreakTime = 600  // heartbeat breakTime in seconds
	Interval  = 1000 // heartbeat interval in milliseconds

	Connected = "connected"
	Success   = "success"
)
View Source
const (
	RateLimit       = 100
	ReadBufferSize  = 1024
	WriteBufferSize = 1024
)

Variables

View Source
var File_pb_message_proto protoreflect.FileDescriptor

Functions

func Connect

func Connect(engine *Engine, opts ...Option) gin.HandlerFunc

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 (*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) 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.

type Engine added in v1.0.9

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

func NewEngineWithOptions added in v1.0.9

func NewEngineWithOptions(opts ...EngineOption) *Engine

func (*Engine) Publish added in v1.0.9

func (e *Engine) Publish(channel string, message []byte) (err error)

Publish message to channel

func (*Engine) RegisterJsonRouter added in v1.0.9

func (e *Engine) RegisterJsonRouter(command string, handler Handler[*JsonMessage])

RegisterJsonRouter register json route

func (*Engine) RegisterProtoRouter added in v1.0.9

func (e *Engine) RegisterProtoRouter(command string, handler Handler[*ProtoMessage])

RegisterProtoRouter register proto route

func (*Engine) Subscribe added in v1.0.9

func (e *Engine) Subscribe(id, channel string) error

Subscribe Subscribe to channel

func (*Engine) Unsubscribe added in v1.0.9

func (e *Engine) Unsubscribe(id, channel string) error

Unsubscribe from channel

type EngineOption added in v1.0.9

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

func WithLogger added in v1.0.9

func WithLogger(logger *logger.Logger) EngineOption

func WithMaxConn added in v1.0.3

func WithMaxConn(maxConn uint32) EngineOption

func WithPublishWorkPool added in v1.0.9

func WithPublishWorkPool(poolSize int) EngineOption

func WithReadBufferSize added in v1.0.3

func WithReadBufferSize(size int) EngineOption

func WithSubscribeEngine added in v1.0.9

func WithSubscribeEngine(storage Memory) EngineOption

func WithWriteBufferSize added in v1.0.3

func WithWriteBufferSize(size int) EngineOption

type Func added in v1.0.9

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

type Handler added in v1.0.9

type Handler[T Message] func(message T)

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 JsonMessage added in v1.0.9

type JsonMessage struct {
	RequestId string `json:"request_id" validate:"required"`
	SocketId  string `json:"socket_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 Memory added in v1.0.5

type Memory interface {
	Set(id, channel string) error
	Get(id string) (ids []string, err error)
	Delete(id, channel string) error
}

type Message

type Message interface {
	*JsonMessage | *ProtoMessage
}

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 WithProtocol added in v1.0.9

func WithProtocol(protocol int) Option

func WithSendLimit added in v1.0.3

func WithSendLimit(sendLimit int) Option

type ProtoFuncWrapper added in v1.0.9

type ProtoFuncWrapper struct {
	*ProtoMessage
}

type ProtoMessage added in v1.0.9

type ProtoMessage struct {
	RequestId string `protobuf:"bytes,1,opt,name=request_id,json=requestId,proto3" json:"request_id,omitempty"`
	SocketId  string `protobuf:"bytes,2,opt,name=socket_id,json=socketId,proto3" json:"socket_id,omitempty"`
	Command   string `protobuf:"bytes,3,opt,name=command,proto3" json:"command,omitempty"`
	Code      int32  `protobuf:"varint,4,opt,name=code,proto3" json:"code,omitempty"`
	Message   string `protobuf:"bytes,5,opt,name=message,proto3" json:"message,omitempty"`
	Data      []byte `protobuf:"bytes,6,opt,name=data,proto3" json:"data,omitempty"`
	// contains filtered or unexported fields
}

func (*ProtoMessage) Descriptor deprecated added in v1.0.9

func (*ProtoMessage) Descriptor() ([]byte, []int)

Deprecated: Use ProtoMessage.ProtoReflect.Descriptor instead.

func (*ProtoMessage) GetCode added in v1.0.9

func (x *ProtoMessage) GetCode() int32

func (*ProtoMessage) GetCommand added in v1.0.9

func (x *ProtoMessage) GetCommand() string

func (*ProtoMessage) GetData added in v1.0.9

func (x *ProtoMessage) GetData() []byte

func (*ProtoMessage) GetMessage added in v1.0.9

func (x *ProtoMessage) GetMessage() string

func (*ProtoMessage) GetRequestId added in v1.0.9

func (x *ProtoMessage) GetRequestId() string

func (*ProtoMessage) GetSocketId added in v1.0.9

func (x *ProtoMessage) GetSocketId() string

func (*ProtoMessage) ProtoMessage added in v1.0.9

func (*ProtoMessage) ProtoMessage()

func (*ProtoMessage) ProtoReflect added in v1.0.9

func (x *ProtoMessage) ProtoReflect() protoreflect.Message

func (*ProtoMessage) Reset added in v1.0.9

func (x *ProtoMessage) Reset()

func (*ProtoMessage) String added in v1.0.9

func (x *ProtoMessage) String() string

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 Router

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

func NewRouter added in v1.0.9

func NewRouter[T Message]() *Router[T]

type Serializer

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

type SystemMemory added in v1.0.5

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

func (*SystemMemory) Delete added in v1.0.5

func (s *SystemMemory) Delete(id, channel string) error

func (*SystemMemory) Get added in v1.0.5

func (s *SystemMemory) Get(channel string) (ids []string, err error)

func (*SystemMemory) Set added in v1.0.5

func (s *SystemMemory) Set(id, channel string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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