go_websocket_client

package module
v0.0.0-...-42afe72 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2021 License: MIT Imports: 10 Imported by: 0

README

go-websocket-client

golang simply implement of websocket client base on gorilla/websocket

install

go get -u github.com/link-yundi/go-websocket-client

example

configuration
import github.com/link-yundi/go-websocket-client
func main() {
    ...
   	config := Config{
		Url:             "ws host",
		ProxyUrl:        "proxy host",
		PingPeriod:      15 * time.Second,
		IsAutoReconnect: true,
	}
	client := NewClient(config) 
    ...
}

start client
...
client.Start()
...
subscribe
...
client.Subscribe([]byte(`some event string`))
...
auto reconnect
...
config := Config{
    	...
    	IsAutoReconnect: true,
    	...
	}
...
close client
...
client.Terminate()
...
customize message-handler
func onMessage(msg []byte) {
    // do somthing you want
	fmt.Println("OnMessage: ", string(msg))
}

func onConnect() {
    // do somthing you want
	fmt.Println("OnConnect")
}

func onClose(code int, text string) error {
    // do somthing you want
	fmt.Println("OnClose", code, text)
	return nil
}

func main() {
    config := Config{
		...
		OnMessage:       onMessage,
		OnClose:         onClose,
		OnConnect:       onConnect,
		PingPeriod:      15 * time.Second,
		IsAutoReconnect: true,
        ...
	}
    client := NewClient(config)
    go client.Start()
    // or client.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Config

	Subs [][]byte
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config Config) *Client

func (*Client) Send

func (client *Client) Send(msg []byte)

func (*Client) SendClose

func (client *Client) SendClose()

func (*Client) Start

func (client *Client) Start()

func (*Client) Subscribe

func (client *Client) Subscribe(data []byte)

func (*Client) Terminate

func (client *Client) Terminate()

type Config

type Config struct {
	Url              string
	ProxyUrl         string
	ReqHeaders       map[string][]string
	PingPeriod       time.Duration
	IsAutoReconnect  bool
	OnConnect        func()
	OnMessage        func([]byte)
	OnClose          func(int, string) error
	OnError          func(err error)
	ReadDeadlineTime time.Duration
	ReconnectPeriod  time.Duration
}

Jump to

Keyboard shortcuts

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