ws

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

websocket handler built on top of guerilla/websocket

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a middleman between the websocket connection and the hub.

type DefaultDialer

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

func NewDialer

func NewDialer(addr string) *DefaultDialer

func (*DefaultDialer) Addr

func (dialer *DefaultDialer) Addr() string

func (*DefaultDialer) Send

func (dialer *DefaultDialer) Send(data any) error

Send message in text format to all websocket clients

type HubOption

type HubOption func(*WebsocketHandler)

func NoBroadcast added in v0.0.3

func NoBroadcast() HubOption

func OnMessage

func OnMessage(f func(msg []byte)) HubOption

type WebsocketHandler

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

WebsocketHandler maintains the set of active clients and broadcasts messages to the clients.

WebsocketHandler implements http.Handler interface and can be used directly in your http routes. Don't forget to call the Run method in a seperate go routine.

hub, quit := ws.NewHandler()
defer quit()

go hub.Run()

http.Handle("/", http.FileServer(http.Dir("public")))
http.Handle("/ws", hub)
log.Fatal(http.ListenAndServe(":8080", nil))

func NewHandler

func NewHandler(options ...HubOption) (handler *WebsocketHandler, quit func())

Returns a new websocker hundler. By default, this handler broadcasts all messages to connected clients as in a chat. If you want to handle each message yourself, pass in an OnMessage Option and NoBroadcast option. Then call BroadCastMessage on this handler to send the message to all clients.

func (*WebsocketHandler) BroadCastMessage added in v0.0.3

func (h *WebsocketHandler) BroadCastMessage(message []byte)

send message to all active clients. Client who can't recv are closed and deleted from the client map

func (*WebsocketHandler) Run

func (h *WebsocketHandler) Run()

Infinite loop that runs the hub indefinately. Run this is a go routine.

func (*WebsocketHandler) ServeHTTP

func (hub *WebsocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Http handler

Jump to

Keyboard shortcuts

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