turnpike

package module
Version: v0.0.0-...-61cff6b Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2015 License: MIT Imports: 17 Imported by: 0

README

turnpike

Go implementation of WAMP - The Websocket Application Messaging Protocol

Turnpike provides a WAMP server and client.

Build Status

Examples

chat

Very simple chat server and client written in Dart. To run, first install Dart (Dart editor should work as well), then:

cd examples/dart
pub install
./build.sh
go run server.go

Open a browser (or more) to localhost:8080. Type in the textbox and hit enter.

hello

Connect to a Turnpike server with autobahn.js:

cd examples/hello
go run server.go

Open a browser to localhost:8080. You should see a message when autobahn.js connects to Turnpike.

Documentation

Overview

Package turnpike provides a Websocket Application Messaging Protocol (WAMP) server and client

Index

Constants

This section is empty.

Variables

View Source
var (
	// An ErrInvalidURI describes an invalid URI.
	ErrInvalidURI = &WAMPError{"invalid URI"}
	// An ErrInvalidNumArgs describes invalid number of arguments in a message.
	ErrInvalidNumArgs = &WAMPError{"invalid number of arguments in message"}
	// An ErrUnsupportedProtocol describes an unsupported WAMP protocol in a welcome message.
	ErrUnsupportedProtocol = &WAMPError{"unsupported protocol"}
)

Functions

This section is empty.

Types

type CallResult

type CallResult struct {
	// Result contains the RPC call result returned by the server.
	Result interface{}
	// Error is nil on call success otherwise it contains the RPC error.
	Error error
}

CallResult represents either a sucess or a failure after a RPC call.

type Client

type Client struct {
	// SessionId is a ID of the session in UUID4 format received at the start of the session.
	SessionId string
	// ProtocolVersion is the version of the WAMP protocol received at the start of the session.
	ProtocolVersion int
	// ServerIdent is the server ID (ie "turnpike, autobahn") received at the start of the session.
	ServerIdent string
	// contains filtered or unexported fields
}

Client represents a WAMP client that handles RPC and pub/sub.

func NewClient

func NewClient(args ...bool) *Client

NewClient creates a new WAMP client.

func (*Client) Call

func (c *Client) Call(procURI string, args ...interface{}) chan CallResult

Call makes a RPC call on the server identified by procURI (in either full URI or CURIE format) with zero or more args. Returns a channel that will receive the call result (or error) on completion.

Ref: http://wamp.ws/spec#call_message

func (*Client) Connect

func (c *Client) Connect(server, origin string) error

Connect will connect to server with an optional origin. More details here: http://godoc.org/code.google.com/p/go.net/websocket#Dial

func (*Client) Prefix

func (c *Client) Prefix(prefix, URI string) error

Prefix sets a CURIE prefix at the server for later use when interacting with the server. prefix is the first part of a CURIE (ie "calc") and URI is a full identifier (ie "http://example.com/simple/calc#") that is mapped to the prefix.

Ref: http://wamp.ws/spec#prefix_message

func (*Client) Publish

func (c *Client) Publish(topicURI string, event interface{}, opts ...interface{}) error

Publish publishes an event to the topicURI that gets sent to all subscribers of that topicURI by the server. opts can can be either empty, one boolean that can be used to exclude outself from receiving the event or two lists; the first a list of clients to exclude and the second a list of clients that are eligible to receive the event. Either list can be empty.

Ref: http://wamp.ws/spec#publish_message

func (*Client) PublishExcludeMe

func (c *Client) PublishExcludeMe(topicURI string, event interface{}) error

PublishExcludeMe is a short hand for Publish(tobicURI, event, true) that will not send the event to ourself.

func (*Client) SendingHeartbeat

func (c *Client) SendingHeartbeat() chan error

func (*Client) SetSessionOpenCallback

func (c *Client) SetSessionOpenCallback(f func(string))

SetSessionOpenCallback adds a callback function that is run when a new session begins. The callback function must accept a string argument that is the session ID.

func (*Client) Subscribe

func (c *Client) Subscribe(topicURI string, f EventHandler) error

Subscribe adds a subscription at the server for events with topicURI lasting for the session or until Unsubscribe is called.

Ref: http://wamp.ws/spec#subscribe_message

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(topicURI string) error

Unsubscribe removes a previous subscription with topicURI at the server.

Ref: http://wamp.ws/spec#unsubscribe_message

type EventHandler

type EventHandler func(topicURI string, event interface{})

EventHandler is an interface for handlers to published events. The topicURI is the URI of the event and event is the event centents.

type PubHandler

