goesl

package module
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2023 License: MPL-2.0 Imports: 15 Imported by: 0

README

goesl

PkgGoDev Go Report Card Go

FreeSWITCH™ Event Socket Library written in Go.

goesl was written base on:

Before writing this library, I was still working quite smoothly with 0x19/goesl and also used percipia/eslgo but there were a few small problems that both of them could not solve, so I had to adjust tweak it a bit.

Install

go get github.com/luandnh/goesl

Examples

Inbound ESL Client
package main

import (
	"context"
	"fmt"
	"github.com/luandnh/goesl"
	"time"
    log "github.com/sirupsen/logrus"
)

func main() {
    client, err := goesl.NewClient("127.0.0.1", 8021, "ClueCon", 10)
	if err != nil {
		fmt.Println("Error connecting", err)
		return
	}
	defer client.ExitAndClose()
    raw, err := client.Send("api sofia status")
	if err != nil {
		panic(err)
	}
	log.Info(string(raw.Body))

	time.Sleep(60 * time.Second)

    err = client.SendAsync("event json CHANNEL_CREATE CHANNEL_ANSWER CHANNEL_HANGUP_COMPLETE CHANNEL_PROGRESS_MEDIA")
	if err != nil {
		panic(err)
	}

    for {
		msg, err := client.ReadMessage()
		if err != nil {
			if !strings.Contains(err.Error(), "EOF") && err.Error() != "unexpected end of JSON input" {
				log.Error("Error while reading Freeswitch message : ", err)
			}
			continue
		}
		msgBytes, err := json.Marshal(msg)
		if err != nil {
			log.Error("marshal json error : ",err)
		}
		log.Info(string(msgBytes))
	}
}

Documentation

Index

Constants

View Source
const (
	ContentType_AuthRequest = `auth/request`
	ContentType_Reply       = `command/reply`
	ContentType_APIResponse = `api/response`
	ContentType_Disconnect  = `text/disconnect-notice`
	// for event
	ContentType_EventPlain = `text/event-plain`
	ContentType_EventJSON  = `text/event-json`
	ContentType_EventXML   = `text/event-xml`
)
View Source
const DEFAULT_TIMEOUT = time.Second * 60
View Source
const EndOfMessage = "\r\n\r\n"

Variables

View Source
var DefaultOptions = Options{
	Context: context.Background(),
	Logger:  NormalLogger{},
}

DefaultOptions - The default options used for creating the connection

Functions

func IsExistInSlice

func IsExistInSlice(s string, list []string) bool

Types

type Client

type Client struct {
	*ESLConnection
	Protocol     string
	Address      string
	Password     string
	Timeout      int
	OnDisconnect func()
}

Client - Used to create an inbound connection to Freeswitch server In order to originate call, transfer, or something amazing ...

func NewClient

func NewClient(host string, port int, password string, timeout int) (*Client, error)

NewClient - Init new client connection, this will establish connection and attempt to authenticate against connected freeswitch server

func (*Client) EstablishConnection

func (client *Client) EstablishConnection() (*ESLConnection, error)

EstablishConnection - Will attempt to establish connection against freeswitch and create new connection

type ESLConnection

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

ESLConnection

func (*ESLConnection) Api

func (c *ESLConnection) Api(cmd string) (*ESLResponse, error)

func (*ESLConnection) Authenticate

func (c *ESLConnection) Authenticate(ctx context.Context, password string) error

Authenticate - Method used to authenticate client against freeswitch.

func (*ESLConnection) BgApi

func (c *ESLConnection) BgApi(cmd string) error

func (*ESLConnection) Close

func (c *ESLConnection) Close() error

Close - Close connection

func (*ESLConnection) Dial

func (c *ESLConnection) Dial(protocol string, address string, timeout time.Duration) (net.Conn, error)

func (*ESLConnection) Exit

func (c *ESLConnection) Exit(cmd string) error

func (*ESLConnection) ExitAndClose

func (c *ESLConnection) ExitAndClose()

ExitAndClose - Send exit command before close connection

func (*ESLConnection) ParseResponse

func (c *ESLConnection) ParseResponse() (*ESLResponse, error)

func (*ESLConnection) ReadMessage

func (c *ESLConnection) ReadMessage() (*ESLResponse, error)

ReadMessage - Read message from channel and return ESLResponse

func (*ESLConnection) Send

func (c *ESLConnection) Send(cmd string) (*ESLResponse, error)

Send - Send command and get response message

func (*ESLConnection) SendAsync

func (c *ESLConnection) SendAsync(cmd string) error

SendAsync - Send command but don't get response message

func (*ESLConnection) SendEvent added in v1.0.3

func (c *ESLConnection) SendEvent(eventHeaders []string) (*ESLResponse, error)

SendEvent - Loop to passed event headers

func (*ESLConnection) SendMsg added in v1.0.5

func (c *ESLConnection) SendMsg(msg map[string]string, uuid, data string) (*ESLResponse, error)

SendEvent - Loop to passed event headers

func (*ESLConnection) SendWithContext

func (c *ESLConnection) SendWithContext(ctx context.Context, cmd string) (*ESLResponse, error)

SendWithContext - Send command and get response message with deadline

type ESLResponse

type ESLResponse struct {
	Headers map[string]string
	Body    []byte
	Err     error
}

func (*ESLResponse) GetHeader

func (r *ESLResponse) GetHeader(header string) string

GetHeader - Get header value

func (*ESLResponse) GetReply

func (r *ESLResponse) GetReply() string

GetReply - Get reply value in header

func (*ESLResponse) HasHeader

func (r *ESLResponse) HasHeader(header string) bool

GetReply - Check value in header

func (*ESLResponse) IsOk

func (r *ESLResponse) IsOk() bool

IsOk - Has prefix +OK

type Logger

type Logger interface {
	Debug(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Error(format string, args ...interface{})
}

type NilLogger

type NilLogger struct{}

func (NilLogger) Debug

func (l NilLogger) Debug(string, ...interface{})

func (NilLogger) Error

func (l NilLogger) Error(string, ...interface{})

func (NilLogger) Info

func (l NilLogger) Info(string, ...interface{})

func (NilLogger) Warn

func (l NilLogger) Warn(string, ...interface{})

type NormalLogger

type NormalLogger struct{}

func (NormalLogger) Debug

func (l NormalLogger) Debug(format string, args ...interface{})

func (NormalLogger) Error

func (l NormalLogger) Error(format string, args ...interface{})

func (NormalLogger) Info

func (l NormalLogger) Info(format string, args ...interface{})

func (NormalLogger) Warn

func (l NormalLogger) Warn(format string, args ...interface{})

type Options

type Options struct {
	Context context.Context
	Logger  Logger
}

Options - Generic options for an ESL connection, either inbound or outbound

Jump to

Keyboard shortcuts

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