Git Product home page Git Product logo

go-cryptomarkets-bridge's Introduction

Binance.dex socket wrapper

Implementation of websocket client base on gorilla/websocket library and Binance.dex data via websocket stream.

Overview

This client provides following easy to implement functionality

  • Support for emitting and receiving text and binary data
  • Data compression
  • Concurrency control

Install

go get github.com/oresdev/binance.dex-socket/binance

Description

Create instance of websocket by passing url of Binance.dex end-point

config := binance.Config{
	URL:             "wss://testnet-dex.binance.org/api/ws",
	OnMessage:       _onMessage,
	OnClose:         _onClose,
	OnConnect:       _onConnect,
	PingPeriod:      15 * time.Second,
	SessionPeriod:   25 * time.Minute,
	ReconnectPeriod: 60 * time.Second,
	IsAutoReconnect: true,
}

client := binance.Connect(config)

Important Note : url to websocket server must be specified with either ws or wss.

Set pipes that connect concurrent goroutines

var (
    connected = make(chan bool, 1)
    block     = make(chan bool, 1)
)

Receive bytes from Binance.dex end-point

func _onMessage(msg []byte) {
    //
}

Set and close new connection

func _onConnect() {
    connected <- true
}

func _onClose(code int, text string) error {
    return nil
}

Usage

Registering all listeners and set subscribe message

import (
	"github.com/oresdev/binance.dex-socket/binance"
)

var (
	connected = make(chan bool, 1)
	block     = make(chan bool, 1)
)

func _onMessage(msg []byte) {
	log.Println("OnMessage: ", string(msg))

	binance.UnmarshalJSON(msg)
}

func _onConnect() {
	log.Println("OnConnect")
	connected <- true
}

func _onClose(code int, text string) error {
	log.Println("OnClose", code, text)
	return nil
}

func main() {
	conf := binance.Conf{
		URL:             "wss://testnet-dex.binance.org/api/ws",
		OnMessage:       _onMessage,
		OnClose:         _onClose,
		OnConnect:       _onConnect,
		PingPeriod:      15 * time.Second,
		ReconnectPeriod: 60 * time.Second,
		SessionPeriod:   1500 * time.Second,
		IsAutoReconnect: true,
	}

	client := binance.Connect(conf)

	go client.Start()

	<-connected

	m, err := json.Marshal(binance.Subscribe{
		Method:  "subscribe",
		Topic:   "transfers",
		Address: "tbnb1qtuf578qs9wfl0wh3vs0r5nszf80gvxd28hkrc",
	})
	if err != nil {
		log.Printf("write error: %v", err)
		return
	}

	client.Subscribe(m)

	<-block
}

go-cryptomarkets-bridge's People

Contributors

oreshkindev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.