1.pom依赖
<dependency>
<groupId>org.freeswitch.esl.client</groupId>
<artifactId>org.freeswitch.esl.client</artifactId>
<version>0.9.2</version>
</dependency>
2.java esl
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="0.0.0.0"/>
<param name="listen-port" value="8021"/>
<param name="password" value="ClueCon"/>
<param name="apply-inbound-acl" value="lan"/>
<!--<param name="stop-on-bind-error" value="true"/>-->
</settings>
</configuration>
/**
* Java esl调用FreeSWITCH发起呼叫等
*
* @author SimonHua
*/
@Controller
public class ESL {
@Autowired
CallRecordServiceImpl callRecordService;
@Autowired
CallSoundServiceImpl callSoundService;
private static final Logger log = LoggerFactory.getLogger(ESL.class);
/**
* autoload_configs目录event_socket.conf.xml文件中
*/
private static String host = "192.168.6.31";
private static int port = 8021;
private static String password = "ClueCon";
@PostConstruct
public 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) {
String type = event.getEventName();
Map<String,String> map = event.getEventHeaders();
String call_uuid = map.get("Unique-ID");
String caller_id_name = map.get("Caller-Caller-ID-Number");
String caller_id_number = map.get("Caller-Caller-ID-Name");
String destination_number = map.get("Caller-Destination-Number");
JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));
switch (type){
case EventConstant.RECORD_START:
String filepath = map.get("Record-File-Path");
CallSound callSound = new CallSound();
callSound.setCallUuid(call_uuid);
callSound.setFilePath(filepath);
callSoundService.insert(callSound);
break;
case EventConstant.RECORD_STOP:
break;
// case EventConstant.CHANNEL_ANSWER:
// break;
case EventConstant.CHANNEL_BRIDGE:
long startTime = Long.parseLong(map.get("Caller-Channel-Created-Time"));
long answerTime = Long.parseLong(map.get("Caller-Channel-Answered-Time"));
CallRecord record = new CallRecord();
record.setCallUuid(call_uuid);
record.setCallerIdName(caller_id_name);
record.setCallerIdNumber(caller_id_number);
record.setDestinationNumber(destination_number);
record.setStartStamp(startTime);
record.setAnswerStamp(answerTime);
callRecordService.insert(record);
break;
// case EventConstant.CHANNEL_DESTROY:
// break;
case EventConstant.CHANNEL_HANGUP_COMPLETE:
String hangupCause = map.get("variable_hangup_cause");
long endTime = Long.parseLong(map.get("Caller-Channel-Hangup-Time"));
CallRecord callRecord = callRecordService.findByuuid(call_uuid);
if(callRecord==null)break;
callRecord.setEndStamp(endTime);
callRecord.setUduration(endTime-callRecord.getStartStamp());
int billsec = Integer.parseInt((endTime-callRecord.getAnswerStamp())/(1000*1000)+"");
callRecord.setBillsec(billsec);
callRecord.setHangupCause(hangupCause);
callRecordService.update(callRecord);
break;
default:
break;
}
}
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;
}
}
3.git代码下载
https://github.com/15190250058/esl.git
4.event事件对照表
http://www.freeswitch.net.cn/82.html