Documentation ¶
Overview ¶
Package esl is another library for working with the FreeSWITCH server on Golang.
In this version, only the client connection is supported, and the outbound connection has not yet been implemented, because it simply was not necessary.
Example ¶
package main import ( "fmt" "github.com/mdigger/esl" ) func main() { // initialize buffered events channel events := make(chan esl.Event, 1) // connect to FreeSWITCH & init events channel with auto-close flag client, err := esl.Connect("10.10.61.76", "ClueCon", esl.WithEvents(events, true)) if err != nil { panic(err) } defer client.Close() // send a command msg, err := client.API("show calls count") if err != nil { panic(err) } fmt.Println(msg) // subscribe to BACKGROUND_JOB events if err = client.Subscribe("BACKGROUND_JOB"); err != nil { panic(err) } // send a background command if err = client.JobWithID("uptime s", "test-xxx"); err != nil { panic(err) } // read events for ev := range events { fmt.Println(ev.Name(), ev.Get("Job-UUID")) } }
Output:
Index ¶
- Variables
- type Client
- func (c *Client) API(command string) (string, error)
- func (c *Client) Close() error
- func (c *Client) DivertEvents(on ...bool) error
- func (c *Client) Done() <-chan struct{}
- func (c *Client) Filter(eventHeader, valueToFilter string) error
- func (c *Client) FilterDelete(eventHeader, valueToFilter string) error
- func (c *Client) Job(command string) (id string, err error)
- func (c *Client) JobWithID(command, id string) error
- func (c *Client) MyEvent(uuid string) error
- func (c *Client) SendEvent(name string, headers map[string]string, body string) error
- func (c *Client) SendMsg(uuid string, headers map[string]string, body string) error
- func (c *Client) Subscribe(names ...string) error
- func (c *Client) Unsubscribe(names ...string) error
- type Event
- func (e Event) Body() string
- func (e Event) ContentLength() int
- func (e Event) ContentType() string
- func (e Event) Get(key string) string
- func (e Event) LogValue() slog.Value
- func (e Event) MarshalJSON() ([]byte, error)
- func (e Event) Name() string
- func (e Event) Sequence() int64
- func (e Event) String() string
- func (e Event) Timestamp() time.Time
- func (e Event) Variable(name string) string
- func (e Event) WriteTo(w io.Writer) (int64, error)
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( DialTimeout = time.Second * 5 AuthTimeout = time.Second * 2 )
Default timeout options.
var ( ErrMissingAuthRequest = errors.New("missing auth request") ErrAccessDenied = errors.New("access denied") ErrInvalidPassword = errors.New("invalid password") ErrTimeout = errors.New("timeout") )
Authentication errors.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a client FreeSWITCH connection.
func Connect ¶
Connect connects to the given address with an optional password and options.
The address should include the host and port. If the port is missing, the default port 8021 will be used. The password is optional and can be empty. The options are variadic and can be used to customize the connection.
Returns a new Client and an error if there was a failure in connecting.
func (*Client) API ¶
API sends a command to the API and returns the response body or an error.
Send a FreeSWITCH API command, blocking mode. That is, the FreeSWITCH instance won't accept any new commands until the api command finished execution.
func (*Client) DivertEvents ¶
The divert_events switch is available to allow events that an embedded script would expect to get in the inputcallback to be diverted to the event socket.
An inputcallback can be registered in an embedded script using setInputCallback(). Setting divert_events to "on" can be used for chat messages like gtalk channel, ASR events and others.
func (*Client) Done ¶ added in v0.2.3
func (c *Client) Done() <-chan struct{}
Done returns a channel that will be closed when the client connection is closed.
func (*Client) Filter ¶
Filter performs a filter operation on the Client.
Specify event types to listen for. Note, this is not a filter out but rather a "filter in," that is, when a filter is applied only the filtered values are received. Multiple filters on a socket connection are allowed.
You can filter on any of the event headers. To filter for a specific channel you will need to use the uuid:
filter Unique-ID d29a070f-40ff-43d8-8b9d-d369b2389dfe
This method is an alternative to the myevents event type. If you need only the events for a specific channel then use myevents, otherwise use a combination of filters to narrow down the events you wish to receive on the socket.
To filter multiple unique IDs, you can just add another filter for events for each UUID. This can be useful for example if you want to receive start/stop-talking events for multiple users on a particular conference.
func (*Client) FilterDelete ¶
FilterDelete removes a filter from the Client.
Specify the events which you want to revoke the filter. filter delete can be used when some filters are applied wrongly or when there is no use of the filter.
func (*Client) Job ¶
Job sends a background command and returns the job-ID.
Send a FreeSWITCH API command, non-blocking mode. This will let you execute a job in the background.
The same API commands available as with the api command, however the server returns immediately and is available for processing more commands.
When the command is done executing, FreeSWITCH fires an event with the result and you can compare that to the Job-UUID to see what the result was. In order to receive this event, you will need to subscribe to BACKGROUND_JOB events.
func (*Client) JobWithID ¶
JobWithID sends a background command with a specified ID.
Send a FreeSWITCH API command, non-blocking mode. This will let you execute a job in the background, and the result will be sent as an event with an indicated UUID to match the reply to the command.
When the command is done executing, FreeSWITCH fires an event with the result and you can compare that to the Job-UUID to see what the result was. In order to receive this event, you will need to subscribe to BACKGROUND_JOB events.
func (*Client) MyEvent ¶
The 'myevents' subscription allows your inbound socket connection to behave like an outbound socket connect. It will "lock on" to the events for a particular uuid and will ignore all other events, closing the socket when the channel goes away or closing the channel when the socket disconnects and all applications have finished executing.
Once the socket connection has locked on to the events for this particular uuid it will NEVER see any events that are not related to the channel, even if subsequent event commands are sent. If you need to monitor a specific channel/uuid and you need watch for other events as well then it is best to use a filter.
func (*Client) SendMsg ¶
SendMsg is used to control the behavior of FreeSWITCH. UUID is mandatory, and it refers to a specific call (i.e., a channel or call leg or session).
func (*Client) Subscribe ¶
Subscribe is a function that subscribes the client to events with the given names.
You may specify any number events on the same line that should be separated with spaces.
Subsequent calls to event won't override the previous event sets.
func (*Client) Unsubscribe ¶
Unsubscribe unsubscribes the client from one or more events.
Suppress the specified type of event. If name is empty then all events will be suppressed.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents an ESL event with headers and a body.
func NewEvent ¶
NewEvent returns a new Event with the given name, headers and body.
It panics if the name is empty or if the name is CUSTOM without the Event-Subclass name.
func (Event) ContentLength ¶
ContentLength returns the length of the body in the Event.
func (Event) ContentType ¶
ContentType returns the content type of the event.
func (Event) LogValue ¶
LogValue returns the log value of the Event.
It returns a slog.Value that contains the name and sequence of the Event.
func (Event) MarshalJSON ¶
MarshalJSON is a Go function that marshals the Event to JSON.
type Option ¶
type Option func(*config)
Option is a function type used to modify configuration options.
func WithDumpIn ¶
WithDumpIn sets the writer to record all incoming messages. It is used for logging during debugging.
func WithDumpOut ¶
WithDumpOut sets the writer to record all outgoing commands. It is used for logging during debugging.
func WithEvents ¶
WithEvents returns an Option that sets the events channel of a config.
If the autoClose parameter is specified, the channel will be automatically closed when the connection to the server is closed.