birect

package module
v0.0.0-...-78aceae Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2017 License: MIT Imports: 16 Imported by: 0

README

Birect

Efficient, scalable and language-agnostic bidirectional realtime messaging with request/response support.

Circle CI

go-birect

Go-birect is a go implementation of birect. For all implementations, see:

  • go: github.com/marcuswestin/go-birect
  • js: github.com/marcuswestin/js-birect

Usage: Go

Server

See http://godoc.org/pkg/github.com/marcuswestin/go-birect/#example_UpgradeRequests_server

Client

See http://godoc.org/pkg/github.com/marcuswestin/go-birect/#example_Connect_client

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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."
)
View Source
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

func Connect(address string) (client *Client, err error)

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

func (c *Conn) SendJSONReq(name string, resValPtr interface{}, paramsObj interface{}) (err error)

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

func (c *Conn) SendProtoReq(name string, resValPtr Proto, paramsObj Proto) (err error)

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

func UpgradeRequests(pattern string) *Handler

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) ConnCount

func (s *Handler) ConnCount() int

ConnCount returns the number of current connections

func (*Handler) Conns

func (s *Handler) Conns() (conns []*Conn)

Conns returns all the current connections

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

func (s *Handler) ListenAndServe(address string) (errChan chan error)

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) Get

func (i Info) Get(key string) interface{}

Get returns the value of the given key

func (Info) GetString

func (i Info) GetString(key string) string

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

func (i Info) MustGetString(key string) (val string)

MustGetString returns the value of the given key as a string, or panics if there is no value.

func (Info) Set

func (i Info) Set(key string, val interface{})

Set sets the value of the given key

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

func (j *JSONReq) JSONString() string

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

type JSONReqHandler func(req *JSONReq) (resValue interface{}, err error)

JSONReqHandler functions get called on every json request

type Proto

type Proto proto.Message

Proto is an alias for proto.Message

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

func (p *ProtoReq) ParseParams(valuePtr Proto)

ParseParams parses the ProtoReq values into the given valuePtr. valuePtr should be a pointer to a struct that implements Proto.message.

type ProtoReqHandler

type ProtoReqHandler func(req *ProtoReq) (resValue Proto, err error)

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 NewServer

func NewServer() *Server

NewServer returns a new server that you are expected to

func (Server) HandleJSONReq

func (m Server) HandleJSONReq(reqName string, handler JSONReqHandler)

func (Server) HandleProtoReq

func (m Server) HandleProtoReq(reqName string, handler ProtoReqHandler)

Directories

Path Synopsis
internal
wire
Package wire is a generated protocol buffer package.
Package wire is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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