type PubHandler func(topicURI string, event interface{}) interface{}

PubHandler is an interface that handlers for publishes should implement to get notified on a client publish with the possibility to modify the event. The event that will be published should be returned.

type RPCError

type RPCError struct {
	URI         string
	Description string
	Details     interface{}
}

RPCError represents a call error and is the recommended way to return an error from a RPC handler.

func (RPCError) Error

func (e RPCError) Error() string

Error returns an error description.

type RPCHandler

type RPCHandler func(clientID string, topicURI string, args ...interface{}) (interface{}, error)

RPCHandler is an interface that handlers to RPC calls should implement. The first parameter is the call ID, the second is the proc URI. Last comes all optional arguments to the RPC call. The return can be of any type that can be marshaled to JSON, or a error (preferably RPCError but any error works.) NOTE: this may be broken in v2 if multiple-return is implemented

type RgxRPCHandler

type RgxRPCHandler struct {
	Rgx     *regexp.Regexp
	Handler RPCHandler
}

type Server

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

Server represents a WAMP server that handles RPC and pub/sub.

func NewServer

func NewServer(d ...bool) *Server

NewServer creates a new WAMP server. Can receive bool params for debug. If true, will log debug messages.

func (*Server) ConnectedClients

func (t *Server) ConnectedClients() []string

ConnectedClients returns a slice of the ids of all connected clients

func (*Server) DisconnectClient

func (t *Server) DisconnectClient(client string)

SetSessionCloseCallback adds a callback function that is run when a session ends. The callback function must accept a string argument that is the session ID.

func (*Server) HandleWebsocket

func (t *Server) HandleWebsocket(conn *websocket.Conn, additionalData interface{})

HandleWebsocket implements the go.net/websocket.Handler interface.

func (*Server) RegisterPubHandler

func (t *Server) RegisterPubHandler(uri string, f PubHandler)

RegisterPubHandler adds a handler called when a client publishes to URI. The event can be modified in the handler and the returned event is what is published to the other clients.

func (*Server) RegisterRPC

func (t *Server) RegisterRPC(uri string, f RPCHandler)

RegisterRPC adds a handler for the RPC named uri.

func (*Server) RegisterRgxRPC

func (t *Server) RegisterRgxRPC(rgx *regexp.Regexp, f RPCHandler)

RegisterRgxRPC adds a handler for the RPC in regexp uri.

func (*Server) RegisterSubHandler

func (t *Server) RegisterSubHandler(uri string, f SubHandler)

RegisterSubHandler adds a handler called when a client subscribes to URI. The subscription can be canceled in the handler by returning false, or approved by returning true.

func (*Server) SendEvent

func (t *Server) SendEvent(topic string, event interface{}, ids ...[]string)

SendEvent sends an event with topic directly (not via Client.Publish())

func (*Server) SetSessionCloseCallback

func (t *Server) SetSessionCloseCallback(f func(string))

SetSessionCloseCallback adds a callback function that is run when a session ends. The callback function must accept a string argument that is the session ID.

func (*Server) SetSessionOpenCallback

func (t *Server) SetSessionOpenCallback(f func(string, interface{}))

SetSessionOpenCallback adds a callback function that is run when a new session begins. The callback function must accept a string argument that is the session ID and the interface{} argument with additional info

func (*Server) Subscribers

func (t *Server) Subscribers(uri string) []string

Subscribers returns a slice of the ids of all subscribed to uri clients

func (*Server) UnregisterPubHandler

func (t *Server) UnregisterPubHandler(uri string)

UnregisterPubHandler removes a publish handler for the URI.

func (*Server) UnregisterRPC

func (t *Server) UnregisterRPC(uri string)

UnregisterRPC removes a handler for the RPC named uri.

func (*Server) UnregisterRgxRPC

func (t *Server) UnregisterRgxRPC(rgx *regexp.Regexp)

UnregisterRgxRPC removes a handler for the RPC in regexp uri.

func (*Server) UnregisterSubHandler

func (t *Server) UnregisterSubHandler(uri string)

UnregisterSubHandler removes a subscription handler for the URI.

type SubHandler

type SubHandler func(clientID string, topicURI string) bool

SubHandler is an interface that handlers for subscriptions should implement to control with subscriptions are valid. A subscription is allowed by returning true or denied by returning false.

type WAMPError

type WAMPError struct {
	Msg string
}

A WAMPError is returned when attempting to create a message that does not follow the WAMP protocol

func (*WAMPError) Error

func (e *WAMPError) Error() string

Error implements the error interface to provide a message.

Directories

Path Synopsis
examples
rpc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL