Documentation
¶
Index ¶
- Constants
- Variables
- func NewWebSocket(url string, protocals []string) (*websocket.Conn, error)
- func Push(conn *websocket.Conn, message string) error
- type BufferSize
- type Client
- type ClosedHandler
- type ConnectedHandler
- type DataHandler
- type ErrorHandler
- type Handlers
- type Hub
- type Limit
- type Upgrader
- func (upgrader *Upgrader) Broadcast(message []byte)
- func (upgrader *Upgrader) Clients() []uint32
- func (upgrader *Upgrader) Direct(id uint32, message []byte)
- func (upgrader *Upgrader) Online() int
- func (upgrader *Upgrader) SetHandler(handler func([]byte, int) ([]byte, error))
- func (upgrader *Upgrader) SetRouter(r *gin.Engine)
- func (upgrader *Upgrader) Start()
- func (upgrader *Upgrader) Stop()
- func (upgrader *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)
- func (upgrader *Upgrader) UpgradeGin(c *gin.Context, responseHeader http.Header) (*websocket.Conn, error)
- type WSClient
- type WSClientOption
Constants ¶
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 )
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 ¶
var Upgraders = map[string]*Upgrader{}
Upgraders have registered
Functions ¶
func NewWebSocket ¶
NewWebSocket create a new websocket connection
Types ¶
type BufferSize ¶
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 ConnectedHandler ¶
type ConnectedHandler func(option WSClientOption) error
ConnectedHandler 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
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 ¶
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) SetHandler ¶
SetHandler set the message handler
func (*Upgrader) SetRouter ¶
SetRouter upgrades the Gin server connection to the WebSocket protocol.
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.
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) SetProtocols ¶
SetProtocols set the websocket protocols
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