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(); }
断点调试结果: