goppetto

package
v0.0.0-...-0d7bd71 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2014 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type EventDispatcher

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

The EventDispatcher can be used to dispatch events on incomming messages.

Create an EventDispatcher.

var ed = EventDispatcher{
	callbacks: make(map[string][]func(*EventMessage) *EventMessage),
}

Now create an callback which listens for an event with the name `set_direction`. A callback receives a pointer to EventMessage as input and must return this pointer. The pointer must be returned so we can chain callbacks.

In our example our callback receives a struct which data map contains a key `direction` which holds an string with like `north` or `south`.

func setDirection(emsg *EventMessage) *EventMessage {
	log.Println(emsg.Data['direction'])
	return emsg
}

Now bind this callback to the `set_direction`. You can bind multiple callbacks to 1 event. They will be executed asynchronously.

ed.Bind('set_direction', setDirection)

The only thing you've left to do is let the EventDispatcher listen on a channel.

messages := make(chan string)
ed.Listen(messages)

You can send messages through the channel. Messages are strings following the JSON syntax with the format:

{
	"event": <event_name>,
	"data": {
		<first_attribute>: <some_value>,
		<second_attribute>: <some_other_value>,
		...
	}
}

messages <- `{"event": "set_direction", "data": {"direction": "north"}}`

func (EventDispatcher) Bind

func (ed EventDispatcher) Bind(e string, cb func(*EventMessage) *EventMessage)

Bind a callback. Multiple callbacks can be bound to 1 event. A callback takes en EventMessages as parameter and returns an EventMessage.

func (EventDispatcher) Dispatch

func (e EventDispatcher) Dispatch(emsg *EventMessage)

Fire callbacks when event arrives. Callbacks are fired inside go routine and are therefore asynchronous.

func (EventDispatcher) Listen

func (e EventDispatcher) Listen(messages chan []byte)

Listen on channel for incomming messages and fires bound callbacks when message arrives.

type EventMessage

type EventMessage struct {
	Event string                 `json:"event"`
	Data  map[string]interface{} `json:"data"`
}

An EventMessage is a simple struct type which containts 2 attributes: `Event` and `Data`. `Event` contains a string with the name of the event. `Data` is a map with string keys, the values can be anything.

type WebSocketManager

type WebSocketManager struct {
	// A map with all connected clients.
	Connections map[string]*websocket.Conn
	// contains filtered or unexported fields
}

The WebSocketManager keeps track of all connected clients and offers ways to get updated when a client sends a message of disconnects.

The ConnectionHandler is handler for incoming WS requests.

wsm := WebSocketManager{
    make(map[string]*websocket.Conn),
    make([]func(*websocket.Conn) *websocket.Conn, 0),
    make([]func([]byte, *websocket.Conn) ([]byte, *websocket.Conn), 0),
}

http.HandleFunc("/ws", wsm.ConnectionHandler)

Use OnMessage to register a callback that is fired when a client sends a message. The callback must receive a slice of bytes and an pointer to websocket.Conn and must return both objects to make chaining of callbacks possible. Multiple callbacks can be bound and are executed in parallel.

messageCallback := func(msg []byte, conn *websocket.Conn) ([]byte, *websocket.Conn) {
    fmt.Println("Client %s send %s", conn.RemoteAddr().String(), msg)
}

wsm.OnMessage(messageCallback)

Use OnDisconnect to register a callback that is fired when a client disconnects. The callback must receive a pointer to a websocket.Conn and must return this pointer to. Multiple callbacks are executed in parallel.

disconnectCallback := func(conn *websocket.Conn) *websocket.Conn {
    fmt.Println("Client %s disconnected.", conn.RemoteAddr().String)
}

wsm.OnDisconnect(disconnectCallback)

func (*WebSocketManager) ConnectionHandler

func (wsm *WebSocketManager) ConnectionHandler(w http.ResponseWriter, r *http.Request)

A handler that can be used create new conections.

func (*WebSocketManager) OnDisconnect

func (wsm *WebSocketManager) OnDisconnect(f func(*websocket.Conn) *websocket.Conn)

Register an callback that will be executed when a clients disconnects.

func (*WebSocketManager) OnMessage

func (wsm *WebSocketManager) OnMessage(f func(msg []byte, conn *websocket.Conn) ([]byte, *websocket.Conn))

Register an callback that will be executed when a clients sends a message.

Jump to

Keyboard shortcuts

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