Documentation ¶
Index ¶
- Constants
- func GenerateSessionKey() string
- func NewEmptyRequestMessage(msgType byte, id [8]byte) (msg []byte)
- func NewNamelessRequestMessage(reqType byte, id [8]byte, payload []byte) (msg []byte)
- func NewReplyMessage(requestID [8]byte, payload Payload) (msg []byte)
- func NewRequestMessage(id [8]byte, name string, payload Payload) (msg []byte)
- func NewSignalMessage(name string, payload Payload) (msg []byte)
- type Client
- type ContextKey
- type Error
- type Hooks
- type Message
- type Options
- type Payload
- type PayloadEncoding
- type Server
- type Session
Constants ¶
const ( // MsgMinLenSignal represents the minimum binary/UTF8 encoded signal message length MsgMinLenSignal = int(3) // MsgMinLenSignalUtf16 represents the minimum UTF16 encoded signal message length MsgMinLenSignalUtf16 = int(4) // MsgMinLenRequest represents the minimum binary/UTF8 encoded request message length MsgMinLenRequest = int(11) // MsgMinLenRequestUtf16 represents the minimum UTF16 encoded request message length MsgMinLenRequestUtf16 = int(12) // MsgMinLenReply represents the minimum binary/UTF8 encoded reply message length MsgMinLenReply = int(9) // MsgMinLenReplyUtf16 represents the minimum UTF16 encoded reply message length MsgMinLenReplyUtf16 = int(10) // MsgMinLenErrorReply represents the minimum error reply message length MsgMinLenErrorReply = int(10) // MsgMinLenRestoreSession represents the minimum session restoration request message length MsgMinLenRestoreSession = int(10) // MsgMinLenCloseSession represents the minimum session destruction request message length MsgMinLenCloseSession = int(9) // MsgMinLenSessionCreated represents the minimum session creation notification message length MsgMinLenSessionCreated = int(2) // MsgMinLenSessionClosed represents the minimum session creation notification message length MsgMinLenSessionClosed = int(1) )
const ( // MsgErrorReply is sent by the server // and represents an error-reply to a previously sent request MsgErrorReply = byte(0) // MsgSessionCreated is sent by the server // to notify the client about the session creation MsgSessionCreated = byte(21) // MsgSessionClosed is sent by the server // to notify the client about the session destruction MsgSessionClosed = byte(22) // MsgCloseSession is sent by the client // and represents a request for the destruction of the currently active session MsgCloseSession = byte(31) // MsgRestoreSession is sent by the client // to request session restoration MsgRestoreSession = byte(32) // MsgSignalBinary represents a signal with binary payload MsgSignalBinary = byte(63) // MsgSignalUtf8 represents a signal with UTF8 encoded payload MsgSignalUtf8 = byte(64) // MsgSignalUtf16 represents a signal with UTF16 encoded payload MsgSignalUtf16 = byte(65) // MsgRequestBinary represents a request with binary payload MsgRequestBinary = byte(127) // MsgRequestUtf8 represents a request with a UTF8 encoded payload MsgRequestUtf8 = byte(128) // MsgRequestUtf16 represents a request with a UTF16 encoded payload MsgRequestUtf16 = byte(129) // MsgReplyBinary represents a reply with a binary payload MsgReplyBinary = byte(191) // MsgReplyUtf8 represents a reply with a UTF8 encoded payload MsgReplyUtf8 = byte(192) // MsgReplyUtf16 represents a reply with a UTF16 encoded payload MsgReplyUtf16 = byte(193) )
Variables ¶
This section is empty.
Functions ¶
func GenerateSessionKey ¶
func GenerateSessionKey() string
GenerateSessionKey returns a URL-safe, base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.
func NewEmptyRequestMessage ¶
NewEmptyRequestMessage composes a new request message consisting only of the type and identifier and returns its binary representation
func NewNamelessRequestMessage ¶
NewNamelessRequestMessage composes a new nameless (initially without a name) request message and returns its binary representation
func NewReplyMessage ¶
NewReplyMessage composes a new reply message and returns its binary representation
func NewRequestMessage ¶
NewRequestMessage composes a new named request message and returns its binary representation
func NewSignalMessage ¶
NewSignalMessage composes a new named signal message and returns its binary representation
Types ¶
type Client ¶
type Client struct { Session *Session // contains filtered or unexported fields }
Client represents a client connected to the server
func (*Client) CloseSession ¶
CloseSession destroys the currently active session for this client. It automatically synchronizes the session destruction to the client. The synchronization happens asynchronously using a signal and doesn't block the calling goroutine. Does nothing if there's no active session
func (*Client) ConnectionTime ¶
ConnectionTime returns the time when the connection was established
func (*Client) CreateSession ¶
CreateSession creates a new session for this client. It automatically synchronizes the new session to the remote client. The synchronization happens asynchronously using a signal and doesn't block the calling goroutine. Returns an error if there's already another session active
func (*Client) RemoteAddr ¶
RemoteAddr returns the address of the client. Returns empty string if the client is not connected.
type ContextKey ¶
type ContextKey int
ContextKey represents the identifiers of objects passed to the handlers context
const ( // Msg identifies the message object stored in the handler context Msg ContextKey = iota )
type Hooks ¶
type Hooks struct { // OnOptions is an optional hook. // It's invoked when the websocket endpoint is examined by the client // using the HTTP OPTION method. OnOptions func(resp http.ResponseWriter) // OnClientConnected is an optional hook. // It's invoked when a new client establishes a connection to the server OnClientConnected func(client *Client) // OnClientDisconnected is an optional hook. // It's invoked when a client closes the connection to the server OnClientDisconnected func(client *Client) // OnSignal is a required hook. // It's invoked when the webwire server receives a signal from the client OnSignal func(ctx context.Context) // OnRequest is an optional hook. // It's invoked when the webwire server receives a request from the client. // It must return either a response payload or an error OnRequest func(ctx context.Context) (response Payload, err *Error) // OnSessionCreated is a required hook for sessions to be supported. // It's invoked right after the synchronisation of the new session to the remote client. // The WebWire server isn't responsible for permanently storing the sessions it creates, // it's up to the user to save the given session in this hook either to a database, // a filesystem or any other kind of persistent or volatile storage // for OnSessionLookup to later be able to restore it by the session key. // If OnSessionCreated fails returning an error then the failure is logged // but the session isn't destroyed and remains active. // The only consequence of OnSessionCreation failing is that the server won't be able // to restore the session after the client is disconnected OnSessionCreated func(client *Client) error // OnSessionLookup is a required hook for sessions to be supported. // It's invoked when the server is looking for a specific session given its key. // The user is responsible for returning the exact copy of the session object // associated with the given key for sessions to be restorable. // If OnSessionLookup fails returning an error then the failure is logged OnSessionLookup func(key string) (*Session, error) // OnSessionClosed is a required hook for sessions to be supported. // It's invoked when the active session of the given client // is closed (thus destroyed) either by the server or the client himself. // The user is responsible for removing the current session of the given client // from its storage for the session to be actually and properly destroyed. // If OnSessionClosed fails returning an error then the failure is logged OnSessionClosed func(client *Client) error }
Hooks represents all callback hook functions
func (*Hooks) SetDefaults ¶
func (hooks *Hooks) SetDefaults()
SetDefaults sets undefined required hooks
type Message ¶
type Message struct { Name string Payload Payload Client *Client // contains filtered or unexported fields }
Message represents a WebWire protocol message
type Options ¶
Options represents the options for a headed server setup
func (*Options) SetDefaults ¶
func (opts *Options) SetDefaults()
SetDefaults sets default values to undefined options
type Payload ¶
type Payload struct { Encoding PayloadEncoding Data []byte }
Payload represents an encoded message payload
type PayloadEncoding ¶
type PayloadEncoding int
PayloadEncoding represents the type of encoding of the message payload
const ( // EncodingBinary represents unencoded binary data EncodingBinary PayloadEncoding = iota // EncodingUtf8 represents UTF8 encoding EncodingUtf8 // EncodingUtf16 represents UTF16 encoding EncodingUtf16 )
func (PayloadEncoding) String ¶
func (enc PayloadEncoding) String() string
type Server ¶
type Server struct { SessionRegistry sessionRegistry // contains filtered or unexported fields }
Server represents a headless WebWire server instance, where headless means there's no HTTP server that's hosting it
type Session ¶
type Session struct { Key string `json:"key"` CreationDate time.Time `json:"crt"` Info interface{} `json:"inf"` }
Session represents a session object. If the key is empty the session is invalid. Info can contain arbitrary attached data
func NewSession ¶
func NewSession(info interface{}) Session
NewSession generates a new session object generating a cryptographically random secure key