ws

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2017 License: MIT Imports: 7 Imported by: 0

README

Package ws

Project status Build Status Coverage Status Go Report Card GoDoc License

Package ws creates a hub for WebSocket connections and abstracts away allot of the boilerplate code for managing connections using Gorilla WebSocket. The design of this library was inspired by Dave Cheney's Talk on first Class Functions

Installation

go get -u github.com/go-playground/ws

Examples

Contributing

Make a pull request

Package Versioning

I'm jumping on the vendoring bandwagon, you should vendor this package as I will not be creating different version with gopkg.in like allot of my other libraries.

Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE, it is so freeing not to worry about it and will help me keep pouring out bigger and better things for you the community.

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Write([]byte)
	Listen()
	Close()
}

Client is interface for the client type

func NewClient

func NewClient(h *Hub, conn *websocket.Conn, readFn ReadFn) Client

NewClient returns a new instance of the default client.

type ClientFn

type ClientFn func(*Hub, *websocket.Conn, *http.Request) Client

ClientFn is the Client creation function callback

type Connections

type Connections map[Client]*websocket.Conn

Connections is a map of Client interfaces to their corresponding WebSocket connection

type Hub

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

Hub is WebSocket hub instance

func New

func New(upgrader websocket.Upgrader, fn ClientFn) *Hub

New returns a new websocket hub instance. NOTE: if fn is nil, the default Client will be used and the default read function will Broadcast the message to all connected users.

func (*Hub) Accept

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

Accept starts the WebSocket connection and adds connection to the pool of clients.

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg []byte)

Broadcast sends a message to all currently registered connections

func (*Hub) BroadcastTo

func (h *Hub) BroadcastTo(msg []byte, filter func(Client) bool)

BroadcastTo sends a message to the provided clients only

func (*Hub) Do

func (h *Hub) Do(fn func(conns Connections))

Do executes the provided function in sequence and is thread safe. It allows external logic to be executed from within Hub.

func (*Hub) PingInterval

func (h *Hub) PingInterval() time.Duration

PingInterval returns the ping interval for WebSocket ping-pong keepalives.

func (*Hub) ReadDeadline

func (h *Hub) ReadDeadline() time.Time

ReadDeadline returns the deadline for connection reads

func (*Hub) ReadLimit

func (h *Hub) ReadLimit() int64

ReadLimit returns the read message limit

func (*Hub) Remove

func (h *Hub) Remove(c Client)

Remove removes the provided client from the Hub NOTE: this does not close the connection on your behalf but rather just removes if from being tracked by the Hub, it is your own responsibility to close your own client, and is usually done within the Client's Close() function transparently.

func (*Hub) SetPongWait

func (h *Hub) SetPongWait(d time.Duration)

SetPongWait sets the pong wait timeout and automatically sets the ping period based off of the duration provided. The WebSocket's ReadDeadline will also be set to the same value.

func (*Hub) SetReadLimit

func (h *Hub) SetReadLimit(size int64)

SetReadLimit sets the limit, in bytes, for read operations

func (*Hub) SetWriteWait

func (h *Hub) SetWriteWait(d time.Duration)

SetWriteWait sets the wait timeout for WebSocket writes.

func (*Hub) Shutdown

func (h *Hub) Shutdown()

Shutdown finishes all actions queued prior to calling Shutdown, closes all client connections and then stops the Hub

func (*Hub) WriteDeadline

func (h *Hub) WriteDeadline() time.Time

WriteDeadline returns the deadline for connection writes

type ReadFn

type ReadFn func([]byte)

ReadFn is the function to be called directly after a read operation by the default client.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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