Documentation
¶
Index ¶
- Constants
- func DefaultUpgrader() *websocket.Upgrader
- type Adapter
- type BroadcastMsg
- type Config
- type EventHandler
- type RNG
- type RoomMsg
- type Server
- func (serv *Server) Broadcast(eventName string, data any)
- func (serv *Server) BroadcastExcept(except []string, eventName string, data any)
- func (serv *Server) EnableSignalShutdown(complete chan<- bool)
- func (serv *Server) Lock()
- func (serv *Server) On(eventName string, handleFunc func(*Socket, []byte))
- func (serv *Server) OnConnect(handleFunc func(*Socket) error)
- func (serv *Server) OnDisconnect(handleFunc func(*Socket) error)
- func (serv *Server) OnError(handleFunc func(*Socket, error))
- func (serv *Server) OnEvent(h EventHandler)
- func (serv *Server) RoomSocketList(id string) map[string]*Socket
- func (serv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (serv *Server) SetMultihomeBackend(b Adapter)
- func (serv *Server) SetUpgrader(u *websocket.Upgrader)
- func (serv *Server) Shutdown() bool
- func (serv *Server) ShutdownWithSignal()
- func (serv *Server) SocketList() map[string]*Socket
- func (serv *Server) ToRoom(roomName, eventName string, data any)
- func (serv *Server) ToRoomExcept(roomName string, except []string, eventName string, data any)
- func (serv *Server) ToSocket(socketID, eventName string, data any)
- func (serv *Server) Unlock()
- func (serv *Server) WebHandler() http.Handler
- type Socket
- func (s *Socket) Broadcast(eventName string, data any)
- func (s *Socket) BroadcastExcept(except []string, eventName string, data any)
- func (s *Socket) Close() error
- func (s *Socket) Context() storage.IMap[string, any]
- func (s *Socket) Emit(eventName string, data any) error
- func (s *Socket) Get(key string) (any, bool)
- func (s *Socket) GetRooms() []string
- func (s *Socket) ID() string
- func (s *Socket) InRoom(roomName string) bool
- func (s *Socket) Join(roomName string)
- func (s *Socket) Leave(roomName string)
- func (s *Socket) LeaveAll()
- func (s *Socket) Ping() error
- func (s *Socket) Request() *http.Request
- func (s *Socket) Set(key string, val any)
- func (s *Socket) ToRoom(roomName, eventName string, data any)
- func (s *Socket) ToRoomExcept(roomName string, except []string, eventName string, data any)
- func (s *Socket) ToSocket(socketID, eventName string, data any)
Constants ¶
const ( // SubProtocol is the official sacrificial-socket sub protocol SubProtocol string = "sac-sock" )
Variables ¶
This section is empty.
Functions ¶
func DefaultUpgrader ¶
DefaultUpgrader returns a websocket upgrader suitable for creating sacrificial-socket websockets.
Types ¶
type Adapter ¶
type Adapter interface {
Init()
Shutdown() error
BroadcastToBackend(*BroadcastMsg)
RoomcastToBackend(*RoomMsg)
BroadcastFromBackend(b chan<- *BroadcastMsg)
RoomcastFromBackend(r chan<- *RoomMsg)
}
type BroadcastMsg ¶
BroadcastMsg represents an event to be dispatched to all Sockets on the Server
type Config ¶
type Config struct {
WriteBufferPool websocket.BufferPool
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
CheckOrigin func(r *http.Request) bool
Subprotocols []string
HandshakeTimeout time.Duration
ReadBufferSize int
WriteBufferSize int
EnableCompression bool
}
Config specifies parameters for upgrading an HTTP connection to a WebSocket connection.
It is safe to call Config's methods concurrently.
type EventHandler ¶
EventHandler is an interface for registering events using SockerServer.OnEvent
type RNG ¶
type RNG struct {
// contains filtered or unexported fields
}
RNG is a random number generator that is safe for concurrent use by multiple go routines
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server manages the coordination between sockets, rooms, events and the socket hub add my own custom field
func (*Server) BroadcastExcept ¶
BroadcastExcept dispatches an event to all Sockets on the Server.
func (*Server) EnableSignalShutdown ¶
EnableSignalShutdown listens for linux syscalls SIGHUP, SIGINT, SIGTERM, SIGQUIT, SIGKILL and calls the Server.Shutdown() to perform a clean shutdown. true will be passed into complete after the Shutdown proccess is finished
func (*Server) On ¶
On registers event functions to be called on individual Socket connections when the server's socket receives an Emit from the client's socket.
Any event functions registered with On, must be safe for concurrent use by multiple go routines
func (*Server) OnConnect ¶
OnConnect registers an event function to be called whenever a new Socket connection is created
func (*Server) OnDisconnect ¶
OnDisconnect registers an event function to be called as soon as a Socket connection is closed
func (*Server) OnError ¶
OnError registers an event function to be called whenever a new Socket connection is created
func (*Server) OnEvent ¶
func (serv *Server) OnEvent(h EventHandler)
OnEvent has the same functionality as On, but accepts an EventHandler interface instead of a handler function.
func (*Server) ServeHTTP ¶
func (serv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP will upgrade a http request to a websocket using the sac-sock subprotocol
func (*Server) SetMultihomeBackend ¶
SetMultihomeBackend registers an Adapter interface and calls its Init() method
func (*Server) SetUpgrader ¶
SetUpgrader sets the websocket.Upgrader used by the Server.
func (*Server) Shutdown ¶
Shutdown closes all active sockets and triggers the Shutdown() method on any Adapter that is currently set.
func (*Server) ShutdownWithSignal ¶
func (serv *Server) ShutdownWithSignal()
func (*Server) SocketList ¶
func (*Server) ToRoomExcept ¶
ToRoomExcept dispatches an event to all Sockets in the specified room.
func (*Server) WebHandler ¶
WebHandler returns a http.Handler to be passed into http.Handle
Depricated: The Server struct now satisfies the http.Handler interface, use that instead
type Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
Socket represents a websocket connection
func (*Socket) BroadcastExcept ¶
BroadcastExcept dispatches an event to all Sockets on the Server.
func (*Socket) Close ¶
Close closes the Socket connection and removes the Socket from any rooms that it was a member of
func (*Socket) Join ¶
Join adds s to the specified room. If the room does not exist, it will be created
func (*Socket) Leave ¶
Leave removes s from the specified room. If s is not a member of the room, nothing will happen. If the room is empty upon removal of s, the room will be closed
func (*Socket) LeaveAll ¶
func (s *Socket) LeaveAll()
LeaveAll removes s from the specified room. If s is not a member of the room, nothing will happen. If the room is empty upon removal of s, the room will be closed
func (*Socket) ToRoomExcept ¶
ToRoomExcept dispatches an event to all Sockets in the specified room.