今天试了试github.com/0x19/goesl,记录如下:
下载并安装go,放在/usr/bin/go目录,步骤略
go配置:
export PATH=$PATH:/usr/bin/go/bin
export GOROOT=/usr/bin/go
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
编辑go.mod:
module goesldemo
go 1.18
require github.com/0x19/goesl v0.0.0-20191107044804-3efcc2f41ccb
require github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
编辑main.go:
package main
// https://pkg.go.dev/github.com/0x19/goesl#section-documentation
// https://pkg.go.dev/github.com/percipia/eslgo#section-documentation
import (
"flag"
// "fmt"
"github.com/0x19/goesl"
"runtime"
"strings"
)
var (
fshost = flag.String("fshost", "127.0.0.1", "Freeswitch hostname. Default: localhost")
fsport = flag.Uint("fsport", 8021, "Freeswitch port. Default: 8021")
password = flag.String("pass", "ClueCon", "Freeswitch password. Default: ClueCon")
timeout = flag.Int("timeout", 10, "Freeswitch conneciton timeout in seconds. Default: 10")
)
// Small client that will first make sure all events are returned as JSON and second, will originate
func main() {
// Boost it as much as it can go ...
runtime.GOMAXPROCS(runtime.NumCPU())
client, err := goesl.NewClient(*fshost, *fsport, *password, *timeout)
if err != nil {
goesl.Error("Error while creating new client: %s", err)
return
}
goesl.Debug("Yuhu! New client: %q", client)
// Apparently all is good... Let us now handle connection :)
// We don't want this to be inside of new connection as who knows where it my lead us.
// Remember that this is crutial part in handling incoming messages :)
go client.Handle()
client.Send("events json ALL")
// client.BgApi(fmt.Sprintf("originate %s %s", "sofia/internal/1001@127.0.0.1", "&socket(192.168.1.2:8084 async full)"))
for {
msg, err := client.ReadMessage()
if err != nil {
// If it contains EOF, we really dont care...
if !strings.Contains(err.Error(), "EOF") && err.Error() != "unexpected end of JSON input" {
goesl.Error("Error while reading Freeswitch message: %s", err)
}
break
}
goesl.Debug("%s", msg)
}
}
go mod tidy && go run main.go # 或者 go build && ./goesldemo
参考下面这个go程序,并略有改动:
https://github.com/0x19/goesl/blob/master/examples/client.go