Categorygithub.com/tacoo/signalr
modulepackage
0.1.5
Repository: https://github.com/tacoo/signalr.git
Documentation: pkg.go.dev

# README

GoDoc CircleCI Go Report Card Maintainability codecov

Overview

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

It supports CloudFlare-protected sites by default.

Example basic usage

package main

import (
	"log"

	"github.com/carterjones/signalr"
)

func main() {
	host := "myhost.not-real-tld"
	protocol := "some-protocol-version-123"
	endpoint := "/usually/something/like/this"
	connectionData := `{"custom":"data"}`

	// Prepare a SignalR client.
	c := signalr.New(host, protocol, endpoint, connectionData)

	// Start the connection.
	msgs, errs, err := c.Run()
	if err != nil {
		log.Panic(err)
	}

	// Process messages and errors.
	for {
		select {
		case msg := <-msgs:
			// Handle the message.
			log.Println(msg)
		case err := <-errs:
			// Handle the error.
			log.Panic(err)
		}
	}
}

Example complex usage

package main

import (
	"log"

	"github.com/carterjones/signalr"
)

func main() {
	host := "myhost.not-real-tld"
	protocol := "some-protocol-version-123"
	endpoint := "/usually/something/like/this"
	connectionData := `{"custom":"data"}`

	// Prepare a SignalR client.
	c := signalr.New(host, protocol, endpoint, connectionData)

	// Perform any optional modifications to the client here. Read the docs for
	// all the available options that are exposed via public fields.

	// Manually perform the initialization routine.
	err := c.Negotiate()
	if err != nil {
		log.Panic(err)
	}
	conn, err := c.Connect()
	if err != nil {
		log.Panic(err)
	}
	err = c.Start(conn)
	if err != nil {
		log.Panic(err)
	}

	// Create message and error channels.
	msgs := make(chan signalr.Message)
	errs := make(chan error)

	// Begin the message reading loop.
	go c.ReadMessages(msgs, errs)

	// Process messages and errors.
	for {
		select {
		case msg := <-msgs:
			// Handle the message.
			log.Println(msg)
		case err := <-errs:
			// Handle the error.
			log.Panic(err)
		}
	}
}

Documentation

Contribute

If anything is unclear or could be improved, please open an issue or submit a pull request. Thanks!

# Packages

Package hubs provides functionality used by the SignalR Hubs API.

# Functions

New creates and initializes a SignalR client.
TestCompleteHandler combines the negotiate, connect, reconnect, and start handlers found in this package into one complete response handler.
TestConnect provides a sample "/connect" handling function.
TestNegotiate provides a sample "/negotiate" handling function.
TestReconnect provides a sample "/reconnect" handling function.
TestStart provides a sample "/start" handling function.

# Constants

HTTP is the literal string, "http".
HTTPS is the literal string, "https".
WS is the literal string, "ws".
WSS is the literal string, "wss".

# Structs

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

# Interfaces

JSONWriter is the interface that wraps WriteJSON.
MessageReader is the interface that wraps ReadMessage.
WebsocketConn is a combination of MessageReader and JSONWriter.

# Type aliases

Scheme represents a type of transport scheme.