ws

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2018 License: MIT Imports: 20 Imported by: 2

README

WebSocket Library by aah framework

Build Status codecov Go Report Card Version GoDoc License Twitter

v0.1.0 released and tagged on TBD

It uses tiny, efficient WebSocket library github.com/gobwas/ws, RFC 6455 implementation.

Installation

# install the library
go get -u aahframework.org/ws.v0

Visit official website https://aahframework.org to learn more.

Documentation

Overview

Package ws is a WebSocket library for aah framework.

It uses tiny, efficient WebSocket library `github.com/gobwas/ws`, RFC 6455 implementation.

Index

Constants

View Source
const (
	// EventOnPreConnect event published right before the aah tries establish a
	// WebSocket connection with client.
	EventOnPreConnect = "OnPreConnect"

	// EventOnPostConnect event published right after the successful WebSocket
	// connection have been established with aah server.
	EventOnPostConnect = "OnPostConnect"

	// EventOnPostDisconnect event published when client disconnects either
	// gracefully, gone, network connection error, etc.
	EventOnPostDisconnect = "OnPostDisconnect"

	// EventOnError event published when any errors, auth error, failures while
	// establishing WebSocket connection, etc.
	EventOnError = "OnError"
)
View Source
const Version = "0.1.0"

Version no. of WebSocket library by aah framework

Variables

View Source
var (
	ErrOriginMismatch         = errors.New("aahws: origin mismatch")
	ErrParameterParseFailed   = errors.New("aahws: parameter parse failed")
	ErrAuthenticationFailed   = errors.New("aahws: authentication failed")
	ErrWebSocketNotFound      = errors.New("aahws: not found")
	ErrWebSocketConnectFailed = errors.New("aahws: connect failed")
	ErrConnectionClosed       = errors.New("aahws: connection closed")
	ErrUseOfClosedConnection  = errors.New("aahws: use of closed ws connection")
)

WebSocket errors

Functions

func IsDisconnected

func IsDisconnected(err error) bool

Types

type AuthCallbackFunc

type AuthCallbackFunc func(ctx *Context) bool

AuthCallbackFunc func type used for WebSocket authentication.

type Context

type Context struct {
	Req    *Request
	Conn   net.Conn
	Header http.Header
	// contains filtered or unexported fields
}

Context struct holds friendly WebSocket implementation for aah framework.

func (*Context) Disconnect

func (ctx *Context) Disconnect() error

Disconnect method disconnects the WebSocket connection immediately. Could be used for force disconnect client from server-side.

Note: After this call, any read/reply will result in error. Since connection already closed from server side.

func (*Context) ErrorReason

func (ctx *Context) ErrorReason() error

ErrorReason method returns error info if error was occurred otherwise nil.

func (*Context) Log

func (ctx *Context) Log() log.Loggerer

Log method adds field WebSocket `Request ID` into current log context and returns the logger.

func (*Context) ReadBinary

func (ctx *Context) ReadBinary() ([]byte, error)

ReadBinary method reads a binary data from WebSocket client.

func (*Context) ReadJSON

func (ctx *Context) ReadJSON(t interface{}) error

ReadJSON method reads JSON data from WebSocket client and does unmarshal into given object.

func (*Context) ReadText

func (ctx *Context) ReadText() (string, error)

ReadText method reads a text value from WebSocket client.

func (*Context) ReadXML

func (ctx *Context) ReadXML(t interface{}) error

ReadXML method reads XML data from WebSocket client and does unmarshal into given object.

func (*Context) ReplyBinary

func (ctx *Context) ReplyBinary(v []byte) error

ReplyBinary method sends Binary data to the WebSocket client returns error if client is gone, network error, etc.

func (*Context) ReplyJSON

func (ctx *Context) ReplyJSON(v interface{}) error

ReplyJSON method sends JSON data to the WebSocket client returns error if json marshal issue, client is gone, network issue, etc.

func (*Context) ReplyText

func (ctx *Context) ReplyText(v string) error

ReplyText method sends Text data to the WebSocket client returns error if client is gone, network error, etc.

func (*Context) ReplyXML

func (ctx *Context) ReplyXML(v interface{}) error

ReplyXML method sends XML data to the WebSocket client returns error if XML marshal issue, client is gone, network issue, etc.

