websocket_

package
Version: v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxBytes = 1 << 20 // 1 MB

Variables

View Source
var (
	// ServerContextKey is a context key. It can be used in Websocket
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of
	// type *Server.
	ServerContextKey = &contextKey{"websocket-server"}
	// ClientContextKey is a context key. It can be used in Websocket
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of
	// type *Client.
	ClientContextKey = &contextKey{"websocket-client"}

	// LocalAddrContextKey is a context key. It can be used in
	// TCP handlers with context.WithValue to access the local
	// address the connection arrived on.
	// The associated value will be of type net.Addr.
	LocalAddrContextKey = &contextKey{"local-addr"}
)
View Source
var ErrAbortHandler = errors.New("net/websocket: abort onMsgHandleHandler")

ErrAbortHandler is a sentinel panic value to abort a handler. While any panic from OnHandshake aborts the response to the client, panicking with ErrAbortHandler also suppresses logging of a stack trace to the server's error log.

View Source
var ErrClientClosed = errors.New("websocket: Client closed")
View Source
var ErrNotFound = errors.New("websocket: Server not found")
View Source
var ErrServerClosed = errors.New("websocket: Server closed")

ErrServerClosed is returned by the Server's Serve and ListenAndServe methods after a call to Shutdown or Close.

View Source
var ErrUnImplement = errors.New("UnImplement Method")
View Source
var NopOnCloseHandler = nopSC
View Source
var NopOnErrorHandler = nopSC
View Source
var NopOnHTTPResponseHandler = nopSC
View Source
var NopOnHandshakeHandler = nopSC
View Source
var NopOnMsgHandleHandler = nopSC
View Source
var NopOnMsgReadHandler = nopSC
View Source
var NopOnOpenHandler = nopSC

Functions

func NewWebSocketConn

func NewWebSocketConn(rw *websocket.Conn) transport.ReadWriteCloser

Types

type Client

type Client struct {
	*Server
	// contains filtered or unexported fields
}

func NewClient

func NewClient(h ClientHandler) *Client

func NewClientFunc

func NewClientFunc(onHTTPRespHandler OnHTTPResponseHandler,
	onOpenHandler OnOpenHandler,
	onMsgReadHandler OnMsgReadHandler,
	onMsgHandleHandler OnMsgHandleHandler,
	onCloseHandler OnCloseHandler,
	onErrorHandler OnErrorHandler) *Client

func (*Client) DialAndServe

func (cli *Client) DialAndServe(urlStr string, requestHeader http.Header) error

OnHandshake takes over the http handler

func (*Client) ServeHTTP deprecated

This method has been deprecated.
func (cli *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) error

Deprecated: use DialAndServe instead.

type ConnState

type ConnState int

A ConnState represents the state of a client connection to a server. It's used by the optional Server.ConnState hook.

const (
	// StateNew represents a new connection that is expected to
	// send a request immediately. Connections begin at this
	// state and then transition to either StateActive or
	// StateClosed.
	StateNew ConnState = iota

	// StateActive represents a connection that has read 1 or more
	// bytes of a request. The Server.ConnState hook for
	// StateActive fires before the request has entered a handler
	// and doesn't fire again until the request has been
	// handled. After the request is handled, the state
	// transitions to StateClosed, StateHijacked, or StateIdle.
	// For HTTP/2, StateActive fires on the transition from zero
	// to one active request, and only transitions away once all
	// active requests are complete. That means that ConnState
	// cannot be used to do per-request work; ConnState only notes
	// the overall state of the connection.
	StateActive

	// StateIdle represents a connection that has finished
	// handling a request and is in the keep-alive state, waiting
	// for a new request. Connections transition from StateIdle
	// to either StateActive or StateClosed.
	StateIdle

	// StateHijacked represents a hijacked connection.
	// This is a terminal state. It does not transition to StateClosed.
	StateHijacked

	// StateClosed represents a closed connection.
	// This is a terminal state. Hijacked connections do not
	// transition to StateClosed.
	StateClosed
)

func (ConnState) String

func (c ConnState) String() string

type NopClient

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

func (*NopClient) OnClose

func (srv *NopClient) OnClose(conn WebSocketReadWriteCloser) error

func (*NopClient) OnError

func (srv *NopClient) OnError(conn WebSocketReadWriteCloser, err error) error

func (*NopClient) OnHTTPResponse

func (srv *NopClient) OnHTTPResponse(resp *http.Response) error

func (*NopClient) OnHandshake

func (srv *NopClient) OnHandshake(w http.ResponseWriter, r *http.Request) error

func (*NopClient) OnMsgHandle

func (srv *NopClient) OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error

func (*NopClient) OnMsgRead

func (srv *NopClient) OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)

func (*NopClient) OnOpen

func (srv *NopClient) OnOpen(conn WebSocketReadWriteCloser) error

type NopServer

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

func (*NopServer) OnClose

func (srv *NopServer) OnClose(conn WebSocketReadWriteCloser) error

func (*NopServer) OnError

