Documentation ¶
Index ¶
- Constants
- Variables
- func NewWebSocketConn(rw *websocket.Conn) transport.ReadWriteCloser
- type Client
- type ClientHandler
- type ConnState
- type NopClient
- func (srv *NopClient) OnClose(conn WebSocketReadWriteCloser) error
- func (srv *NopClient) OnError(conn WebSocketReadWriteCloser, err error) error
- func (srv *NopClient) OnHTTPResponse(resp *http.Response) error
- func (srv *NopClient) OnHandshake(w http.ResponseWriter, r *http.Request) error
- func (srv *NopClient) OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error
- func (srv *NopClient) OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)
- func (srv *NopClient) OnOpen(conn WebSocketReadWriteCloser) error
- type NopServer
- func (srv *NopServer) OnClose(conn WebSocketReadWriteCloser) error
- func (srv *NopServer) OnError(conn WebSocketReadWriteCloser, err error) error
- func (srv *NopServer) OnHTTPResponse(resp *http.Response) error
- func (srv *NopServer) OnHandshake(w http.ResponseWriter, r *http.Request) error
- func (srv *NopServer) OnMsgHandle(conn WebSocketReadWriteCloser, msg interface{}) error
- func (srv *NopServer) OnMsgRead(conn WebSocketReadWriteCloser) (msg interface{}, err error)
- func (srv *NopServer) OnOpen(conn WebSocketReadWriteCloser) error
- type OnCloseHandler
- type OnCloseHandlerFunc
- type OnErrorHandler
- type OnErrorHandlerFunc
- type OnHTTPResponseHandler
- type OnHTTPResponseHandlerFunc
- type OnHandshakeHandler
- type OnHandshakeHandlerFunc
- type OnMsgHandleHandler
- type OnMsgHandleHandlerFunc
- type OnMsgReadHandler
- type OnMsgReadHandlerFunc
- type OnOpenHandler
- type OnOpenHandlerFunc
- type Server
- type ServerHandler
- type WebSocketCloser
- type WebSocketConn
- type WebSocketReadWriteCloser
- type WebSocketReader
- type WebSocketWriter
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 ¶
OnHandshake takes over the http handler
type ClientHandler ¶
type ClientHandler interface { OnHTTPResponseHandler OnOpenHandler OnMsgReadHandler OnMsgHandleHandler OnCloseHandler OnErrorHandler }
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 )
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 (*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 (*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 ¶
func (f OnCloseHandlerFunc) OnClose(conn WebSocketReadWriteCloser) error
type OnErrorHandler ¶
type OnErrorHandler interface {
OnError(conn WebSocketReadWriteCloser, err error) error
}
type OnErrorHandlerFunc ¶
type OnErrorHandlerFunc func(conn WebSocketReadWriteCloser, err error) error
func (OnErrorHandlerFunc) OnError ¶
func (f OnErrorHandlerFunc) OnError(conn WebSocketReadWriteCloser, err error) error
type OnHTTPResponseHandler ¶
type OnHTTPResponseHandlerFunc ¶
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 ¶
func (f OnHandshakeHandlerFunc) OnHandshake(w http.ResponseWriter, r *http.Request) error
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 ¶
func (f OnOpenHandlerFunc) OnOpen(conn WebSocketReadWriteCloser) error
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())
type ServerHandler ¶
type ServerHandler interface { OnHandshakeHandler OnOpenHandler OnMsgReadHandler //Block OnMsgHandleHandler //Unblock OnCloseHandler OnErrorHandler }
type WebSocketCloser ¶
type WebSocketCloser interface {
Close() error
}
type WebSocketConn ¶
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 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 }
Click to show internal directories.
Click to hide internal directories.