astiws

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2019 License: MIT Imports: 9 Imported by: 4

README

Example

Run the 2 following commands at the same time :

$ go run example/manager/main.go -v

$ go run example/client/main.go -v

Documentation

Index

Constants

View Source
const (
	EventNameDisconnect = "astiws.disconnect"
	PingPeriod          = (pingWait * 9) / 10
)

Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyMessage

type BodyMessage struct {
	EventName string      `json:"event_name"`
	Payload   interface{} `json:"payload"`
}

BodyMessage represents the body of a message

type BodyMessageRead

type BodyMessageRead struct {
	BodyMessage
	Payload json.RawMessage `json:"payload"`
}

BodyMessageRead represents the body of a message for read purposes Indeed when reading the body, we need the payload to be a json.RawMessage

type Client

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

Client represents a websocket client

func NewClient

func NewClient(c ClientConfiguration) *Client

NewClient creates a new client

func NewClientWithContext added in v1.0.1

func NewClientWithContext(ctx context.Context, cfg ClientConfiguration) (c *Client)

NewClientWithContext creates a new client with a context

func (*Client) AddListener

func (c *Client) AddListener(eventName string, f ListenerFunc)

AddListener adds a listener

func (*Client) Close

func (c *Client) Close() (err error)

Close closes the client properly

func (*Client) Context added in v1.0.1

func (c *Client) Context() context.Context

Context return the client's context

func (*Client) DelListener

func (c *Client) DelListener(eventName string)

DelListener deletes a listener

func (*Client) Dial

func (c *Client) Dial(addr string) error

Dial dials an addr

func (*Client) DialWithHeaders

func (c *Client) DialWithHeaders(addr string, h http.Header) (err error)

DialWithHeader dials an addr with specific headers

func (*Client) ExtendConnection added in v1.0.1

func (c *Client) ExtendConnection() error

ExtendConnection extends the connection

func (*Client) Read

func (c *Client) Read() error

Read reads from the client

func (*Client) SetListener

func (c *Client) SetListener(eventName string, f ListenerFunc)

SetListener sets a listener

func (*Client) SetMessageHandler added in v1.0.1

func (c *Client) SetMessageHandler(h MessageHandler)

SetMessageHandler sets the message handler

func (*Client) WithContext added in v1.0.1

func (c *Client) WithContext(ctx context.Context) *Client

WithContext updates the client's context

func (*Client) Write

func (c *Client) Write(eventName string, payload interface{}) error

Write writes a message to the client

func (*Client) WriteJSON added in v1.0.1

func (c *Client) WriteJSON(m interface{}) (err error)

WriteJSON writes a JSON message to the client

func (*Client) WriteText added in v1.0.1

func (c *Client) WriteText(m []byte) (err error)

WriteText writes a text message to the client

type ClientAdapter

type ClientAdapter func(c *Client) error

ClientAdapter represents a client adapter func

type ClientConfiguration

type ClientConfiguration struct {
	MaxMessageSize int `toml:"max_message_size"`
}

ClientConfiguration represents a client configuration

type ListenerFunc

type ListenerFunc func(c *Client, eventName string, payload json.RawMessage) error

ListenerFunc represents a listener callback

type Manager

type Manager struct {
	Upgrader websocket.Upgrader
	// contains filtered or unexported fields
}

Manager represents a websocket manager

func NewManager

func NewManager(c ManagerConfiguration) *Manager

NewManager creates a new manager

func (*Manager) AutoRegisterClient

func (m *Manager) AutoRegisterClient(c *Client)

AutoRegisterClient auto registers a new client

func (*Manager) AutoUnregisterClient added in v1.0.1

func (m *Manager) AutoUnregisterClient(c *Client)

AutoUnregisterClient auto unregisters a client

func (*Manager) Client

func (m *Manager) Client(k interface{}) (c *Client, ok bool)

Client returns the client stored with the specific key

func (*Manager) Clients

func (m *Manager) Clients(fn func(k interface{}, c *Client) error)

Clients executes a function on every client. It stops if an error is returned.

func (*Manager) Close

func (m *Manager) Close() error

Close implements the io.Closer interface

func (*Manager) CountClients

func (m *Manager) CountClients() int

CountClients returns the number of connected clients

func (*Manager) Loop

func (m *Manager) Loop(fn func(k interface{}, c *Client))

Loop loops in clients and execute a function for each of them

func (*Manager) RegisterClient

func (m *Manager) RegisterClient(k interface{}, c *Client)

RegisterClient registers a new client

func (*Manager) ServeHTTP

func (m *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request, a ClientAdapter) (err error)

ServeHTTP handles an HTTP request and returns an error unlike an http.Handler We don't want to register the client yet, since we may want to index the map of clients with an information we don't have yet

func (*Manager) UnregisterClient

func (m *Manager) UnregisterClient(k interface{})

UnregisterClient unregisters a client astiws.disconnected event is a good place to call this function

type ManagerConfiguration

type ManagerConfiguration struct {
	CheckOrigin    func(r *http.Request) bool `toml:"-"`
	MaxMessageSize int                        `toml:"max_message_size"`
}

ManagerConfiguration represents a manager configuration

type MessageHandler added in v1.0.1

type MessageHandler func(m []byte) error

MessageHandler represents a message handler

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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