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

websocket

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// WAITING waiting for connecting
	WAITING uint = iota

	// CONNECTING connecting the host
	CONNECTING

	// CONNECTED  the socket is connected
	CONNECTED

	// CLOSED the socket is closed
	CLOSED

	// LISTENING the websocket server is listening
	LISTENING
)
View Source
const (

	// MREAD socket read error ( the local peer closed )
	MREAD uint = iota + 1

	// MBREAK the remote peer closed
	MBREAK

	// MCLOSE user send the CLOSE signal
	MCLOSE
)

Variables

View Source
var Upgraders = map[string]*Upgrader{}

Upgraders have registered

Functions

func NewWebSocket

func NewWebSocket(url string, protocals []string) (*websocket.Conn, error)

NewWebSocket create a new websocket connection

func Push

func Push(conn *websocket.Conn, message string) error

Push a message to websocket connection and get the response message

Types

type BufferSize

type BufferSize struct {
	Read  int `json:"read,omitempty"`
	Write int `json:"write,omitempty"`
}

BufferSize read and write buffer sizes

type Client

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

Client is a middleman between the websocket connection and the hub.

type ClosedHandler

type ClosedHandler func([]byte, error) []byte

ClosedHandler Handler

type ConnectedHandler

type ConnectedHandler func(option WSClientOption) error

ConnectedHandler Handler

type DataHandler

type DataHandler func([]byte, int) ([]byte, error)

DataHandler Handler

type ErrorHandler

type ErrorHandler func(error)

ErrorHandler Handler

type Handlers

type Handlers struct {
	Data      DataHandler
	Error     ErrorHandler
	Closed    ClosedHandler
	Connected ConnectedHandler
}

Handlers the websocket handlers

type Hub

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

Hub maintains the set of active clients and broadcasts messages to the

func (*Hub) AddID

func (h *Hub) AddID(id uint32, message []byte) []byte

AddID add a id to the message 0-4 id, 4~N message eg: [0 0 0 1 49 124...]

func (*Hub) Clients

func (h *Hub) Clients() []uint32

Clients return the online clients

func (*Hub) NextID

func (h *Hub) NextID() uint32

NextID return the next client ID

func (*Hub) Nums

func (h *Hub) Nums() int

Nums count the online client's nums

type Limit

type Limit struct {
	WriteWait  int `json:"write-wait,omitempty"`  // Time allowed to write a message to the peer.
	PongWait   int `json:"pong-wait,omitempty"`   // Time allowed to read the next pong message from the peer.
	MaxMessage int `json:"max-message,omitempty"` // Maximum message size allowed from peer. bytes
	// contains filtered or unexported fields
}

Limit the limit of session

type Upgrader

type Upgrader struct {
	Name        string     `json:"name,omitempty"`
	Description string     `json:"description,omitempty"`
	Version     string     `json:"version,omitempty"`
	Protocols   []string   `json:"protocols,omitempty"`
	Guard       string     `json:"guard,omitempty"`
	Buffer      BufferSize `json:"buffer,omitempty"`
	Limit       Limit      `json:"limit,omitempty"`
	Timeout     int        `json:"timeout,omitempty"`
	Process     string     `json:"process,omitempty"` // serve handler
	// contains filtered or unexported fields
}

Upgrader the upgrader setting

{
		"name": "A Chat WebSocket server",
		"description": "A Chat WebSocket serverr",
		"version": "0.9.2",
		"protocols": ["yao-chat-01"],
		"guard": "bearer-jwt",
		"buffer": { "read": 1024, "write": 1024 },
		"limit": { "read-wait": 5, "pong-wait": 10, "max-message":512 },
		"timeout": 10,
		"process": "flows.websocket.chat",
}

func NewUpgrader

func NewUpgrader(name string, config ...[]byte) (*Upgrader, error)

NewUpgrader create a new WebSocket upgrader

{
		"name": "A Chat WebSocket server",
		"description": "A Chat WebSocket serverr",
		"version": "0.9.2",
		"protocols": ["yao-chat-01"],
		"guard": "bearer-jwt",
		"buffer": { "read": 1024, "write": 1024 },
		"limit": { "read-wait": 5, "pong-wait": 10, "max-message":512 },
		"process": "flows.websocket.chat",
}

func (*Upgrader) Broadcast

func (upgrader *Upgrader) Broadcast(message []byte)

Broadcast broadcast the message

func (*Upgrader) Clients

func (upgrader *Upgrader) Clients() []uint32

Clients return the online clients

func (*Upgrader) Direct

func (upgrader *Upgrader) Direct(id uint32, message []byte)

Direct send the message to the client directly

func (*Upgrader) Online

func (upgrader *Upgrader) Online() int

Online count the online client's nums

func (*Upgrader) SetHandler

func (upgrader *Upgrader) SetHandler(handler func([]byte, int) ([]byte, error))

SetHandler set the message handler

func (*Upgrader) SetRouter

func (upgrader *Upgrader) SetRouter(r *gin.Engine)

SetRouter upgrades the Gin server connection to the WebSocket protocol.

func (*Upgrader) Start

func (upgrader *Upgrader) Start()

Start the hub

func (*Upgrader) Stop

func (upgrader *Upgrader) Stop()

Stop the hub

func (*Upgrader) Upgrade

func (upgrader *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)

Upgrade upgrades the HTTP server connection to the WebSocket protocol.

The responseHeader is included in the response to the client's upgrade request. Use the responseHeader to specify cookies (Set-Cookie). To specify subprotocols supported by the server, set Upgrader.Subprotocols directly.

If the upgrade fails, then Upgrade replies to the client with an HTTP error response.

func (*Upgrader) UpgradeGin

func (upgrader *Upgrader) UpgradeGin(c *gin.Context, responseHeader http.Header) (*websocket.Conn, error)

UpgradeGin upgrades the Gin server connection to the WebSocket protocol.

type WSClient

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

WSClient the websocket client

func NewWSClient

func NewWSClient(option WSClientOption, handlers Handlers) *WSClient

NewWSClient create a new webocket client connection

func (*WSClient) Close

func (ws *WSClient) Close() error

Close the connection

func (*WSClient) Open

func (ws *WSClient) Open() error

Open the websockt connetion

func (*WSClient) SetProtocols

func (ws *WSClient) SetProtocols(Protocols ...string)

SetProtocols set the websocket protocols

func (*WSClient) SetURL

func (ws *WSClient) SetURL(url string)

SetURL set the websocket url

func (*WSClient) Write

func (ws *WSClient) Write(message []byte) error

Write messge

type WSClientOption

type WSClientOption struct {
	Name         string     `json:"name,omitempty"`
	Description  string     `json:"description,omitempty"`
	Version      string     `json:"version,omitempty"`
	URL          string     `json:"url,omitempty"`
	Protocols    []string   `json:"protocols,omitempty"`
	Guard        string     `json:"guard,omitempty"`
	Buffer       BufferSize `json:"buffer,omitempty"`
	Timeout      int        `json:"timeout,omitempty"`
	Ping         int        `json:"ping,omitempty"`
	KeepAlive    int        `json:"keep,omitempty"`          // -1 not keep alive, 0 keep alive always, keep alive n seconds.
	AttemptAfter int        `json:"attempt_after,omitempty"` // Attempt attempt_after
	Attempts     int        `json:"attempts,omitempty"`      // max times try to reconnect server when connection break (client mode only)
	Timestamp    int        `json:"timestamp,omitempty"`
	IP           string     `json:"ip,omitempty"`
	Port         int        `json:"port,omitempty"`
}

WSClientOption the webocket client option

Jump to

Keyboard shortcuts

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