conn

package module
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2017 License: MIT Imports: 7 Imported by: 1

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 added in v1.0.0

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 added in v1.0.0

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

UpgradeFromHTTP upgrades HTTP to WebSocket.

func (*Conn) Err added in v1.0.0

func (c *Conn) Err() error

Err returns the disconnection error if the connection closed.

func (*Conn) SendBinaryMessage added in v1.0.0

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

SendBinaryMessage to the peer. This method is goroutine safe.

func (*Conn) SendTextMessage added in v1.0.0

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

SendTextMessage to the peer. This method is goroutine safe.

func (*Conn) Stream added in v1.0.0

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

Stream retrieve the peer's message data from the stream channel. If the connection closed, it returns data with true of EOS flag at last.

type DialerSettings

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

DialerSettings represents websocket.Dialer settings.

type Message added in v1.0.0

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 added in v1.0.0

func (m Message) Text() string

Text returns text message data as string.

type MessageType added in v1.0.0

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 added in v1.0.0

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