outbound外聯模式下,可以參考我先前寫的文章:freeswitch: ESL中如何自定義事件及自定義事件的監聽,使用export導出變量。但是inbound模式下,ESL client並未封裝export命令,如果要給某條腿附加一個變量值,可以借助uuid_setvar命令。
一、命令行驗證
1.1 啟動freeswitch控制台,手動originate發起呼叫
originate {origination_uuid=abd2d52e-6074-4a46-aa0e-c73d04f566f6}user/1000 &park()
注:外呼freeswitch內置的1000賬號,同時指定該腿的uuid為abd2d52e-6074-4a46-aa0e-c73d04f566f6 (前提:要先用一個網絡電話程序,以1000賬號注冊到freeswitch上)

點擊Answer接通,保持這條腿的通話
1.2 設置變量
uuid_setvar abd2d52e-6074-4a46-aa0e-c73d04f566f6 test-var value-a value-b value-c
正常的話,會回顯+OK
1.3 獲取變量
uuid_getvar abd2d52e-6074-4a46-aa0e-c73d04f566f6 test-var
輸出結果,參考下圖:

二、ESL Inbound示例代碼
try {
//inbound test
final Client inboundClient = new Client();
inboundClient.connect("localhost", 8021, "ClueCon", 10);
inboundClient.setEventSubscriptions(EventFormat.PLAIN, "ALL");
inboundClient.addEventListener(new IInboundEslEventListener() {
@Override
public void onEslEvent(Context ctx, EslEvent eslEvent) {
String eventName = eslEvent.getEventName();
if (eventName.startsWith("CHANNEL")) {
if (eventName.startsWith("CHANNEL_ANSWER")) {
//接通時,設置test變量
String uuid = eslEvent.getEventHeaders().get(EslEventConstant.UNIQUE_ID);
ctx.sendAsyncApiCommand("uuid_setvar " + uuid + " test 123123123");
}
if (eventName.startsWith("CHANNEL_HANGUP_COMPLETE")) {
//掛斷時,獲取test變量
String testValue = eslEvent.getEventHeaders().get("variable_test");
System.out.println("test:" + testValue);
}
}
}
@Override
public void onDisconnect(Context ctx, EslMessage eslMsg) {
System.out.println("message:" + eslMsg);
}
});
} catch (Exception e) {
e.printStackTrace();
}
斷點調試結果:

