ws

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2020 License: MIT Imports: 5 Imported by: 0

README

ws

godoc goreportcard

Install

go get -u github.com/117/ws

Usage

Creating a new client
client := NewClient()

if err := client.Connect("wss://echo.websocket.org"); err != nil {
    panic(err)
}

// success 🥳, interact with the client further
Sending a message
if err := client.Send("hello"); err != nil {
    panic(err)
}
Receiving messages
for {
    message, err := client.Receive()

    if err != nil {
        panic(err)
    }

    fmt.Println(message.String())
}
Receiving messages as structs
type ExampleMessage struct {
    Hello string `json:"hello"`
}

var example ExampleMessage

for {
    // will block until the message shares the same fields as the pointer
    if err := client.ReceivePtr(&example); err != nil {
        panic(err)
    }

    fmt.Println(example)
}

Contribute

Pull requests are encouraged.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Connection *gorilla.Conn
}

Client - A bare bones WebSocket client.

func NewClient

func NewClient() *Client

NewClient - Creates a client and attempts to establish connection.

func (*Client) Connect

func (client *Client) Connect(url string) error

Connect - Establish a new connection with the WebSocket server.

func (*Client) Receive

func (client *Client) Receive() (Message, error)

Receive - Block until a new message is received.

func (*Client) ReceivePtr

func (client *Client) ReceivePtr(ptr interface{}) error

ReceivePtr - Block until a new message of same type as the pointer is received.

func (*Client) Send

func (client *Client) Send(message interface{}) error

Send - Send a new message. Use a JSON-friendly struct, string, or byte slice.

type Message

type Message []byte

Message - WebSocket message wrapper.

func (Message) String

func (m Message) String() string

func (Message) Unmarshal

func (m Message) Unmarshal(pointer interface{}) error

Unmarshal - Unpack the message into a provided a JSON-friendly struct.

Jump to

Keyboard shortcuts

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