signalr

package module
v0.0.0-...-77f1649 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2018 License: MIT Imports: 13 Imported by: 0

README

GoDoc Build Status Go Report Card Maintainability Test Coverage

Overview

This is my personal attempt at implementating the client side of the WebSocket portion of the SignalR protocol. I use it for various altcoin trading platforms that use SignalR.

Documentation

Excellent technical deep dive of the protocol

https://blog.3d-logic.com/2015/03/29/signalr-on-the-wire-an-informal-description-of-the-signalr-protocol/

Startup Sequence

https://github.com/SignalR/SignalR/issues/2997

Microsoft Specification

https://docs.microsoft.com/en-us/aspnet/signalr/overview/

Documentation

Overview

Package signalr provides the client side implementation of the WebSocket portion of the SignalR protocol. This was almost entirely written using https://blog.3d-logic.com/2015/03/29/signalr-on-the-wire-an-informal-description-of-the-signalr-protocol/ as a reference guide.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// The host providing the SignalR service.
	Host string

	// The relative path where the SignalR service is provided.
	Endpoint string

	// The websockets protocol version.
	Protocol string

	ConnectionData string

	// The HTTPClient used to initialize the websocket connection.
	HTTPClient *http.Client

	// This field holds a struct that can read messages from and write JSON
	// objects to a websocket. In practice, this is simply a raw websocket
	// connection that results from a successful connection to the SignalR
	// server.
	Conn WebsocketConn

	// An optional setting to provide a non-default TLS configuration to use
	// when connecting to the websocket.
	TLSClientConfig *tls.Config

	// Either HTTPS or HTTP.
	Scheme Scheme

	// Set a maximum number of negotiate retries.
	MaxNegotiateRetries int

	// The time to wait before retrying, in the event that an error occurs
	// when contacting the SignalR service.
	RetryWaitDuration time.Duration

	ConnectionToken string
	ConnectionID    string

	// Header values that should be applied to all HTTP requests.
	Headers     map[string]string
	AccessToken string
	// contains filtered or unexported fields
}

Client represents a SignlR client. It manages connections so that the caller doesn't have to.

func New

func New(host, protocol, endpoint, connectionData string) (c *Client)

New creates and initializes a SignalR client.

func (*Client) Connect

func (c *Client) Connect() (conn *websocket.Conn, err error)

Connect implements the connect step of the SignalR connection sequence.

func (*Client) Init

func (c *Client) Init() (err error)

Init connects to the host and performs the websocket initialization routines that are part of the SignalR specification.

func (*Client) Messages

func (c *Client) Messages() <-chan Message

Messages returns the channel that receives persistent connection messages.

func (*Client) Negotiate

func (c *Client) Negotiate() (err error)

Negotiate implements the negotiate step of the SignalR connection sequence.

func (*Client) Reconnect

func (c *Client) Reconnect() (conn *websocket.Conn, err error)

Reconnect implements the reconnect step of the SignalR connection sequence.

func (*Client) Send

func (c *Client) Send(m hubs.ClientMsg) (err error)

Send sends a message to the websocket connection.

func (*Client) Start

func (c *Client) Start(conn WebsocketConn) (err error)

Start implements the start step of the SignalR connection sequence.

type JSONWriter

type JSONWriter interface {
	WriteJSON(v interface{}) error
}

JSONWriter is the interface that wraps WriteJSON.

WriteJSON is defined at https://godoc.org/github.com/gorilla/websocket#Conn.WriteJSON

At a high level, it writes a structure to the underlying websocket and returns any error that was encountered during the write operation.

type Message

type Message struct {
	// message id, present for all non-KeepAlive messages
	C string

	// an array containing actual data
	M []hubs.ClientMsg

	// indicates that the transport was initialized (a.k.a. init message)
	S int

	// groups token – an encrypted string representing group membership
	G string
}

Message represents a message sent from the server to the persistent websocket connection.

type MessageReader

type MessageReader interface {
	ReadMessage() (messageType int, p []byte, err error)
}

MessageReader is the interface that wraps ReadMessage.

ReadMessage is defined at https://godoc.org/github.com/gorilla/websocket#Conn.ReadMessage

At a high level, it reads messages and returns:

  • the type of message read
  • the bytes that were read
  • any errors encountered during reading the message

type Scheme

type Scheme string

Scheme represents a type of transport scheme. For the purposes of this project, we only provide constants for schemes relevant to HTTP and websockets.

const (
	// HTTPS is the literal string, "https".
	HTTPS Scheme = "https"

	// HTTP is the literal string, "http".
	HTTP Scheme = "http"

	// WSS is the literal string, "wss".
	WSS Scheme = "wss"

	// WS is the literal string, "ws".
	WS Scheme = "ws"
)

type WebsocketConn

type WebsocketConn interface {
	MessageReader
	JSONWriter
}

WebsocketConn is a combination of MessageReader and JSONWriter. It is used to provide an interface to objects that can read from and write to a websocket connection.

Directories

Path Synopsis
Package hubs provides functionality used by the SignalR Hubs API.
Package hubs provides functionality used by the SignalR Hubs API.

Jump to

Keyboard shortcuts

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