scclient

package module
v0.0.0-...-693500c Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

Go SocketCluster Client

This library can be used to connect to a SocketCluster server.

It was written as an alternative to the official implementation, because its API is a 1:1 copy from the JS implementation, whereas this library attempts to implement a somewhat more Golang-like API.

It uses the gorilla/websocket library, so it should be fully RFC-6455 compliant. Besides that, it reconnects automatically using an exponential backoff algorithm. It does not, however, implement JSON Web Tokens (JWT), though Pull Requests are of course welcome :)

Getting started

import "github.com/Freeaqingme/go-socketcluster-client"

const wsUrl = "wss://sc-02.coinigy.com/socketcluster/"

func main() {
    client := scclient.New(wsUrl)

    // Supply a callback for any events that need to be performed upon every reconnnect
    client.ConnectCallback = func() error {
        _, err := client.Emit("auth", &authEvent{apiKey, apiSecret })
        return err
    }

    if err := client.Connect(); err != nil {
        panic(err)
    }

    channel, err := client.Subscribe("TRADE-KRKN--XBT--EUR")
    if err != nil {
        panic(err)
    }

    go func() {
        for msg := range channel {
            fmt.Println("New kraken trade: " + string(msg))
        }
    }()

    if res, err := client.Emit("exchanges", nil); err != nil {
        panic(err)
    } else {
        fmt.Println("exchanges: " + string(res))
    }
}

Using a custom dialer

Useful if you'd like to use a local proxy or some other special use case

import (
    "github.com/Freeaqingme/go-socketcluster-client"
    "github.com/gorilla/websocket"
)

func main() {
    client := scclient.New(wsUrl)

    uProxy, _ := url.Parse("http://localhost:8888")
    dialer := &websocket.Dialer{
        Proxy: http.ProxyURL(uProxy),
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true, // Running this on production is bad, fyi...
        },
    }

    if err := client.ConnectWithDialer(dialer); err != nil {
        panic(err)
    }
}

License

This project is licensed under the Apache2 License - see the LICENSE file for details

Documentation

Index

Constants

View Source
const (
	LogLevelDebug = iota
	LogLevelError = iota
)

Variables

View Source
var Ping = []byte("#1")
View Source
var Pong = []byte("#2")

Functions

This section is empty.

Types

type Client

type Client struct {
	EventHandler    func(event string, data []byte)
	ConnectCallback func() error
	Logger          func(level LogLevel, levelmsg string)
	// contains filtered or unexported fields
}

func New

func New(url string) *Client

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) ConnectWithDialer

func (c *Client) ConnectWithDialer(dialer *websocket.Dialer) error

func (*Client) Emit

func (c *Client) Emit(msgType string, data interface{}) (out []byte, outErr error)

func (*Client) EmitWithoutResponse

func (c *Client) EmitWithoutResponse(msgType string, data interface{}) error

func (*Client) Subscribe

func (c *Client) Subscribe(chName string) (<-chan []byte, error)

type LogLevel

type LogLevel int

Jump to

Keyboard shortcuts

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