websocket

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteStatus added in v0.1.1

func WriteStatus(conn Conn, status Status, message string, body interface{}) error

Types

type Conn

type Conn interface {
	// ReadJson reads the current input.
	ReadJSON(p interface{}) error

	// WriteJson writes back to the client.
	WriteJSON(p interface{}) error

	// RemoteAddr returns the remote address of the client.
	RemoteAddr() net.Addr

	// Close closes the connection with the client.
	Close() error
}

Conn is the type which represent an connection with the client. grpc.Conn is an example of implementation.

type Key

type Key string

Key is a key representing a job for a connection.

type Status added in v0.1.1

type Status string
var StatusError Status = "error"
var StatusOK Status = "ok"

type StatusBody added in v0.1.1

type StatusBody struct {
	Status  Status      `json:"status"`
	Message string      `json:"message"`
	Body    interface{} `json:"body"`
}

type Store

type Store map[Conn][]Key

Store stores the map between connections and its job keys.

func NewStore

func NewStore() Store

NewStore builds a new store.

func (Store) AddNewJob

func (s Store) AddNewJob(ws Conn) (Key, error)

AddNewJob adds a new job key to a registered websocket.

func (Store) GetWebsocket

func (s Store) GetWebsocket(key Key) (Conn, error)

GetWebsocket provides the websocket associated with the provided job key

func (Store) Register

func (s Store) Register(ws Conn) error

RegisterWebsocket adds a new websocket in the store. It initialize its job key slice.

func (Store) Unregister

func (s Store) Unregister(ws Conn) error

UnregisterWebsocket removes a registered websocket from the store and removes all its job keys

type Websocket

type Websocket interface {

	// Upgrade creates a new connection from the current HTTP context.
	Upgrade(w http.ResponseWriter, r *http.Request,
		h http.Header) (Conn, error)

	// IsClosed checks if the given error is related to a connection closure.
	IsClosed(err error) bool
}

Websocket is responsible for creating new websocket connections.

func NewWebsocket

func NewWebsocket(opt WebsocketOptions) (Websocket, error)

type WebsocketOptions

type WebsocketOptions struct {
	Origins []string
}

Jump to

Keyboard shortcuts

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