ws

package
v0.0.0-...-897d125 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyClosed = errors.New("already closed")

Functions

This section is empty.

Types

type ActionJSON

type ActionJSON struct {
	ActionName action.Action     `json:"action"`
	Data       action.AnyMessage `json:"data"`
}

ActionJSON is a data-transfer-object which is sent by json the client connection.

type Conn

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

Conn is end-point for reading/writing messages from/to websocket. One Conn corresponds to one browser-side client.

func NewConn

func NewConn(conn *websocket.Conn, userID uint64) *Conn

func (*Conn) Close

func (c *Conn) Close() error

Close stops Listen() immediately. closed Conn never listen any message. it returns ErrAlreadyClosed when the Conn is already closed otherwise nil.

func (*Conn) Listen

func (c *Conn) Listen(ctx context.Context)

Listen starts handling reading/writing websocket. it blocks until websocket is closed or context is done.

when Listen() ends, Conn is closed.

func (*Conn) OnActionMessage

func (c *Conn) OnActionMessage(f func(*Conn, action.ActionMessage))

set callback function to handle the event for a message is received. the callback function may be called asynchronously.

func (*Conn) OnClosed

func (c *Conn) OnClosed(f func(*Conn))

set callback function to handle the event for the connection is closed . the callback function may be called asynchronously.

func (*Conn) OnError

func (c *Conn) OnError(f func(*Conn, error))

set callback function to handle the event for the connection gets error. the callback function may be called asynchronously.

func (*Conn) Request

func (c *Conn) Request() *http.Request

Request returns its internal http request.

func (*Conn) Send

func (c *Conn) Send(m event.Event)

Send ActionMessage to browser-side client. message is ignored when Conn is closed.

func (*Conn) UserID

func (c *Conn) UserID() uint64

UserID returns user ID binding to the connection.

type Handler

type Handler func(*Conn)

Handler handles websocket Conn in this package.

type Server

type Server struct {

	// Handler for the Conn type in this package.
	Handler Handler
	// contains filtered or unexported fields
}

Server serves Conn, wrapper for websocket Connetion, for each HTTP request. It implements http.Handler interface.

func NewServer

func NewServer(handler Handler) *Server

NewServer creates the server which serves websocket Connection and providing customizable handler for that connection.

func NewServerFunc

func NewServerFunc(handler func(*Conn)) *Server

NewServerFunc is wrapper function for the NewServer so that given function need not to cast to Handler type.

func (*Server) ServeHTTPWithUserID

func (s *Server) ServeHTTPWithUserID(w http.ResponseWriter, req *http.Request, userID uint64)

ServeHTTPWithUserID is similar with the http.Handler except that it requires userID to specify the which user connects.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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