jsonbuffer

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2020 License: MPL-2.0-no-copyleft-exception Imports: 4 Imported by: 0

Documentation

Overview

Package jsonbuffer implements a stream protocol using JSON blobs as segments in the buffer, with a type and envelope.

Each message is sent as JSON (see the Message struct) and contains a type and payload. The type determines how the payload is handled; but arguably what happens as a result of that belongs to the client.

There is also a javascript client for this in the ci-ui repository.

Index

Constants

View Source
const (
	// TypeMessage is a message and the payload is the message we're trying to send.
	TypeMessage = "message"
	// TypeError is an error and the payload is the error text.
	TypeError = "error"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Type    string `json:"type"`
	Payload string `json:"payload"`
}

Message is the websocket message transmitted.

type Wrapper

type Wrapper struct {
	io.Writer
	io.Reader
	// contains filtered or unexported fields
}

Wrapper is a wrapper for io.Reader that lets us power it via protocol through our client.

func NewWrapper

func NewWrapper(rw io.ReadWriter) *Wrapper

NewWrapper creates a Wrapper from a pre-established connection.

func (*Wrapper) Read

func (w *Wrapper) Read(buf []byte) (int, error)

Read conforms to the io.Reader interface. If it cannot fill buf with the payload, it will keep the buffer for the next read call.

func (*Wrapper) Recv

func (w *Wrapper) Recv() (string, error)

Recv reads a single message from the reader and returns it. If the type is message, it returns the payload, otherwise if it hits EOF it will return ErrEOF; on any error return it returns the error.

func (*Wrapper) Send

func (w *Wrapper) Send(message string) error

Send sends a message by writing through the Wrapper. The outgoing message will be of type websocket.TypeMessage.

func (*Wrapper) SendError

func (w *Wrapper) SendError(err error) error

SendError is like Send, but for errors.

func (*Wrapper) Write

func (w *Wrapper) Write(buf []byte) (int, error)

Write is Send that conforms to the io.Writer spec. Each buffer will be sent as a single Message.

Jump to

Keyboard shortcuts

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