freeswitch从启动到关闭过程中会产生大量的事件,而事件的监听都可以通过socket监听接口,去监听你想要的事件
fs事件大致分为几大类:
通道事件:比如通道建立事件
系统事件:比如发出命令事件
其他事件:比如api事件
自定义事件:比如可以根据自身需求定义事件
具体参考:https://wiki.freeswitch.org/wiki/Event_List 说得很详细,可以根据自己业务订阅感兴趣的事件
第二分部 java事件监听
1.订阅事件
根据自己的需要订阅自己感兴趣的事件,本人主要订阅以下事件(由于是用java编程,所以通过枚举,可以调整,当然如果感兴趣可以动态订阅)
a.枚举类
/**
* @author zzy
* fs通道事件类型
*/
public enum FSEventType {
//跟呼叫相关的通道事件
CHANNEL_CREATE,//通道创建事件
CHANNEL_ANSWER,//通道应答事件
CHANNEL_BRIDGE,//通道桥接事件
CHANNEL_HANGUP,//通道挂断事件
CHANNEL_HANGUP_COMPLETE,
CUSTOM,
//CHANNEL_DESTROY,
//CHANNEL_ORIGINATE,
//CHANNEL_OUTGOING,
HEARTBEAT,//心跳包
SESSION_HEARTBEAT,//计费心跳包心跳包
//
}
b.java通过socket连接fs的核心类
/**
* @author zzy
* freeswitch esl
*/
public class ESLClient {
//========esl ip地址 端口 密码===========
private String host;
private int port;
private String password;
private Client client;
private boolean isConnect ;
private TopicSender topicSender;//此类只是用来发送mq信息用来其他程序消费
public ESLClient(String host,int port,String password) {
seting(host,port,password);
}
private void seting(String host,int port,String password) {
this.host = host;
this.port = port;
this.password = password;
}
public void connect() {
try {
client = new Client();
client.connect(host, port, password, 1);
addListener();
isConnect = true;
} catch (Exception e) {
isConnect = false;
e.printStackTrace();
}
}
private void addListener() {
EslCoreEventListener myIEslEventListener = new EslCoreEventListener(topicSender);
client.addEventListener(myIEslEventListener);
client.setEventSubscriptions("plain", "all");
//不区分大小写,对事件进行过滤,只关注需要的事件
for(FSEventType fSEventType:FSEventType.values()){
client.addEventFilter("Event-Name", fSEventType.name());
}
}
public void close() {
client.close();
isConnect = false;
}
public boolean canSend() {
return client.canSend();
}
public boolean isConnect() {
return isConnect;
}
public List
List
try {
EslMessage elme = client.sendSyncApiCommand(command, "");
strList = elme.getBodyLines();
for (String string : strList) {
System.out.println(" " + string);
}
} catch (java.lang.IllegalStateException e) {
e.printStackTrace();
strList = new ArrayList
strList.add(e.getMessage());
//清除该缓存
ESLClientMap.remove(this.host);
}
return strList;
}
public String comandString(String command){
List
StringBuffer sb = new StringBuffer();
for(String str:strList){
sb.append(""+str+"");
}
return sb.toString();
}
public void setTopicSender(TopicSender topicSender) {
this.topicSender = topicSender;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public static void main(String[] args) throws Exception {
ESLClient e = new ESLClient("10.0.0.152",8021,"ClueCon");
e.connect();
System.out.println(e.isConnect());
}
}
c.事件监听类(通过事件类型做出各种判断)
/**
* @author zzy
* esl 事件监听
* 核心事件,改监听器接收fs事件队列发送消息到mq
* 核心方法被我删除 可以自己根据业务写
*/
public class EslCoreEventListener implements IEslEventListener {
private TopicSender topicSender;
public EslCoreEventListener(TopicSender topicSender) {
this.topicSender = topicSender;
}
public void backgroundJobResultReceived(EslEvent event) {
System.out.println("Background job result received [{}]"
+ event);
}
public void eventReceived(EslEvent event) {
//fs事件接收
String eventName = event.getEventName();
//System.out.println("=======eventReceived======"+event.getEventHeaders());
FSEventType fSEventType = FSEventType.valueOf(eventName);
switch (fSEventType) {
case CUSTOM://订阅事件
dealCustomEvent(event);
break;
case CHANNEL_CREATE://通道创建事件
dealChannelCreateEvent(event);
break;
case CHANNEL_HANGUP_COMPLETE://通道挂断事件
dealChannelHangupEvent(event);
break;
case HEARTBEAT://心跳包
dealBeatEvent(event);
break;
case SESSION_HEARTBEAT://计费心跳包心跳包
dealSessionHertBeatEvent(event);
break;
default:
break;
}
}
}
2 测试运行
以上几个类基本上可以监听到fs的事件
同时可以通过方法去调用esl api方法,可以支持的方法详见
https://wiki.freeswitch.org/wiki/Mod_commands
第三部分 java esl 多台fs切换
未完待续.....