gosocketify

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package gosocketify is a package with server side implementation for socketify. It includes message handling and event dispatching for WebSocket connections.

Package gosocketify is a package with server side implementation for socketify. It provides utilities and middlewares to handle connections and messages.

Package gosocketify is a package with server side implementation for socketify. It provides a router for handling WebSocket connections and HTTP requests.

Package gosocketify is a package with server side implementation for socketify

Package gosocketify is a package with server side implementation for socketify. It includes connection management and event dispatching for WebSocket clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(connectionManager *ConnectionManager) http.Handler

NewRouter creates a new instance of the Router. It initializes the Router with the provided connection manager, sets up the appropriate routes for WebSocket and HTTP, and returns an http.Handler that can be used to handle incoming requests.

Types

type Connection

type Connection struct {
	ID      string
	Socket  *websocket.Conn
	Manager *ConnectionManager
}

Connection struct represents a WebSocket connection. It includes an ID to identify the connection, the actual WebSocket connection, and a reference to the ConnectionManager responsible for managing this connection.

type ConnectionManager

type ConnectionManager struct {
	EventDispatcher EventDispatcher
	// contains filtered or unexported fields
}

ConnectionManager struct manages all active WebSocket connections. It keeps track of connections and facilitates broadcasting and emitting messages to connected clients.

func NewConnectionManager

func NewConnectionManager(eventDispatcher EventDispatcher) *ConnectionManager

NewConnectionManager creates a new instance of ConnectionManager. It initializes an empty connections map and sets the event dispatcher used for handling incoming messages from the connections.

func (*ConnectionManager) AddConnection

func (cm *ConnectionManager) AddConnection(conn *websocket.Conn) *Connection

AddConnection adds a new WebSocket connection to the ConnectionManager. It creates a new Connection instance, assigns it a unique ID, and starts handling messages for that connection in a separate goroutine. Returns the newly created Connection.

func (*ConnectionManager) Broadcast

func (cm *ConnectionManager) Broadcast(message Message)

Broadcast sends a message to all currently connected clients. It iterates through all active connections and attempts to send the message. Errors encountered during sending are logged but do not interrupt the broadcast process.

func (*ConnectionManager) Emit

func (cm *ConnectionManager) Emit(conn *Connection, message Message)

Emit sends a message to a specific connection identified by the Connection instance. If an error occurs while sending the message, it logs the error.

func (*ConnectionManager) RemoveConnection

func (cm *ConnectionManager) RemoveConnection(conn *Connection)

RemoveConnection removes the specified connection from the ConnectionManager. It closes the WebSocket connection and cleans up the underlying resources.

func (*ConnectionManager) ToConnection

func (cm *ConnectionManager) ToConnection(id string, message Message)

ToConnection sends a message to a specific connection using its ID. It searches the active connections for the matching ID and emits the message to the corresponding connection.

type EventDispatcher

type EventDispatcher interface {
	Dispatch(connId string, message Message) // Dispatch a message for a specific connection.
	Broadcast(message string)                // Broadcast a message to all connected clients.
	Send(connectionID any, message string)   // Send a message to a specific connection identified by its ID.
}

EventDispatcher interface defines methods for handling messages and events. It provides methods to dispatch messages to specific connections, broadcast messages to all clients, and send messages to specific connections.

type ExampleEventDispatcher

type ExampleEventDispatcher struct{}

ExampleEventDispatcher is a basic implementation of the EventDispatcher interface. It provides stub functionality for dispatching and handling events.

func (*ExampleEventDispatcher) Dispatch

func (ed *ExampleEventDispatcher) Dispatch(connId string, message Message)

Dispatch processes incoming messages and handles them based on the event type. It contains logic to handle specific events such as "broadcast" and "ping".

type Message

type Message struct {
	Event string      `json:"event"` // The type of event represented by this message.
	Data  interface{} `json:"data"`  // The data payload associated with the event.
}

Message struct represents a message sent over the WebSocket connection. It contains an event type and data associated with that event.

func LoggingMiddleware

func LoggingMiddleware(conn Connection, message Message) Message

LoggingMiddleware logs data received and the connection id.

This middleware is useful for debugging and monitoring purposes. It prints the connection id and the received message to the console.

The message returned is the original message, so you can use this middleware in any position in your pipeline without affecting the original message.

type MiddlewareFunc

type MiddlewareFunc func(Connection, Message) Message

Middleware type definition. A function that takes a connection and a message as input and returns a new message. This function is meant to be used as a pipeline to transform or modify the messages before they are processed by the server.

type Router

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

Router struct handles upgrading a websocket connection with all CORS allowed. It manages incoming WebSocket connections and integrates with a connection manager to keep track of active connections.

Jump to

Keyboard shortcuts

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