ws

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Internal package for handling websocket connections and dispatching updates to clients.

This package is used in the following way:

  1. Create a new WebSocket type with NewWebSocket().
  2. Start the WebSocket with Start().
  3. Stop the WebSocket with Stop().
  4. Dispatch updates to clients with DispatchUpdate().

No need to call these methods exclusively, they are automatically called and are executed synchronously in the socketeer.go file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WebSocket

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

WebSocket is an interface for handling websocket connections.

  • clients is a map of websocket connections.
  • clientsMux is a mutex for clients for thread safety.

func NewWebSocket

func NewWebSocket() *WebSocket

NewWebSocket returns a new WebSocket.

This method is utilized to create a new WebSocket type and the clients map is initialized which is initially empty.

Example:

conn := ws.NewWebSocket()

func (*WebSocket) DispatchUpdate

func (w *WebSocket) DispatchUpdate(update []byte)

DispatchUpdate dispatches an update to all clients as a websocket message in the form of a byte slice.

This method is called internally when an update is received from the database.

Parameters:

  • update ([]byte): the update to dispatch to clients.

Example:

ws.DispatchUpdate([]byte("Hello, world!"))

func (*WebSocket) Start

func (w *WebSocket) Start(host string, endpoint string)

Start starts the https server and calls the websocketHandler method when a connection is made to upgrade the connection to a websocket connection.

This method is called internally when the socketeer is started.

Parameters:

  • host (string): the host address to listen on, example: localhost:8080
  • endpoint (string): the endpoint to listen on (without the trailing slash), example: /listen

Example:

ws.Start("localhost:8080", "/listen") // listens on 'ws://localhost:8080/listen' endpoint

func (*WebSocket) Stop

func (w *WebSocket) Stop()

Stop stops the websocket server and closes all websocket connections.

This method is called internally when the socketeer is stopped.

Example:

ws.Stop()

Jump to

Keyboard shortcuts

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