eventsocket

package module
v0.0.0-...-dbc2d95 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: BSD-3-Clause Imports: 14 Imported by: 0

README

eventsocket

FreeSWITCH Event Socket library for the Go programming language.

It supports both inbound and outbound event socket connections, acting either as a client connecting to FreeSWITCH or as a server accepting connections from FreeSWITCH to control calls.

This code has not been tested in production and is considered alpha. Use at your own risk.

Installing

Make sure $GOPATH is set, and use the following command to install:

go get github.com/fiorix/go-eventsocket/eventsocket

The library is currently a single file, so feel free to drop into any project without bothering to install.

Usage

There are simple and clear examples of usage under the examples directory. A client that connects to FreeSWITCH and originate a call, pointing to an Event Socket server, which answers the call and instructs FreeSWITCH to play an audio file.

Documentation

Overview

FreeSWITCH Event Socket library for the Go programming language.

eventsocket supports both inbound and outbound event socket connections, acting either as a client connecting to FreeSWITCH or as a server accepting connections from FreeSWITCH to control calls.

Reference: http://wiki.freeswitch.org/wiki/Event_Socket http://wiki.freeswitch.org/wiki/Event_Socket_Outbound

WORK IN PROGRESS, USE AT YOUR OWN RISK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, fn HandleFunc) error

ListenAndServe listens for incoming connections from FreeSWITCH and calls HandleFunc in a new goroutine for each client.

Example:

func main() {
	eventsocket.ListenAndServe(":9090", handler)
}

func handler(c *eventsocket.Connection) {
	ev, err := c.Send("connect") // must always start with this
	ev.PrettyPrint()             // print event to the console
	...
	c.Send("myevents")
	for {
		ev, err = c.ReadEvent()
		...
	}
}

Types

type Connection

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

Connection is the event socket connection handler.

func Dial

func Dial(addr, passwd string) (*Connection, error)

Dial attempts to connect to FreeSWITCH and authenticate.

Example:

c, _ := eventsocket.Dial("localhost:8021", "ClueCon")
ev, _ := c.Send("events plain ALL") // or events json ALL
for {
	ev, _ = c.ReadEvent()
	ev.PrettyPrint()
	...
}

func (*Connection) Close

func (h *Connection) Close()

Close terminates the connection.

func (*Connection) Execute

func (h *Connection) Execute(appName, appArg string, lock bool) (*Event, error)

Execute is a shortcut to SendMsg with call-command: execute without UUID, suitable for use on outbound event socket connections (acting as server).

Example:

Execute("playback", "/tmp/test.wav", false)

See http://wiki.freeswitch.org/wiki/Event_Socket#execute for details.

func (*Connection) ExecuteUUID

func (h *Connection) ExecuteUUID(uuid, appName, appArg string) (*Event, error)

ExecuteUUID is similar to Execute, but takes a UUID and no lock. Suitable for use on inbound event socket connections (acting as client).

func (*Connection) ReadEvent

func (h *Connection) ReadEvent() (*Event, error)

ReadEvent reads and returns events from the server. It supports both plain or json, but *not* XML.

When subscribing to events (e.g. `Send("events json ALL")`) it makes no difference to use plain or json. ReadEvent will parse them and return all headers and the body (if any) in an Event struct.

func (*Connection) RemoteAddr

func (h *Connection) RemoteAddr() net.Addr

RemoteAddr returns the remote addr of the connection.

func (*Connection) Send

func (h *Connection) Send(command string) (*Event, error)

Send sends a single command to the server and returns a response Event.

See http://wiki.freeswitch.org/wiki/Event_Socket#Command_Documentation for details.

func (*Connection) SendMsg

func (h *Connection) SendMsg(m MSG, uuid, appData string) (*Event, error)

SendMsg sends messages to FreeSWITCH and returns a response Event.

Examples:

SendMsg(MSG{
	"call-command": "hangup",
	"hangup-cause": "we're done!",
}, "", "")

SendMsg(MSG{
	"call-command":     "execute",
	"execute-app-name": "playback",
	"execute-app-arg":  "/tmp/test.wav",
}, "", "")

Keys with empty values are ignored; uuid and appData are optional. If appData is set, a "content-length" header is expected (lower case!).

See http://wiki.freeswitch.org/wiki/Event_Socket#sendmsg for details.

type Event

type Event struct {
	Header EventHeader // Event headers, key:val
	Body   string      // Raw body, available in some events
}

Event represents a FreeSWITCH event.

func (*Event) Get

func (r *Event) Get(key string) string

Get returns an Event value, or "" if the key doesn't exist.

func (*Event) GetInt

func (r *Event) GetInt(key string) (int, error)

GetInt returns an Event value converted to int, or an error if conversion is not possible.

func (*Event) PrettyPrint

func (r *Event) PrettyPrint()

PrettyPrint prints Event headers and body to the standard output.

func (*Event) String

func (r *Event) String() string

type EventHeader

type EventHeader map[string]interface{}

EventHeader represents events as a pair of key:value.

type HandleFunc

type HandleFunc func(*Connection)

HandleFunc is the function called on new incoming connections.

type MSG

type MSG map[string]string

MSG is the container used by SendMsg to store messages sent to FreeSWITCH. It's supposed to be populated with directives supported by the sendmsg command only, like "call-command: execute".

See http://wiki.freeswitch.org/wiki/Event_Socket#sendmsg for details.

Directories

Path Synopsis
examples
client
Event Socket client that connects to FreeSWITCH to originate a new call.
Event Socket client that connects to FreeSWITCH to originate a new call.
server
Server that accepts connections from FreeSWITCH and controls incoming calls.
Server that accepts connections from FreeSWITCH and controls incoming calls.

Jump to

Keyboard shortcuts

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