环境:Window/Eclipse/FreeSWITCH-1.6.19源码
Java ESL调用FreeSWITCH发起呼叫
1:FreeSWITCH对外连接配置:
修改D:\VS\freeswitch-1.6.19\Win32\Debug\conf\autoload_configs\event_socket.conf.xml文件,如下:
<configuration name="event_socket.conf" description="Socket Client">
<settings>
<param name="nat-map" value="false"/>
<param name="listen-ip" value="127.0.0.1"/>
<param name="listen-port" value="8021"/>
<param name="password" value="fs"/>
</settings>
</configuration>
listen-ip:FreeSWITCH服务器IP地址
password:随便写
2:JAVA调用FreeSWITCH:
需要Jar包:org.freeswitch.esl.client-0.9.2.jar、slf4j-api-1.7.25.jar
package com.fs;
import org.freeswitch.esl.client.IEslEventListener;
import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.inbound.InboundConnectionFailure;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java esl调用FreeSWITCH发起呼叫
* @author YY
*/
public class TEST {
private static final Logger log = LoggerFactory.getLogger(TEST.class);
/**
* host/port/password配置在D:\VS\freeswitch-1.6.19\Win32\Debug\conf\
* autoload_configs目录event_socket.conf.xml文件中
*/
private static String host = "127.0.0.1";
private static int port = 8021;
private static String password = "fs";
public static Client inBand() {
final Client client = new Client();
try {
client.connect(host, port, password, 20);
} catch (InboundConnectionFailure e) {
log.error("Connect failed", e);
return null;
}
// 注册事件处理程序
client.addEventListener(new IEslEventListener() {
public void eventReceived(EslEvent event) {
// System.out.println("Event received:" + event.getEventHeaders());
if (event.getEventName().equals("CHANNEL_ANSWER")) {
// 呼叫应答事件
System.err.println("CHANNEL_ANSWER");
}
if (event.getEventName().equals("CHANNEL_BRIDGE")) {
// 一个呼叫两个端点之间的桥接事件
System.err.println("CHANNEL_BRIDGE");
}
if (event.getEventName().equals("CHANNEL_DESTROY")) {
// 销毁事件
System.err.println("CHANNEL_DESTROY");
}
if (event.getEventName().equals("CHANNEL_HANGUP_COMPLETE")) {
// 挂机完成事件
System.err.println("CHANNEL_HANGUP_COMPLETE");
}
}
public void backgroundJobResultReceived(EslEvent event) {
// String uuid = event.getEventHeaders().get("Job-UUID");
log.info("Background job result received+:" + event.getEventName() + "/" + event.getEventHeaders());// +"/"+JoinString(event.getEventHeaders())+"/"+JoinString(event.getEventBodyLines()));
}
});
client.setEventSubscriptions("plain", "all");
return client;
}
public static void main(String[] args) throws InterruptedException {
Client client = inBand();
if (client != null) {
// 呼叫1002-播放语音
client.sendSyncApiCommand("originate", "{ignore_early_media=true}user/1002 &playback(text/end.wav)");
// 呼叫手机-执行lua脚本
// client.sendSyncApiCommand("originate", "{ignore_early_media=true}sofia/gateway/fs_sg/186xxxxxxxx &lua(welcome.lua)");
// 建立1002和1000的通话
// client.sendSyncApiCommand("originate", "user/1002 &bridge(user/1000)");
client.close();
}
}
}
{ignore_early_media=true}:不设置的话,播放语音文件时会不完整
playback、bridge等App不懂的去看FreeSWITCH权威指南吧...
fs_sg:网关,打手机需要配置网关,打内部短号不需要
welcome.lua脚本功能:播放语音提示并接收用户按键数据保存到MySql话单表中...后续在写...
3:网关配置:
D:\VS\freeswitch-1.6.19\Win32\Debug\conf\sip_profiles\external目录下新建fs_sg.xml文件,内容配置如下:
<include>
<gateway name="fs_sg"><!-- 网关的名字 -->
<param name="username" value="sip账号"/>
<param name="password" value="sip密码"/>
<param name="realm" value="sip地址"/>
<param name="from-domain" value="sip地址"/>
</gateway>
</include>
4:拨号说明
软电话拨号:打FreeSWITCH内部用户可以直接打;打手机号需要配置拨号规则从网关打出去,拨号规则配置如下:
D:\VS\freeswitch-1.6.19\Win32\Debug\conf\dialplan\default下新建fs_di.xml文件(文件名随便),文件内容:
<include>
<extension name="extension-call">
<condition field="destination_number" expression="^0(\d+)$"">
<action application="bridge" data="sofia/gateway/fs_sg/$1"/>
</condition>
</extension>
</include>
软电话打手机号时需要加0来匹配上面这个拨号规则,然后从网关打出去
Java调用拨号:打FreeSWITCH内部用户可以直接打;打手机号也是从网关打出去,不需要匹配拨号规则