Categorygithub.com/wamoscode/go-signalr
modulepackage
0.1.0
Repository: https://github.com/wamoscode/go-signalr.git
Documentation: pkg.go.dev

# README

Overview

SignalR go client is built over websockets based on the client implementation of the Javascript client.

The spec documents below give a deeper insight about the implementation

TransportProtocols

HubProtocoal

Example

Typical Usage example:

package yours

import (
  signalr "github.com/wamoscode/go-signalr"
)

func main() {

   p = signalr.NegotiationRequestPayload{
       URL: "YOUR URL",
       AccessToken: "Add token if auth is required!"
   }

   err = signalr.Start(p)
	if err != nil {
		 return
    }

    errchan := make(chan error, 1)
    done := make(chan bool)
    interrupt := make(chan os.Signal, 1)
	restart := make(chan bool)
 
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
    
    go func() {
		defer func() {
			signalr.Conn().Close()
			restart <- true
		}()
		for {
			t, p, err := signalr.Read(processReceivedMessage)

			if err != nil {
				errchan <- err
				return
			}

			if strings.Contains(string(p), "{}") {
				log.Debug("Handshake Successfull")
			}

			if t == signalr.Close {
				done <- true
				return
			}
		}
    }()
    
    go func() {
		defer func() {
			close(done)
			restart <- true
		}()
		for {
			select {
			case <-done:
				return
			case <-ticker.C:
				pingMessage := signalr.PingMessage{
					Type: signalr.Ping,
				}
				pm, err := json.Marshal(pingMessage)
				if err != nil {
					return
				}
				err = signalr.Send(pm)
				if err != nil {
					return
				}
			case <-interrupt:
				closeMessage := signalr.CloseMessage{
					Type:  signalr.Close,
					Error: "Interrupt occurred",
				}
				cm, err := json.Marshal(closeMessage)
				if err != nil {
					return
				}
				// send ping
				err = signalr.Send(cm)
				if err != nil {
					return
				}

				select {
				case <-done:
				case <-time.After(time.Second):
				}
				return
			}
		}
    }()
    
    // Wait for restart and error message
    for {
		select {
		case <-restart:
			{
				log.Debug("Signaled Restart")
				// Do something
			}
		case err := <-errchan:
			{
				 // Do something
			} 
		default:
		}

	}
  
}

// Signalr Callback
func processReceivedMessage(data signalr.InvokeMessage) {
   switch data.Target {
	case "YOUR EVENT":
        //Process message
    default:
   }
}

Documentation

SignalR: https://dotnet.microsoft.com/apps/aspnet/signalr

# Functions

Conn returns Websocket connection.
New ...
Read retrieves response on the socket.
Send writes data on the socket.
Start ...
Stop disables socket connection.

# Constants

CancelInvocation Sent by the client to cancel a streaming invocation on the server.
Close Sent by the server when a connection is closed.
Completion Indicates a previous Invocation or StreamInvocation has Completed Contains an error if the invocation concluded with an error or result of non-streaming method invocation The result will be absent for void methods.
HandshakeRequest Sent by the client to agree on the message format.
HandshakeResponse Sent by the server as an acknowledgement of teh previous HandshakeRequest message.
Invocation Indicates a request to invoke a particular method(the Target) with provided Arguments on the remote endpoint.
Ping Sent by either party to check if connection is active.
StreamInvocation Indicates a request to invoke a streaming method (the target) with provided Arguments on the remote endpoint.
StreamItem Indicates individual Items of streamed response data from a previous StreamInvocation message.

# Structs

AvailableTransport ...
Client ...
CloseMessage ...
CompletionMessage ...
HandshakeRequestMessage ...
HandshakeResponseMessage ...
InvokeMessage ...
MessageFormat ...
NegotiationError ...
NegotiationRequestPayload ...
NegotiationResponse ...
PingMessage ...