conn

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 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

type Conn struct {
  Settings             *Settings
  BinaryMessageHandler func([]byte)
  TextMessageHandler   func(string)
  DisconnectHandler    func()
  ErrorHandler         func(error)
}

func New(ctx context.Context) *Conn
func (c *Conn) Connect(url string, requestHeader http.Header) (*http.Response, error)
func (c *Conn) UpgradeFromHTTP(responseWriter http.ResponseWriter, request *http.Request) error
func (c *Conn) WriteBinaryMessage(data []byte) error
func (c *Conn) WriteTextMessage(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 := context.Background()
    c := conn.New(ctx)
    c.TextMessageHandler = func(text string) {
      c.WriteTextMessage(text + " World")
    }
    if err := c.UpgradeFromHTTP(w, r); err != nil {
      w.Write([]byte("Error"))
    }
  })
  http.ListenAndServe(":5000", nil)
}

Client:

package main

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

func main() {
  textMessageCh := make(chan string)
  ctx := context.Background()
  c := conn.New(ctx)
  c.TextMessageHandler = func(text string) {
    textMessageCh <- text
  }
  if _, err := c.Connect("ws://localhost:5000", nil); err != nil {
    panic(err)
  }
  c.WriteTextMessage("Hello")
  text := <-textMessageCh
  fmt.Println(text)
}

License

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

Documentation

Index

Constants

This section is empty.

Variables

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

	// ErrAlreadyUsed indicates that the connection is already used.
	ErrAlreadyUsed = errors.New("websocket-conn: Already used")
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Settings             *Settings
	BinaryMessageHandler func([]byte)
	TextMessageHandler   func(string)
	DisconnectHandler    func()
	ErrorHandler         func(error)
	// contains filtered or unexported fields
}

Conn represents a WebSocket connection.

func New

func New(ctx context.Context) *Conn

New creates a Conn.

func (*Conn) Connect

func (c *Conn) Connect(url string, requestHeader http.Header) (*http.Response, error)

Connect to the peer.

func (*Conn) UpgradeFromHTTP

func (c *Conn) UpgradeFromHTTP(responseWriter http.ResponseWriter, request *http.Request) error

UpgradeFromHTTP upgrades HTTP to WebSocket.

func (*Conn) WriteBinaryMessage

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

WriteBinaryMessage to the peer.

func (*Conn) WriteTextMessage

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

WriteTextMessage to the peer.

type DialerSettings

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

DialerSettings represents websocket.Dialer settings.

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.

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
client command
server command

Jump to

Keyboard shortcuts

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