Documentation ¶
Overview ¶
Internal package for handling websocket connections and dispatching updates to clients.
This package is used in the following way:
- Create a new WebSocket type with NewWebSocket().
- Start the WebSocket with Start().
- Stop the WebSocket with Stop().
- 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 ¶
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 ¶
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