Java通过ESL连接FreeSWITCH

转载请注明出处

http://sunxiaodou.com/2017/08/02/Java通过ESL连接FreeSWITCH/

本文来自【孫小逗的博客】

Java通过ESL连接FreeSWITCH

创建Gradle项目

首先我们创建一个基于Gradle的Java工程,并添加FreeSWITCH ESL的Java依赖。

1
compile 'org.freeswitch.esl.client:org.freeswitch.esl.client:0.9.2'

Project

创建ESLTest.java文件,并添加如下代码:

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
private static final Logger log = LoggerFactory.getLogger(ESLTest.class);
private static final String HOST = "192.168.1.22";
private static final int PORT = 8021;
private static final String PASSWORD = "ClueCon";
private static String job_UUID = null;
public static void main(String[] args) {
InBound();
}
private static void InBound() {
Client client = new Client();
try {
client.connect(HOST, PORT, PASSWORD, 20);
System.out.println("连接成功");
} catch (Exception e) {
log.error("Connect Failed", e);
}
client.addEventListener(new IEslEventListener() {
@Override
public void eventReceived(EslEvent event) {
if (event.getEventName().equals("CHANNEL_ANSWER")) { //
System.out.println("通道应答");
} else if (event.getEventName().equals("HEARTBEAT")) {
System.out.println("收到心跳 --> " + event.getEventBodyLines());
job_UUID = client.sendAsyncApiCommand("status", null);
System.out.println("Job_UUID --> " + job_UUID);
} else if (event.getEventName().equals("CHANNEL_DESTROY")) {
System.out.println("通道销毁");
} else if (event.getEventName().equals("CHANNEL_HANGUP_COMPLETE")) {
//挂断
System.out.println("通道挂断完成");
} else if (event.getEventName().equals("CHANNEL_CREATE")) {
System.out.println("通道创建");
}
}
@Override
public void backgroundJobResultReceived(EslEvent event) {
String uuid = event.getEventHeaders().get("Job-UUID");
if (job_UUID.equals(uuid)) {
for (String s : event.getEventBodyLines()) {
System.out.println(s);
}
}
}
});
client.setEventSubscriptions("plain", "all");
}

我们使用InBound模式与FreeSWITCH进行连接,我这边的FreeSWITCH运行在192.168.1.22机器上,InBound模式默认是8021端口,密码默认为ClueCon,20为连接超时时长,单位秒。

FreeSWITCH每隔20秒会发送心跳事件,代码中收到心跳事件,会发送一个异步API去查看FreeSWITCH的状态,并返回一个Job_UUID,异步完成后通过backgroundJobResultReceived回调执行结果。

文章目录
  1. 1. Java通过ESL连接FreeSWITCH
    1. 1.1. 创建Gradle项目
|