wsconn

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 6, 2020 License: MIT Imports: 6 Imported by: 1

README

wsconn

wsconn is a websocket client based on gorilla/websocket that will automatically reconnect if the connection is dropped and keeps the connection alive.

Example

package main

import (
	"fmt"
	"github.com/coinrust/wsconn"
	"log"
)

func main() {
	wsURL := "wss://api.zb.live/websocket"
	ws := wsconn.NewWs(
		wsconn.WsUrlOption(wsURL),
		wsconn.WsDumpOption(true),
		wsconn.WsAutoReconnectOption(true),
		wsconn.WsMessageHandleFuncOption(func(bytes []byte) error {
			log.Printf("%v", string(bytes))
			return nil
		}),
		wsconn.WsErrorHandleFuncOption(func(err error) {
			log.Printf("%v", err)
		}),
	)
	ch := fmt.Sprintf("%v_depth", "zbqc") // zbqc_depth
	sub := map[string]string{
		"event":   "addChannel",
		"channel": ch,
	}
	ws.Subscribe(sub)

	select {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDialer added in v1.0.2

func DefaultDialer() *websocket.Dialer

Types

type WsConn

type WsConn struct {
	WsParameter
	// contains filtered or unexported fields
}

func NewWs

func NewWs(opts ...WsParameterOption) *WsConn

func (*WsConn) Close

func (ws *WsConn) Close()

func (*WsConn) Dialer added in v1.0.1

func (ws *WsConn) Dialer() *websocket.Dialer

func (*WsConn) ReceiveMessage

func (ws *WsConn) ReceiveMessage(msg []byte)

func (*WsConn) SendCloseMessage

func (ws *WsConn) SendCloseMessage(msg []byte)

func (*WsConn) SendJsonMessage

func (ws *WsConn) SendJsonMessage(m interface{}) error

func (*WsConn) SendMessage

func (ws *WsConn) SendMessage(msg []byte)

func (*WsConn) SendPingMessage

func (ws *WsConn) SendPingMessage(msg []byte)

func (*WsConn) Subscribe

func (ws *WsConn) Subscribe(sub interface{}) error

type WsParameter

type WsParameter struct {
	WsUrl             string
	ReqHeaders        map[string][]string
	Dialer            *websocket.Dialer
	MessageHandleFunc func([]byte) error
	ReSubscribeFunc   func(ws *WsConn) error // 短线重连后触发,你可以重新订阅,因为有些订阅跟当前时间相关
	ErrorHandleFunc   func(err error)
	AutoReconnect     bool // 自动重连(默认: true)
	ReSubscribe       bool // 自动重新订阅(默认: true)
	IsDump            bool
	// contains filtered or unexported fields
}

type WsParameterOption

type WsParameterOption func(p *WsParameter)

func WsAutoReconnectOption

func WsAutoReconnectOption(autoReconnect bool) WsParameterOption

func WsDialerOption added in v1.0.2

func WsDialerOption(dialer *websocket.Dialer) WsParameterOption

func WsDumpOption

func WsDumpOption(dump bool) WsParameterOption

func WsErrorHandleFuncOption

func WsErrorHandleFuncOption(f func(err error)) WsParameterOption

func WsMessageHandleFuncOption

func WsMessageHandleFuncOption(f func([]byte) error) WsParameterOption

func WsReSubscribeFuncOption added in v1.0.4

func WsReSubscribeFuncOption(f func(ws *WsConn) error) WsParameterOption

func WsReSubscribeOption added in v1.0.4

func WsReSubscribeOption(reSubscribe bool) WsParameterOption

func WsReqHeaderOption

func WsReqHeaderOption(key, value string) WsParameterOption

func WsUrlOption

func WsUrlOption(wsUrl string) WsParameterOption

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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