hub

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 8 Imported by: 0

README

Hub

Terms:

  • Topic: The high level set of conversations around a group of common channels (e.g. chatroom)
  • Room: The identifier for the single instance of a topic (e.g. 42 for the room ID of 42)
  • Channel: The combination of the topic and room (e.g. chatroom:42)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanConn

type ChanConn struct {
	Conn *Conn

	Topic string
	Room  string
}

ChanConn is the connection given to the topic handlers it contains the connection information and the context of the current channel

func (ChanConn) GetChannel

func (c ChanConn) GetChannel() string

GetChannel gets the fully named channel in the form of "<topic>:<room>"

func (ChanConn) SendJSON

func (c ChanConn) SendJSON(event string, payload interface{}) error

SendJSON sends a message to the client connection

type Conn

type Conn struct {
	WS       *websocket.Conn
	Channels map[string]bool
	InitData map[string]interface{}
	// contains filtered or unexported fields
}

Conn is the wrapped connection for a client

func (*Conn) SendJSON

func (c *Conn) SendJSON(topic, room, event string, payload interface{}) (err error)

SendJSON sends a message to the client connection

type ConnValidHandler

type ConnValidHandler func(*http.Request) (string, interface{}, error)

ConnValidHandler is a callback function for the pre-connect validation step to allow users to specify a handler that allows or denys the wescoket connection based on the request.

type Hub

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

Hub is the struct for the main Hub

func New

func New(opts *Opts) (h *Hub)

New creates a new hub and starts the ping service to keep connections alive

func (*Hub) AddConnValidHandler

func (h *Hub) AddConnValidHandler(handle ConnValidHandler)

AddConnValidHandler registers a new connection validation handler

func (*Hub) AddTopicHandler

func (h *Hub) AddTopicHandler(topic string, handler TopicHandler)

AddTopicHandler registers a new TopicHandler with the topic name

func (*Hub) ServeHTTP

func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the HTTP handler for WebSocket connections to go to

type Opts

type Opts struct {
	PongWait       *time.Duration
	PingPeriod     *time.Duration
	MaxMessageSize int64
}

Opts is the struct for options passed into the hub

type TopicHandler

type TopicHandler interface {
	Join(conn ChanConn, msg interface{}) error
	Receive(conn ChanConn, event string, msg interface{})
	Leave(conn ChanConn)
}

TopicHandler is the interface that must be implemented to register a new topic handler with the Hub for processing messages

Jump to

Keyboard shortcuts

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