wshandler

package module
v0.0.2 Latest Latest
Warning

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

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

README

GoDoc

Simple Websocket Handler

Publish messages to all connected clients

wsh := wshandler.NewWsHandler(func(s *wshandler.Subscriber) {
	// on new connection
}, true)
http.HandleFunc("/ws", wsh.SubscribeHandler)
go http.ListenAndServe(":8000", nil)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Subscriber

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

Subscriber represents a subscriber. Messages are sent on the msgs channel and if the client cannot keep up with the messages, closeSlow is called.

type WsHandler added in v0.0.2

type WsHandler struct {
	OnNewSubscriber func(s *Subscriber)
	AcceptOptions   *websocket.AcceptOptions
	// contains filtered or unexported fields
}

WsHandler enables broadcasting to a set of subscribers.

func NewWsHandler

func NewWsHandler() *WsHandler

NewWsHandler constructs a WsHandler with the defaults.

func (*WsHandler) EnableDebugMode added in v0.0.2

func (ws *WsHandler) EnableDebugMode()

EnableDebugMode set the AcceptOptions.InsecureSkipVerify = true

func (*WsHandler) HasSubscriber added in v0.0.2

func (ws *WsHandler) HasSubscriber() bool

HasSubscriber returns true if there a webclients subscribed, false otherwise

func (*WsHandler) Publish added in v0.0.2

func (ws *WsHandler) Publish(msg []byte)

Publish publishes the msg to all subscribers. It never blocks and so messages to slow subscribers are dropped.

func (*WsHandler) PublishJSON added in v0.0.2

func (ws *WsHandler) PublishJSON(v interface{})

PublishJSON published as json, in calls json.Marshal internally

func (*WsHandler) PublishTo added in v0.0.2

func (ws *WsHandler) PublishTo(s *Subscriber, msg []byte)

PublishTo publishes the msg to a single subscribers.

func (*WsHandler) PublishToJSON added in v0.0.2

func (ws *WsHandler) PublishToJSON(s *Subscriber, v interface{})

PublishJSON published to a single subscriber as json, in calls json.Marshal internally

func (*WsHandler) SubscribeHandler added in v0.0.2

func (ws *WsHandler) SubscribeHandler(w http.ResponseWriter, r *http.Request)

SubscribeHandler accepts the WebSocket connection and then subscribes it to all future messages.

Jump to

Keyboard shortcuts

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