-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_playback.go
113 lines (85 loc) · 2.44 KB
/
server_playback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2015 Nevio Vesic
// Please check out LICENSE file for more information about what you CAN and what you CANNOT do!
// Basically in short this is a free software for you to do whatever you want to do BUT copyright must be included!
// I didn't write all of this code so you could say it's yours.
// MIT License
package examples
import (
"fmt"
. "github.com/0x19/goesl"
"os"
"runtime"
"strings"
)
var welcomeFile = "%s/media/welcome.wav"
func main() {
defer func() {
if r := recover(); r != nil {
Error("Recovered in f", r)
}
}()
// Boost it as much as it can go ...
runtime.GOMAXPROCS(runtime.NumCPU())
wd, err := os.Getwd()
if err != nil {
Error("Error while attempt to get WD: %s", wd)
os.Exit(1)
}
welcomeFile = fmt.Sprintf(welcomeFile, wd)
if s, err := NewOutboundServer(":8084"); err != nil {
Error("Got error while starting Freeswitch outbound server: %s", err)
} else {
go handle(s)
s.Start()
}
}
// handle - Running under goroutine here to explain how to handle playback ( play to the caller )
func handle(s *OutboundServer) {
for {
select {
case conn := <-s.Conns:
Notice("New incomming connection: %v", conn)
if err := conn.Connect(); err != nil {
Error("Got error while accepting connection: %s", err)
break
}
answer, err := conn.ExecuteAnswer("", false)
if err != nil {
Error("Got error while executing answer: %s", err)
break
}
Debug("Answer Message: %s", answer)
Debug("Caller UUID: %s", answer.GetHeader("Caller-Unique-Id"))
cUUID := answer.GetCallUUID()
if sm, err := conn.Execute("playback", welcomeFile, true); err != nil {
Error("Got error while executing playback: %s", err)
break
} else {
Debug("Playback Message: %s", sm)
}
if hm, err := conn.ExecuteHangup(cUUID, "", false); err != nil {
Error("Got error while executing hangup: %s", err)
break
} else {
Debug("Hangup Message: %s", hm)
}
go func() {
for {
msg, err := conn.ReadMessage()
if err != nil {
// If it contains EOF, we really dont care...
if !strings.Contains(err.Error(), "EOF") {
Error("Error while reading Freeswitch message: %s", err)
}
break
}
Debug("%s", msg)
}
}()
default:
// YabbaDabbaDooooo!
//Flintstones. Meet the Flintstones. They're the modern stone age family. From the town of Bedrock,
// They're a page right out of history. La la,lalalalala la :D
}
}
}