Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultIDGenerator(r *http.Request) string
- type Config
- type Connection
- type ConnectionFunc
- type DisconnectFunc
- type ErrorFunc
- type Masters
- type RowConn
- type SampleConn
- type Server
- func (s *Server) Broadcast(topic string, msg []byte, connID string) error
- func (s *Server) CancelAll(id string)
- func (s *Server) CancelSubscribe(topic, id string)
- func (s *Server) Disconnect(id string)
- func (s *Server) GetConnection(id string) SampleConn
- func (s *Server) GetConnections() []Connection
- func (s *Server) GetConnectionsByTopic(topic string) []Connection
- func (s *Server) ID() string
- func (s *Server) IsConnected(id string) bool
- func (s *Server) IsSubscribe(topic string, id string) bool
- func (s *Server) LenConnections() (n int)
- func (s *Server) OnConnection(cb ConnectionFunc)
- func (s *Server) Subscribe(topic string, id string)
- func (s *Server) Upgrade(w http.ResponseWriter, r *http.Request) (Connection, error)
Constants ¶
View Source
const ( // DefaultWebsocketWriteTimeout 0, no timeout DefaultWebsocketWriteTimeout = 0 // DefaultWebsocketReadTimeout 0, no timeout DefaultWebsocketReadTimeout = 0 // DefaultWebsocketPongTimeout 60 * time.Second DefaultWebsocketPongTimeout = 60 * time.Second // DefaultWebsocketPingPeriod (DefaultPongTimeout * 9) / 10 DefaultWebsocketPingPeriod = (DefaultWebsocketPongTimeout * 9) / 10 // DefaultWebsocketMaxMessageSize 1024 DefaultWebsocketMaxMessageSize = 1024 // DefaultWebsocketReadBufferSize 4096 DefaultWebsocketReadBufferSize = 4096 // DefaultWebsocketWriterBufferSize 4096 DefaultWebsocketWriterBufferSize = 4096 // DefaultEvtMessageKey is the default prefix of the underline websocket events // that are being established under the hoods. // // Last character of the prefix should be ':'. DefaultEvtMessageKey = "ws" )
View Source
const (
Version = "v0.2.4"
)
Variables ¶
Functions ¶
func DefaultIDGenerator ¶ added in v0.2.0
DefaultIDGenerator returns a random unique for a new connection. Used when config.IDGenerator is nil.
Types ¶
type Config ¶ added in v0.2.0
type Config struct {
// the server id
ID string
// IDGenerator used to create (and later on, set)
// an ID for each incoming websocket connections (clients).
// The request is an input parameter which you can use to generate the ID (from headers for example).
// If empty then the ID is generated by DefaultIDGenerator: randomString(64)
IDGenerator func(r *http.Request) string
// record the url address of the superior masters
SuperiorMaster []string
// record the url address of the lateral masters
LateralMaster []string
EnableCluster bool
// EvtMessagePrefix is the prefix of the underline websocket events that are being established under the hoods.
// This prefix is visible only to the javascript side (code) and it has nothing to do
// with the message that the end-user receives.
// Do not change it unless it is absolutely necessary.
//
// If empty then defaults to []byte("ws").
EvtMessagePrefix []byte
NewConn func(server *Server, w http.ResponseWriter, r *http.Request) (Connection, error)
// Error is the function that will be fired if any client couldn't upgrade the HTTP connection
// to a websocket connection, a handshake error.
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
// CheckOrigin a function that is called right before the handshake,
// if returns false then that client is not allowed to connect with the websocket server.
CheckOrigin func(r *http.Request) bool
// HandshakeTimeout specifies the duration for the handshake to complete.
HandshakeTimeout time.Duration
// WriteTimeout time allowed to write a message to the connection.
// 0 means no timeout.
// Default value is 0
WriteTimeout time.Duration
// ReadTimeout time allowed to read a message from the connection.
// 0 means no timeout.
// Default value is 0
ReadTimeout time.Duration
// PongTimeout allowed to read the next pong message from the connection.
// Default value is 60 * time.Second
PongTimeout time.Duration
// PingPeriod send ping messages to the connection within this period. Must be less than PongTimeout.
// Default value is 60 *time.Second
PingPeriod time.Duration
// MaxMessageSize max message size allowed from connection.
// Default value is 1024
MaxMessageSize int64
// BinaryMessages set it to true in order to denotes binary data messages instead of utf-8 text
// compatible if you wanna use the Connection's EmitMessage to send a custom binary data to the client, like a native server-client communication.
// Default value is false
BinaryMessages bool
// ReadBufferSize is the buffer size for the connection reader.
// Default value is 4096
ReadBufferSize int
// WriteBufferSize is the buffer size for the connection writer.
// Default value is 4096
WriteBufferSize int
// EnableCompression specify if the server should attempt to negotiate per
// message compression (RFC 7692). Setting this value to true does not
// guarantee that compression will be supported. Currently only "no context
// takeover" modes are supported.
//
// Defaults to false and it should be remain as it is, unless special requirements.
EnableCompression bool
// Subprotocols specifies the server's supported protocols in order of
// preference. If this field is set, then the Upgrade method negotiates a
// subprotocol by selecting the first match in this list with a protocol
// requested by the client.
Subprotocols []string
}
Config the websocket server configuration all of these are optional.
type Connection ¶ added in v0.2.0
type Connection interface {
// ID returns the connection's identifier
ID() string
Request() *http.Request
// Server returns the websocket server instance
// which this connection is listening to.
//
// Its connection-relative operations are safe for use.
Server() *Server
// OnDisconnect registers a callback which is fired when this connection is closed by an error or manual
OnDisconnect(DisconnectFunc) message.Subscriber
// OnError registers a callback which fires when this connection occurs an error
OnError(ErrorFunc) message.Subscriber
// OnPing registers a callback which fires on each ping
// It does nothing more than firing the OnError listeners. It doesn't send anything to the client.
FireOnError(err error)
// To defines on which "topic" the server should send a message
// returns an Publisher to send messages.
// Broadcast to any node which subscribe this topic.
// 发布给订阅主题的所有节点
Publisher(string) message.Publisher
// On registers a callback to a particular topic which is fired when a message to this topic is received
// just for topic with prefix '/inner'
// 注册某些话题的回调事件
On(topic string, callback message.Func) message.Subscriber
// only send to the node of this conn
Echo(topic string, data interface{})
// subscribe registers this connection to a topic, if it doesn't exist then it creates a new.
// One topic can have one or more connections. One connection can subscribe many topics.
// All connections subscribe a topic specified by their `ID` automatically.
// 代替节点订阅主题, 可以配置节点是否有权限订阅主题
Subscribe(string)
IsSubscribe(string) bool
// Leave removes this connection entry from a room
// Returns true if the connection has actually left from the particular room.
CancelSubscribe(string)
// Wait starts the ping and the messages reader,
// it's named as "Wait" because it should be called LAST,
// after the "Subscribe" events IF server's `Upgrade` is used,
// otherwise you don't have to call it because the `Handler()` does it automatically.
Wait()
// Disconnect disconnects the client, close the underline websocket conn and removes it from the conn list
// returns the error, if any, from the underline connection
io.Closer
io.Writer
// 0: client 1: lateral 2: superior
Type() uint
Alive() bool
}
Connection is the front-end API that you will use to communicate with the client side
func NewConn ¶ added in v0.2.2
func NewConn(s *Server, w http.ResponseWriter, r *http.Request) (Connection, error)
type ConnectionFunc ¶ added in v0.2.0
type ConnectionFunc func(Connection) error
type DisconnectFunc ¶ added in v0.2.0
DisconnectFunc is the callback which is fired when a client/connection closed
type SampleConn ¶ added in v0.2.4
type Server ¶ added in v0.2.0
type Server struct {
// contains filtered or unexported fields
}
func (*Server) CancelSubscribe ¶ added in v0.2.0
func (*Server) Disconnect ¶ added in v0.2.0
func (*Server) GetConnection ¶ added in v0.2.0
func (s *Server) GetConnection(id string) SampleConn
func (*Server) GetConnections ¶ added in v0.2.0
func (s *Server) GetConnections() []Connection
func (*Server) GetConnectionsByTopic ¶ added in v0.2.0
func (s *Server) GetConnectionsByTopic(topic string) []Connection
func (*Server) IsConnected ¶ added in v0.2.0
func (*Server) IsSubscribe ¶ added in v0.2.0
func (*Server) LenConnections ¶ added in v0.2.0
func (*Server) OnConnection ¶ added in v0.2.0
func (s *Server) OnConnection(cb ConnectionFunc)
OnConnection 当有新连接生成时触发
func (*Server) Upgrade ¶ added in v0.2.0
func (s *Server) Upgrade(w http.ResponseWriter, r *http.Request) (Connection, error)
Click to show internal directories.
Click to hide internal directories.
