Documentation ¶
Index ¶
- Variables
- func LogToStdout()
- type Client
- type Conn
- func (m Conn) HandleJSONReq(reqName string, handler JSONReqHandler)
- func (m Conn) HandleProtoReq(reqName string, handler ProtoReqHandler)
- func (c *Conn) Log(args ...interface{})
- func (c *Conn) SendJSONReq(name string, resValPtr interface{}, paramsObj interface{}) (err error)
- func (c *Conn) SendProtoReq(name string, resValPtr Proto, paramsObj Proto) (err error)
- type Handler
- type Info
- type JSONReq
- type JSONReqHandler
- type Proto
- type ProtoReq
- type ProtoReqHandler
- type Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NewError creates an error with debugging information, such as stack traces, etc. NewError = errs.New // WrapError wraps an error with debugging information, such as stack traces, etc. WrapError = errs.Wrap // DefaultPublicErrorMessage will be set as the public error message for any error without one. DefaultPublicErrorMessage = "Oops! Something went wrong - please try again." )
var Log = func(conn *Conn, argv ...interface{}) {}
Log lets you control logging output.
Functions ¶
func LogToStdout ¶
func LogToStdout()
LogToStdout causes all birect connections to star logging to stdout
Types ¶
type Client ¶
type Client struct { *Conn // Temporary OnDisconnectHack func() // contains filtered or unexported fields }
Client is used register request handlers (for requests sent from the server), and to send requests to the server.
func Connect ¶
Connect connects to a birect server at address
Example (Client) ¶
conn, _ := birect.Connect("http://localhost:8097/birect/upgrade") type EchoParams struct{ Text string } type EchoResponse struct{ Text string } var par = EchoParams{"Hi!"} var res EchoResponse fmt.Println("Send:", par.Text) conn.SendJSONReq("Echo", &res, par) fmt.Println("Received:", res.Text)
Output: Send: Hi! Received: Hi!
func (Client) HandleJSONReq ¶
func (m Client) HandleJSONReq(reqName string, handler JSONReqHandler)
func (Client) HandleProtoReq ¶
func (m Client) HandleProtoReq(reqName string, handler ProtoReqHandler)
type Conn ¶
type Conn struct { Info Info // contains filtered or unexported fields }
Conn represents a persistent bi-directional connection between a birect client and a birect server.
func (Conn) HandleJSONReq ¶
func (m Conn) HandleJSONReq(reqName string, handler JSONReqHandler)
func (Conn) HandleProtoReq ¶
func (m Conn) HandleProtoReq(reqName string, handler ProtoReqHandler)
func (*Conn) Log ¶
func (c *Conn) Log(args ...interface{})
Log logs the given arguments, along with contextual information about the Conn.
func (*Conn) SendJSONReq ¶
SendJSONReq sends a request for the JSONReqHandler with the given `name`, along with the given paramsObj. When the server responds, SendJSONReq will parse the response into resValPtr.
func (*Conn) SendProtoReq ¶
SendProtoReq sends a request for the ProtoReqHandler with the given `name`, along with the given paramsObj. When the server responds, SendProtoReq will parse the response into resValPtr.
type Handler ¶
type Handler struct { ConnectHandler func(*Conn) DisconnectHandler func(*Conn) // contains filtered or unexported fields }
Handler is used register request handlers (for requests sent from clients), and to accept incoming connections from birect clients.
func UpgradeRequests ¶
UpgradeRequests will upgrade all incoming HTTP requests that match `pattern` to birect connections. Instead of using server.ListenAndServe(), you should call http.ListenAndServe()
Example (Server) ¶
listener, err := net.Listen("tcp", "localhost:8097") if err != nil { panic(err) } go http.Serve(listener, nil) server := birect.UpgradeRequests("/birect/upgrade") type EchoParams struct{ Text string } type EchoResponse struct{ Text string } server.HandleJSONReq("Echo", func(req *birect.JSONReq) (res interface{}, err error) { var par EchoParams req.ParseParams(&par) return EchoResponse{par.Text}, nil })
Output:
func (Handler) HandleJSONReq ¶
func (m Handler) HandleJSONReq(reqName string, handler JSONReqHandler)
func (Handler) HandleProtoReq ¶
func (m Handler) HandleProtoReq(reqName string, handler ProtoReqHandler)
func (*Handler) ListenAndServe ¶
ListenAndServe will start listening to the given address and upgrading any incoming http requests to websocket and birect connections.
type Info ¶
type Info map[string]interface{}
Info allows for associating data with any given connection
func (Info) GetString ¶
GetString returns the value of the given key as a string. If the value of key is not a string, GetString will panic.
func (Info) MustGetString ¶
MustGetString returns the value of the given key as a string, or panics if there is no value.
type JSONReq ¶
type JSONReq struct { Conn *Conn // contains filtered or unexported fields }
JSONReq wraps a request sent via SendJSONReq. Use ParseParams to access the JSON values.
func (*JSONReq) JSONString ¶
JSONString returns the request params data as a JSON string
func (*JSONReq) ParseParams ¶
func (j *JSONReq) ParseParams(valuePtr interface{})
ParseParams parses the JSONReq values into the given valuePtr. valuePtr should be a pointer to a struct that can be JSON-parsed, e.g
type params struct { Foo string } var p params jsonReq.ParseParams(&p)
type JSONReqHandler ¶
JSONReqHandler functions get called on every json request
type ProtoReq ¶
type ProtoReq struct { *Conn // contains filtered or unexported fields }
ProtoReq wraps a request sent via SendProtoReq. Use ParseParams to access the proto values.
func (ProtoReq) HandleJSONReq ¶
func (m ProtoReq) HandleJSONReq(reqName string, handler JSONReqHandler)
func (ProtoReq) HandleProtoReq ¶
func (m ProtoReq) HandleProtoReq(reqName string, handler ProtoReqHandler)
func (*ProtoReq) ParseParams ¶
ParseParams parses the ProtoReq values into the given valuePtr. valuePtr should be a pointer to a struct that implements Proto.message.
type ProtoReqHandler ¶
ProtoReqHandler functions get called on every proto request
type Server ¶
type Server struct {
*Handler
}
Server allows you to create a standalone birect upgrade server. Call ListenAndServe to start upgrading all incoming http requests.
func (Server) HandleJSONReq ¶
func (m Server) HandleJSONReq(reqName string, handler JSONReqHandler)
func (Server) HandleProtoReq ¶
func (m Server) HandleProtoReq(reqName string, handler ProtoReqHandler)