fibersocket

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	TextMessage = 1
	// BinaryMessage denotes a binary data message.
	BinaryMessage = 2
	// CloseMessage denotes a close control message. The optional message
	// payload contains a numeric code and text. Use the FormatCloseMessage
	// function to format a close message payload.
	CloseMessage = 8
	// PingMessage denotes a ping control message. The optional message payload
	// is UTF-8 encoded text.
	PingMessage = 9
	// PongMessage denotes a pong control message. The optional message payload
	// is UTF-8 encoded text.
	PongMessage = 10
)

Source @url:https://github.com/gorilla/websocket/blob/master/conn.go#L61 The message types are defined in RFC 6455, section 11.8.

View Source
const (
	// EventMessage Fired when a Text/Binary message is received
	EventMessage = "message"
	// EventPing More details here:
	// @url https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets
	EventPing = "ping"
	EventPong = "pong"
	// EventDisconnect Fired on disconnection
	// The error provided in disconnection event
	// is defined in RFC 6455, section 11.7.
	// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
	EventDisconnect = "disconnect"
	// EventConnect Fired on first connection
	EventConnect = "connect"
	// EventClose Fired when the connection is actively closed from the server
	EventClose = "close"
	// EventError Fired when some error appears useful also for debugging websockets
	EventError = "error"
)

Supported event list

View Source
const (
	EventMessageId = "1"
	EventPingId    = "2"
	EventPongId    = "3"
	// EventDisconnect Fired on disconnection
	// The error provided in disconnection event
	// is defined in RFC 6455, section 11.7.
	// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
	EventDisconnectId = "4"
	// EventConnect Fired on first connection
	EventConnectId = "5"
	// EventClose Fired when the connection is actively closed from the server
	EventCloseId = "6"
	// EventError Fired when some error appears useful also for debugging websockets
	EventErrorId = "7"
)

support event id

Variables

View Source
var (
	// ErrorInvalidConnection The addressed ws connection is not available anymore
	// error data is the uuid of that connection
	ErrorInvalidConnection = errors.New("message cannot be delivered invalid/gone connection")
	// ErrorUUIDDuplication The UUID already exists in the pool
	ErrorUUIDDuplication = errors.New("UUID already exists in the available connections pool")
)
View Source
var (
	PongTimeout = 1 * time.Second
	// RetrySendTimeout retry after 20 ms if there is an error
	RetrySendTimeout = 20 * time.Millisecond
	//MaxSendRetry define max retries if there are socket issues
	MaxSendRetry = 5
	// ReadTimeout Instead of reading in a for loop, try to avoid full CPU load taking some pause
	ReadTimeout = 10 * time.Millisecond
)

Functions

func Fire added in v0.1.2

func Fire(event string, eventId *string, data []byte)

Fire custom event on all connections

func New

func New(callback func(socket *FiberSocket)) func(*fiber.Ctx) error

Creates a new instance of GoFiberWebSocket

func On

func On(event string, callback eventCallback)

Adds listener callback to an event into the safe listeners list

Types

type Event

type Event struct {
	// contains filtered or unexported fields
}

type EventPayload

type EventPayload struct {
	Fiber            *FiberSocket
	Name             string
	SocketUUID       uuid.UUID
	SocketAttributes map[string]interface{}
	Error            error
	Data             []byte
}

EventPayload holds all information about an event

type Fiber

type Fiber interface {
	IsAlive() bool
	GetUUID() uuid.UUID
	SetUUID(uuid uuid.UUID)
	SetAttribute(key string, attribute interface{})
	GetAttribute(key string) interface{}
	GetIntAttribute(key string) int
	GetStringAttribute(key string) string
	EmitToMany(uuids []uuid.UUID, message []byte, mType ...int)
	EmitTo(uuid uuid.UUID, message []byte, mType ...int) error
	Broadcast(message []byte, except bool, mType ...int)
	Fire(event string, data []byte)
	Emit(message []byte, mType ...int)
	Close()
	// contains filtered or unexported methods
}

type FiberSocket

type FiberSocket struct {
	Mutex sync.RWMutex

	IsConnectionAlive bool
	Done              chan struct{}
	EventQueue        chan Event
	EventAttributes   map[string]interface{}
	UUID              uuid.UUID
	Locals            func(key string) interface{}
	Params            func(key string, defaultValue ...string) string
	Query             func(key string, defaultValue ...string) string
	Cookies           func(key string, defaultValue ...string) string
	// contains filtered or unexported fields
}

func (*FiberSocket) Broadcast

func (fs *FiberSocket) Broadcast(message []byte, except bool, mType ...int)

Broadcast message to all the active connections except avoid broadcasting the message to itself

func (*FiberSocket) Close

func (fs *FiberSocket) Close()

Close Actively close the connection from the server

func (*FiberSocket) Emit

func (fs *FiberSocket) Emit(message []byte, mType ...int)

Emits a message to the client connection

func (*FiberSocket) EmitTo

func (fs *FiberSocket) EmitTo(uuid uuid.UUID, message []byte, mType ...int) error

Handles event event emission to a specific connection socket

func (*FiberSocket) EmitToMany

func (fs *FiberSocket) EmitToMany(uuids []uuid.UUID, message []byte, mType ...int)

EmitToList Emit the message to a specific socket uuids list

func (*FiberSocket) Fire

func (fs *FiberSocket) Fire(event string, data []byte)

Fire custom event

func (*FiberSocket) GetAttribute

func (fs *FiberSocket) GetAttribute(key string) interface{}

GetAttribute Get a specific attribute from the socket attributes

func (*FiberSocket) GetIntAttribute

func (fs *FiberSocket) GetIntAttribute(key string) int

GetIntAttribute Convenience method to retrieve an attribute as an int. Will panic if attribute is not an int.

func (*FiberSocket) GetStringAttribute

func (fs *FiberSocket) GetStringAttribute(key string) string

GetStringAttribute Convenience method to retrieve an attribute as a string. Will panic if attribute is not an int.

func (*FiberSocket) GetUUID

func (socket *FiberSocket) GetUUID() uuid.UUID

Get the UUID of the *FiberSocket safely

func (*FiberSocket) IsAlive

func (fs *FiberSocket) IsAlive() bool

func (*FiberSocket) SetAttribute

func (fs *FiberSocket) SetAttribute(key string, attribute interface{})

SetAttribute Set a specific attribute for the specific socket connection

func (*FiberSocket) SetUUID

func (socket *FiberSocket) SetUUID(uuid uuid.UUID)

type SafeListeners

type SafeListeners struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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