conn

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 8 Imported by: 0

README

websocket-conn Build Status

📞 A dead simple WebSocket connection written in Go.

websocket-conn provides you with easy handling for WebSockets, it's based on github.com/gorilla/websocket.

Installation

$ go get -u github.com/shiwano/websocket-conn

Usage

func Connect(ctx context.Context, settings Settings, url string, requestHeader http.Header) (*Conn *http.Response, error)
func UpgradeFromHTTP(ctx context.Context, settings Settings, w http.ResponseWriter, r *http.Request) (*Conn, error)

type Conn struct {
	Stream() <-chan Message
	Err() error
	SendBinaryMessage(data []byte) error
	SendTextMessage(text string) error
}

Examples

Server:

package main

import (
	"context"
	"net/http"
	"github.com/shiwano/websocket-conn"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		c, err := conn.UpgradeFromHTTP(ctx, conn.DefaultSettings(), w, r)
		if err != nil {
			w.Write([]byte("Error"))
			return
		}
		m := <-c.Stream()
		c.SendTextMessage(m.Text() + " World")
		cancel()
		for range c.Stream() {
		}
	})
	http.ListenAndServe(":5000", nil)
}

Client:

package main

import (
	"context"
	"log"
	"github.com/shiwano/websocket-conn"
)

func main() {
	c, _, err := conn.Connect(context.Background(), conn.DefaultSettings(), "ws://localhost:5000", nil)
	if err != nil {
		log.Fatal(err)
	}
	c.SendTextMessage("Hello")
	m := <-c.Stream()
	log.Println(m.Text()) // Hello World
	for range c.Stream() {
	}
}

See also examples directory.

License

Copyright (c) 2016 Shogo Iwano Licensed under the MIT license.

Documentation

Index

Constants

View Source
const (
	TextMessageType   = websocket.TextMessage
	BinaryMessageType = websocket.BinaryMessage
)

MessageType constants. these are same websocket package's values.

Variables

View Source
var (
	// ErrMessageChannelFull indicates that the connection's message channel is full.
	ErrMessageChannelFull = errors.New("websocket-conn: Message channel is full")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents a WebSocket connection.

func Connect

func Connect(ctx context.Context, settings Settings, url string, requestHeader http.Header) (*Conn, *http.Response, error)

Connect to the peer. the requestHeader argument may be nil.

func UpgradeFromHTTP

func UpgradeFromHTTP(ctx context.Context, settings Settings, w http.ResponseWriter, r *http.Request) (*Conn, error)

UpgradeFromHTTP upgrades HTTP to WebSocket.

func (*Conn) Err

func (c *Conn) Err() error

Err returns the disconnection error if the connection closed.

func (*Conn) SendBinaryMessage

func (c *Conn) SendBinaryMessage(data []byte) error

SendBinaryMessage to the peer. This method is goroutine safe.

func (*Conn) SendJSONMessage

func (c *Conn) SendJSONMessage(v interface{}) error

SendJSONMessage to the peer. This method is goroutine safe.

func (*Conn) SendTextMessage

func (c *Conn) SendTextMessage(text string) error

SendTextMessage to the peer. This method is goroutine safe.

func (*Conn) Stream

func (c *Conn) Stream() <-chan Message

Stream retrieve the peer's message data from the stream channel. If the connection closed, this channel closes too.

type DialerSettings

type DialerSettings struct {
	NetDial         func(network, addr string) (net.Conn, error)
	TLSClientConfig *tls.Config
}

DialerSettings represents websocket.Dialer settings.

type Message

type Message struct {
	MessageType MessageType
	Data        []byte
}

Message represents a message.

func (Message) IsBinaryMessage

func (m Message) IsBinaryMessage() bool

IsBinaryMessage determines whether the message is a binary message.

func (Message) IsTextMessage

func (m Message) IsTextMessage() bool

IsTextMessage determines whether the message is a text message.

func (Message) Text

func (m Message) Text() string

Text returns text message data as string.

func (Message) UnmarshalAsJSON

func (m Message) UnmarshalAsJSON(v interface{}) error

UnmarshalAsJSON unmarshals data as JSON.

type MessageType

type MessageType int

MessageType represents websocket message types.

type Settings

type Settings struct {
	WriteWait                time.Duration
	PongWait                 time.Duration
	PingPeriod               time.Duration
	HandshakeTimeout         time.Duration
	MessageChannelBufferSize int
	MaxMessageSize           int64
	ReadBufferSize           int
	WriteBufferSize          int
	Subprotocols             []string

	DialerSettings   *DialerSettings
	UpgraderSettings *UpgraderSettings
}

Settings represents connection settings.

func DefaultSettings

func DefaultSettings() Settings

DefaultSettings returns default settings.

type UpgraderSettings

type UpgraderSettings struct {
	Error       func(w http.ResponseWriter, r *http.Request, status int, reason error)
	CheckOrigin func(r *http.Request) bool
}

UpgraderSettings represents websocket.Upgrader settings.

Directories

Path Synopsis
examples
simple/client command
simple/server command

Jump to

Keyboard shortcuts

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