sio

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// SubProtocol is the official sacrificial-socket sub protocol
	SubProtocol string = "sac-sock"
)

Variables

This section is empty.

Functions

func DefaultUpgrader

func DefaultUpgrader() *websocket.Upgrader

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

type BroadcastMsg struct {
	Data      any
	EventName string
	Except    []string
}

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

type EventHandler interface {
	HandleEvent(*Socket, []byte)
	EventName() string
}

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

func NewRNG

func NewRNG() *RNG

NewRNG creates a new random number generator

func (*RNG) Read

func (r *RNG) Read(b []byte) (int, error)

Read reads len(b) random bytes into b and always returns a nil error

type RoomMsg

type RoomMsg struct {
	Data      any
	RoomName  string
	EventName string
	Except    []string
}

RoomMsg represents an event to be dispatched to a room of sockets

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 New

func New(cfg ...Config) *Server

New creates a new instance of Server

func (*Server) Broadcast

func (serv *Server) Broadcast(eventName string, data any)

Broadcast dispatches an event to all Sockets on the Server.

func (*Server) BroadcastExcept

func (serv *Server) BroadcastExcept(except []string, eventName string, data any)

BroadcastExcept dispatches an event to all Sockets on the Server.

func (*Server) EnableSignalShutdown

func (serv *Server) EnableSignalShutdown(complete chan<- bool)

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) Lock

func (serv *Server) Lock()

func (*Server) On

func (serv *Server) On(eventName string, handleFunc func(*Socket, []byte))

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

func (serv *Server) OnConnect(handleFunc func(*Socket) error)

OnConnect registers an event function to be called whenever a new Socket connection is created

func (*Server) OnDisconnect

func (serv *Server) OnDisconnect(handleFunc func(*Socket) error)

OnDisconnect registers an event function to be called as soon as a Socket connection is closed

func (*Server) OnError

func (serv *Server) OnError(handleFunc func(*Socket, error))

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) RoomSocketList

func (serv *Server) RoomSocketList(id string) map[string]*Socket

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

func (serv *Server) SetMultihomeBackend(b Adapter)

SetMultihomeBackend registers an Adapter interface and calls its Init() method

func (*Server) SetUpgrader

func (serv *Server) SetUpgrader(u *websocket.Upgrader)

SetUpgrader sets the websocket.Upgrader used by the Server.

func (*Server) Shutdown

func (serv *Server) Shutdown() bool

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 (serv *Server) SocketList() map[string]*Socket

func (*Server) ToRoom

func (serv *Server) ToRoom(roomName, eventName string, data any)

ToRoom dispatches an event to all Sockets in the specified room.

func (*Server) ToRoomExcept

func (serv *Server) ToRoomExcept(roomName string, except []string, eventName string, data any)

ToRoomExcept dispatches an event to all Sockets in the specified room.

func (*Server) ToSocket

func (serv *Server) ToSocket(socketID, eventName string, data any)

ToSocket dispatches an event to the specified socket ID.

func (*Server) Unlock

func (serv *Server) Unlock()

func (*Server) WebHandler

func (serv *Server) WebHandler() http.Handler

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) Broadcast

func (s *Socket) Broadcast(eventName string, data any)

Broadcast dispatches an event to all Sockets on the Server.

func (*Socket) BroadcastExcept

func (s *Socket) BroadcastExcept(except []string, eventName string, data any)

BroadcastExcept dispatches an event to all Sockets on the Server.

func (*Socket) Close

func (s *Socket) Close() error

Close closes the Socket connection and removes the Socket from any rooms that it was a member of

func (*Socket) Context

func (s *Socket) Context() storage.IMap[string, any]

Context gets value

func (*Socket) Emit

func (s *Socket) Emit(eventName string, data any) error

Emit dispatches an event to s.

func (*Socket) Get

func (s *Socket) Get(key string) (any, bool)

Get gets value

func (*Socket) GetRooms

func (s *Socket) GetRooms() []string

GetRooms returns a list of rooms that s is a member of

func (*Socket) ID

func (s *Socket) ID() string

ID returns the unique ID of s

func (*Socket) InRoom

func (s *Socket) InRoom(roomName string) bool

InRoom returns true if s is currently a member of roomName

func (*Socket) Join

func (s *Socket) Join(roomName string)

Join adds s to the specified room. If the room does not exist, it will be created

func (*Socket) Leave

func (s *Socket) Leave(roomName string)

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) Ping

func (s *Socket) Ping() error

func (*Socket) Request

func (s *Socket) Request() *http.Request

Request get request

func (*Socket) Set

func (s *Socket) Set(key string, val any)

Set get request

func (*Socket) ToRoom

func (s *Socket) ToRoom(roomName, eventName string, data any)

ToRoom dispatches an event to all Sockets in the specified room.

func (*Socket) ToRoomExcept

func (s *Socket) ToRoomExcept(roomName string, except []string, eventName string, data any)

ToRoomExcept dispatches an event to all Sockets in the specified room.

func (*Socket) ToSocket

func (s *Socket) ToSocket(socketID, eventName string, data any)

ToSocket dispatches an event to the specified socket ID.

Jump to

Keyboard shortcuts

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