websocket

package
v0.0.0-...-98a7958 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package websocket contains helpers and implementation for backend functions that interact with the frontend over websockets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	sockjs.Session
}

Conn is a wrapper for a standard websocket connection with utility methods added for interacting with Kolide specific message types.

func (*Conn) ReadAuthToken

func (c *Conn) ReadAuthToken() (token.Token, error)

ReadAuthToken reads from the websocket, returning an auth token embedded in a JSONMessage with type "auth" and data that can be unmarshalled to authData.

func (*Conn) ReadJSONMessage

func (c *Conn) ReadJSONMessage() (*JSONMessage, error)

ReadJSONMessage reads an incoming Message from JSON. Note that the Message.Data field is guaranteed to be *json.RawMessage, and so unchecked type assertions may be performed as in:

msg, err := c.ReadJSONMessage()
if err == nil && msg.Type == "foo" {
	var foo fooData
	json.Unmarshal(*(msg.Data.(*json.RawMessage)), &foo)
}

func (*Conn) WriteJSON

func (c *Conn) WriteJSON(msg JSONMessage) error

func (*Conn) WriteJSONError

func (c *Conn) WriteJSONError(data interface{}) error

WriteJSONError writes an error (Message struct with Type="error"), returning any error condition from the connection.

func (*Conn) WriteJSONMessage

func (c *Conn) WriteJSONMessage(typ string, data interface{}) error

WriteJSONMessage writes the provided data as JSON (using the Message struct), returning any error condition from the connection.

type JSONMessage

type JSONMessage struct {
	// Type is a string indicating which message type the data contains
	Type string `json:"type"`
	// Data contains the arbitrarily schemaed JSON data. Type should
	// indicate how this should be deserialized.
	Data interface{} `json:"data"`
}

JSONMessage is a wrapper struct for messages that will be sent across the wire as JSON.

Jump to

Keyboard shortcuts

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