# README
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
- GoDoc: https://godoc.org/github.com/carterjones/signalr
- SignalR specification: https://docs.microsoft.com/en-us/aspnet/signalr/overview/
- 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/
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.
# 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.