type Engine

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

Engine struct holds the implementation of managing WebSocket for aah framework.

func New

func New(cfg *config.Config, logger log.Loggerer) (*Engine, error)

New method creates aah WebSocket engine :)

func (*Engine) AddWebSocket

func (e *Engine) AddWebSocket(t interface{}, methods []*ainsp.Method)

AddWebSocket method adds the given WebSocket implementation into engine.

func (*Engine) CallAction

func (e *Engine) CallAction(ctx *Context)

CallAction method calls the defined action for the WebSocket.

func (*Engine) Connect

func (e *Engine) Connect(w http.ResponseWriter, r *http.Request, route *router.Route, pathParams ahttp.PathParams) (*Context, error)

Connect method primarily does upgrades HTTP connection into WebSocket connection.

Along with Check Origin, Authentication Callback and aah WebSocket events such as `OnPreConnect`, `OnPostConnect`, `OnPostDisconnect` and `OnError`.

func (*Engine) Log

func (e *Engine) Log() log.Loggerer

Log method provides logging methods at WebSocket engine.

func (*Engine) OnError

func (e *Engine) OnError(ecf EventCallbackFunc)

OnError method sets WebSocket `OnError` event callback into WebSocket engine.

Event published for mismatch origin, action parameter parse error, authentication failure, websocket initial connection failure, websocket not found.

func (*Engine) OnPostConnect

func (e *Engine) OnPostConnect(ecf EventCallbackFunc)

OnPostConnect method sets WebSocket `OnPostConnect` event callback into WebSocket engine.

Event published after each WebSocket connection successfully established.

func (*Engine) OnPostDisconnect

func (e *Engine) OnPostDisconnect(ecf EventCallbackFunc)

OnPostDisconnect method sets WebSocket `OnPostDisconnect` event callback into WebSocket engine.

Event published after each WebSocket connection is disconncted from aah server such as client disconnct, connection interrupted, etc.

func (*Engine) OnPreConnect

func (e *Engine) OnPreConnect(ecf EventCallbackFunc)

OnPreConnect method sets WebSocket `OnPreConnect` event callback into WebSocket engine.

Event published before each WebSocket connection been established.

func (*Engine) ReplyError

func (e *Engine) ReplyError(w http.ResponseWriter, errCode int)

ReplyError method writes HTTP error response.

func (*Engine) SetAuthCallback

func (e *Engine) SetAuthCallback(ac AuthCallbackFunc)

SetAuthCallback method sets the WebSocket authentication callback. It gets called for every WebSocket connection.

Authentication callback function should return true for success otherwise false.

type EventCallbackFunc

type EventCallbackFunc func(eventName string, ctx *Context)

EventCallbackFunc func type used for all WebSocket event callback.

type Request

type Request struct {
	// ID aah assigns Globally Unique Identifier (GUID) using Mongo Object ID
	// algorithm for every WebSocket connection made to aah server.
	//
	// You may use it for tracking, tracing or identifying WebSocket client.
	ID string

	// Host value of the HTTP 'Host' header (e.g. 'example.com:8080').
	Host string

	// Path the request URL Path e.g. `/chatroom/aahframework`.
	Path string

	// Header holds the values of HTTP headers when WebSocket connection made.
	Header http.Header
	// contains filtered or unexported fields
}

Request struct holds information for successful WebSocket connection made.

func (*Request) ClientIP

func (r *Request) ClientIP() string

ClientIP method returns remote Client IP address aka Remote IP. It parses in the order of given set of headers otherwise it uses default default header set `X-Forwarded-For`, `X-Real-IP`, "X-Appengine-Remote-Addr" and finally `http.Request.RemoteAddr`.

func (*Request) PathValue

func (r *Request) PathValue(key string) string

PathValue method returns value for given Path param key otherwise empty string. For eg.: `/discussion/:roomName` => `PathValue("roomName")`.

func (*Request) QueryArrayValue

func (r *Request) QueryArrayValue(key string) []string

QueryArrayValue method returns array value for given URL query param key otherwise empty string slice.

func (*Request) QueryValue

func (r *Request) QueryValue(key string) string

QueryValue method returns value for given URL query param key otherwise empty string.

func (Request) String

func (r Request) String() string

String request stringer interface.

Jump to

Keyboard shortcuts

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