gowse

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 6 Imported by: 1

README

gowse

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Infof(format string, v ...interface{})
	Errorf(format string, v ...interface{})
}

Logger defines the shape of the component needed by the gowse server and topics to log info and errors.

type Message

type Message struct {
	Text string `json:"text"`
}

Message ...

type Server

type Server struct {
	Topics map[string]*Topic
	// contains filtered or unexported fields
}

Server allows to create topics that clients can subscribe to.

func NewServer

func NewServer(l Logger) *Server

NewServer creates and initializes a gowse Server. If the logger argument is nil the logs written by the server will be discarded.

func (*Server) CreateTopic

func (s *Server) CreateTopic(ID string) *Topic

CreateTopic creates a topic where clients can connect to receive messages.

func (*Server) Stop

func (s *Server) Stop()

Stop signals all the topics created by the server to finish and blocks the calling goroutine until all the Topics have finished. The caller must ensure all the calls to topic.TopicHandler had completed and no futher calls to that method will be made. That usually means the http server receiving the client subscritions has already been stopped.

type Subscriber

type Subscriber struct {
	ID string
	// contains filtered or unexported fields
}

Subscriber represents a client subscribed to a topic.

func NewSubscriber

func NewSubscriber(w http.ResponseWriter, r *http.Request, t *Topic) (*Subscriber, error)

NewSubscriber creates a subscriber by upgrading the http request to a websocket.

func (*Subscriber) Close

func (s *Subscriber) Close()

Close closes the underlaying connection with the subscriber.

func (*Subscriber) Monitor

func (s *Subscriber) Monitor(closed chan<- *Subscriber)

Monitor detects when a subscriber closed a connection.

func (*Subscriber) SendMessage

func (s *Subscriber) SendMessage(msg interface{}) error

SendMessage sends a message to the subscriber.

type Topic

type Topic struct {
	ID string
	// contains filtered or unexported fields
}

Topic represents and endpoint where multiple clients can subscribe to receive the broadcasted messages.

func NewTopic

func NewTopic(ctx context.Context, ID string, l Logger) *Topic

NewTopic creates a new topic given and ID, a logger and a context.

func (*Topic) Broadcast

func (t *Topic) Broadcast(message interface{})

Broadcast sends a messages to the all the clients subscribed to the topic. Calling broadcast after canceling the context passed to the server that created the topic causes panic.

func (*Topic) Process

func (t *Topic) Process(wg *sync.WaitGroup)

Process starts the topic to accept new subscribers and to broadcast messages.

func (*Topic) SubscriberHandler

func (t *Topic) SubscriberHandler(w http.ResponseWriter, r *http.Request) error

SubscriberHandler is called when a new subscriber connects to the topic.

Jump to

Keyboard shortcuts

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