websocket

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultWebsocketWriteTimeout     = 0
	DefaultWebsocketReadTimeout      = 0
	DefaultWebsocketPongTimeout      = 60 * time.Second
	DefaultWebsocketPingPeriod       = (DefaultWebsocketPongTimeout * 9) / 10
	DefaultWebsocketMaxMessageSize   = 1024
	DefaultWebsocketReadBufferSize   = 4096
	DefaultWebsocketWriterBufferSize = 4096
	DefaultEvtMessageKey             = "gin-websocket-message:"
)
View Source
const (
	All       = ""
	Broadcast = ";to;all;except;me;"
)
View Source
const CloseMessage = websocket.CloseMessage
View Source
const WriteWait = 1 * time.Second

Variables

View Source
var ClientSource []byte
View Source
var (
	DefaultIDGenerator = func(*gin.Context) string {
		id, err := uuid.NewV4()
		if err != nil {
			return randomString(64)
		}
		return id.String()
	}
)
View Source
var ErrAlreadyDisconnected = errors.New("already disconnected")

Functions

This section is empty.

Types

type Config

type Config struct {
	IDGenerator       func(ctx *gin.Context) string
	EvtMessagePrefix  []byte
	Error             func(w http.ResponseWriter, r *http.Request, status int, reason error)
	CheckOrigin       func(r *http.Request) bool
	HandshakeTimeout  time.Duration
	WriteTimeout      time.Duration
	ReadTimeout       time.Duration
	PongTimeout       time.Duration
	PingPeriod        time.Duration
	MaxMessageSize    int64
	BinaryMessages    bool
	ReadBufferSize    int
	WriteBufferSize   int
	EnableCompression bool
	Subprotocols      []string
}

func (Config) Validate

func (c Config) Validate() Config

type Connection

type Connection interface {
	Emitter
	Err() error
	ID() string
	Server() *Server
	Write(websocketMessageType int, data []byte) error
	Context() *gin.Context
	OnDisconnect(DisconnectFunc)
	OnError(ErrorFunc)
	OnPing(PingFunc)
	OnPong(PongFunc)
	FireOnError(err error)
	To(string) Emitter
	OnMessage(NativeMessageFunc)
	On(string, MessageFunc)
	Join(string)
	IsJoined(roomName string) bool
	Leave(string) bool
	OnLeave(roomLeaveCb LeaveRoomFunc)
	Wait()
	Disconnect() error
	SetValue(key string, value any)
	GetValue(key string) any
	GetValueArrString(key string) []string
	GetValueString(key string) string
	GetValueInt(key string) int
}

Connection 接口

type ConnectionFunc

type ConnectionFunc func(Connection)

type ConnectionValues

type ConnectionValues []connectionValue

func (*ConnectionValues) Get

func (r *ConnectionValues) Get(key string) any

func (*ConnectionValues) Reset

func (r *ConnectionValues) Reset()

func (*ConnectionValues) Set

func (r *ConnectionValues) Set(key string, value any)

type DisconnectFunc

type DisconnectFunc func()

type Emitter

type Emitter interface {
	EmitMessage([]byte) error
	Emit(string, any) error
}

type ErrorFunc

type ErrorFunc func(error)

type LeaveRoomFunc

type LeaveRoomFunc func(roomName string)

type MessageFunc

type MessageFunc any

type NativeMessageFunc

type NativeMessageFunc func([]byte)

type PingFunc

type PingFunc func()

type PongFunc

type PongFunc func()

type Server

type Server struct {
	ClientSource []byte
	// contains filtered or unexported fields
}

func NewWSServer

func NewWSServer(cfg Config) *Server

func (*Server) Disconnect

func (s *Server) Disconnect(connID string) (err error)

func (*Server) GetConnection

func (s *Server) GetConnection(connID string) Connection

func (*Server) GetConnections

func (s *Server) GetConnections() []Connection

func (*Server) GetConnectionsByRoom

func (s *Server) GetConnectionsByRoom(roomName string) []Connection

func (*Server) GetTotalConnections

func (s *Server) GetTotalConnections() (n int)

func (*Server) Handler

func (s *Server) Handler() func(ctx *gin.Context)

func (*Server) IsConnected

func (s *Server) IsConnected(connID string) bool

func (*Server) IsJoined

func (s *Server) IsJoined(roomName string, connID string) bool

func (*Server) Join

func (s *Server) Join(roomName string, connID string)

func (*Server) Leave

func (s *Server) Leave(roomName string, connID string) bool

func (*Server) LeaveAll

func (s *Server) LeaveAll(connID string)

func (*Server) OnConnection

func (s *Server) OnConnection(cb ConnectionFunc)

func (*Server) Upgrade

func (s *Server) Upgrade(ctx *gin.Context) Connection

type UnderlineConnection

type UnderlineConnection interface {
	SetWriteDeadline(t time.Time) error
	SetReadDeadline(t time.Time) error
	SetReadLimit(limit int64)
	SetPongHandler(h func(appData string) error)
	SetPingHandler(h func(appData string) error)
	WriteControl(messageType int, data []byte, deadline time.Time) error
	WriteMessage(messageType int, data []byte) error
	ReadMessage() (messageType int, p []byte, err error)
	NextWriter(messageType int) (io.WriteCloser, error)
	Close() error
}

Jump to

Keyboard shortcuts

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