Documentation ¶
Overview ¶
Package websocket provides an easy way to setup a rich Websocket server and client side.
Index ¶
- Constants
- func Random(n int) []byte
- func RandomString(n int) string
- type Config
- type Connection
- func (c *Connection) Disconnect() error
- func (c *Connection) Emit(event string, data interface{}) error
- func (c *Connection) EmitError(errorMessage string)
- func (c *Connection) EmitMessage(nativeMessage []byte) error
- func (c *Connection) Get(key string) string
- func (c *Connection) ID() string
- func (c *Connection) Join(roomName string)
- func (c *Connection) Leave(roomName string)
- func (c *Connection) List(room string) []string
- func (c *Connection) On(event string, cb MessageFunc)
- func (c *Connection) OnDisconnect(cb DisconnectFunc)
- func (c *Connection) OnError(cb ErrorFunc)
- func (c *Connection) OnMessage(cb NativeMessageFunc)
- func (c *Connection) Request() *http.Request
- func (c *Connection) Set(key string, value string)
- func (c *Connection) To(to string) Emmiter
- type ConnectionFunc
- type ConnectionIDFunc
- type DisconnectFunc
- type Emmiter
- type ErrorFunc
- type MessageFunc
- type NameSpace
- type NativeMessageFunc
- type Rooms
- type Server
- func (s *Server) Connection(cid string) *Connection
- func (s *Server) Connections() []string
- func (s *Server) Handler() http.Handler
- func (s *Server) Of(namespaceName string) *NameSpace
- func (s *Server) OnConnection(cb ConnectionFunc)
- func (s *Server) Serve()
- func (s *Server) Stop()
- func (s *Server) ToAll() Emmiter
- type UsersRooms
Constants ¶
const ( // DefaultWebsocketWriteTimeout 15 * time.Second DefaultWebsocketWriteTimeout = 15 * time.Second // 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 )
const (
// All is the string which the Emmiter use to send a message to all
All = ""
)
const (
// Version current version number
Version = "0.0.1"
)
Variables ¶
This section is empty.
Functions ¶
func Random ¶
Random takes a parameter (int) and returns random slice of byte ex: var randomstrbytes []byte; randomstrbytes = utils.Random(32)
func RandomString ¶
Types ¶
type Config ¶
type Config struct { Error func(res http.ResponseWriter, req *http.Request, status int, reason error) CheckOrigin func(req *http.Request) bool // we may want to custom the id generate from the request CustomIDFunc ConnectionIDFunc // WriteTimeout time allowed to write a message to the connection. // Default value is 15 * time.Second WriteTimeout 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 with this period. Must be less than PongTimeout // Default value is (PongTimeout * 9) / 10 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. // defaults to false BinaryMessages bool // ReadBufferSize is the buffer size for the underline reader ReadBufferSize int // WriteBufferSize is the buffer size for the underline writer WriteBufferSize int }
Config the websocket server configuration
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
func (*Connection) Disconnect ¶
func (c *Connection) Disconnect() error
func (*Connection) Emit ¶
func (c *Connection) Emit(event string, data interface{}) error
func (*Connection) EmitError ¶
func (c *Connection) EmitError(errorMessage string)
func (*Connection) EmitMessage ¶
func (c *Connection) EmitMessage(nativeMessage []byte) error
func (*Connection) Get ¶
func (c *Connection) Get(key string) string
func (*Connection) ID ¶
func (c *Connection) ID() string
func (*Connection) Join ¶
func (c *Connection) Join(roomName string)
func (*Connection) Leave ¶
func (c *Connection) Leave(roomName string)
func (*Connection) List ¶
func (c *Connection) List(room string) []string
func (*Connection) On ¶
func (c *Connection) On(event string, cb MessageFunc)
func (*Connection) OnDisconnect ¶
func (c *Connection) OnDisconnect(cb DisconnectFunc)
func (*Connection) OnError ¶
func (c *Connection) OnError(cb ErrorFunc)
func (*Connection) OnMessage ¶
func (c *Connection) OnMessage(cb NativeMessageFunc)
func (*Connection) Request ¶
func (c *Connection) Request() *http.Request
func (*Connection) Set ¶
func (c *Connection) Set(key string, value string)
func (*Connection) To ¶
func (c *Connection) To(to string) Emmiter
type ConnectionFunc ¶
type ConnectionFunc func(*Connection)
ConnectionFunc is the callback which fires when a client/connection is connected to the server. Receives one parameter which is the Connection
type ConnectionIDFunc ¶
type DisconnectFunc ¶
type DisconnectFunc func()
DisconnectFunc is the callback which fires when a client/connection closed
type Emmiter ¶
type Emmiter interface { // EmitMessage sends a native websocket message EmitMessage([]byte) error // Emit sends a message on a particular event Emit(string, interface{}) error }
Emmiter is the message/or/event manager
type ErrorFunc ¶
type ErrorFunc (func(string))
ErrorFunc is the callback which fires when an error happens
type MessageFunc ¶
type MessageFunc interface{}
MessageFunc is the second argument to the Emmiter's Emit functions. A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct
type NativeMessageFunc ¶
type NativeMessageFunc func([]byte)
NativeMessageFunc is the callback for native websocket messages, receives one []byte parameter which is the raw client's message
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) Connection ¶
func (s *Server) Connection(cid string) *Connection
func (*Server) Connections ¶
func (*Server) OnConnection ¶
func (s *Server) OnConnection(cb ConnectionFunc)
OnConnection this is the main event you, as developer, will work with each of the websocket connections