func (srv *NopServer) OnError(conn WebSocketReadWriteCloser, err error) error

func (*NopServer) OnHTTPResponse

func (srv *NopServer) OnHTTPResponse(resp *http.Response) error

func (*NopServer) OnHandshake

func (srv *NopServer) OnHandshake(w http.ResponseWriter, r *http.Request) error

func (*NopServer) OnMsgHandle

func (srv *NopServer) OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error

func (*NopServer) OnMsgRead

func (srv *NopServer) OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)

func (*NopServer) OnOpen

func (srv *NopServer) OnOpen(conn WebSocketReadWriteCloser) error

type OnCloseHandler

type OnCloseHandler interface {
	OnClose(conn WebSocketReadWriteCloser) error
}

type OnCloseHandlerFunc

type OnCloseHandlerFunc func(conn WebSocketReadWriteCloser) error

func (OnCloseHandlerFunc) OnClose

type OnErrorHandler

type OnErrorHandler interface {
	OnError(conn WebSocketReadWriteCloser, err error) error
}

type OnErrorHandlerFunc

type OnErrorHandlerFunc func(conn WebSocketReadWriteCloser, err error) error

func (OnErrorHandlerFunc) OnError

type OnHTTPResponseHandler

type OnHTTPResponseHandler interface {
	OnHTTPResponse(resp *http.Response) error
}

type OnHTTPResponseHandlerFunc

type OnHTTPResponseHandlerFunc func(resp *http.Response) error

func (OnHTTPResponseHandlerFunc) OnHTTPResponse

func (f OnHTTPResponseHandlerFunc) OnHTTPResponse(resp *http.Response) error

type OnHandshakeHandler

type OnHandshakeHandler interface {
	OnHandshake(http.ResponseWriter, *http.Request) error
}

type OnHandshakeHandlerFunc

type OnHandshakeHandlerFunc func(http.ResponseWriter, *http.Request) error

func (OnHandshakeHandlerFunc) OnHandshake

type OnMsgHandleHandler

type OnMsgHandleHandler interface {
	OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error
}

type OnMsgHandleHandlerFunc

type OnMsgHandleHandlerFunc func(conn WebSocketReadWriteCloser, msg interface{}) error

func (OnMsgHandleHandlerFunc) OnMsgHandle

func (f OnMsgHandleHandlerFunc) OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error

type OnMsgReadHandler

type OnMsgReadHandler interface {
	OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)
}

type OnMsgReadHandlerFunc

type OnMsgReadHandlerFunc func(conn WebSocketReadWriteCloser) (msg interface{}, err error)

func (OnMsgReadHandlerFunc) OnMsgRead

func (f OnMsgReadHandlerFunc) OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)

type OnOpenHandler

type OnOpenHandler interface {
	OnOpen(conn WebSocketReadWriteCloser) error
}

type OnOpenHandlerFunc

type OnOpenHandlerFunc func(conn WebSocketReadWriteCloser) error

func (OnOpenHandlerFunc) OnOpen

type Server

type Server struct {
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
	MaxBytes     int

	ErrorLog *log.Logger

	// ConnState specifies an optional callback function that is
	// called when a client connection changes state. See the
	// ConnState type and associated constants for details.
	ConnState func(*WebSocketConn, ConnState)
	// contains filtered or unexported fields
}

func NewServer

func NewServer(h ServerHandler) *Server

func NewServerFunc

func NewServerFunc(onHandshake OnHandshakeHandler,
	onOpen OnOpenHandler,
	onMsgRead OnMsgReadHandler,
	onMsgHandle OnMsgHandleHandler,
	onClose OnCloseHandler,
	onError OnErrorHandler) *Server

func (*Server) CheckError

func (srv *Server) CheckError(conn WebSocketReadWriteCloser, err error) error

func (*Server) RegisterOnShutdown

func (srv *Server) RegisterOnShutdown(f func())

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) error

OnHandshake takes over the http handler

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

type WebSocketCloser

type WebSocketCloser interface {
	Close() error
}

type WebSocketConn

type WebSocketConn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

make websocket concurrent safe see https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency

func (*WebSocketConn) Close

func (c *WebSocketConn) Close() error

func (*WebSocketConn) ReadMessage

func (c *WebSocketConn) ReadMessage() (messageType int, p []byte, err error)

func (*WebSocketConn) WriteJSON

func (c *WebSocketConn) WriteJSON(v interface{}) error

type WebSocketReadWriteCloser

type WebSocketReadWriteCloser interface {
	WebSocketReader
	WebSocketWriter
	WebSocketCloser
}

type WebSocketReader

type WebSocketReader interface {
	ReadMessage() (messageType int, p []byte, err error)
}

type WebSocketWriter

type WebSocketWriter interface {
	//WriteControl(messageType int, data []byte, deadline time.Time) error
	//WritePreparedMessage(pm *websocket.PreparedMessage) error
	//WriteMessage(messageType int, data []byte) error
	WriteJSON(v interface{}) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to