FreeSwitcch (java 使⽤)
FreeSwitcch 安装(此处以Win 系统为例)
FreeSwitcch 配置
默认安装位置:C:\Program Files\FreeSWITCH
⽤户连接配置
FreeSwitch默认设置了20个⽤户,如果需要更多的⽤户,那么只需要简单的三步就可以完成。
在conf/directory/default/中增加⼀个⽤户配置⽂件 修改拨号计划(Dialplan)使其它⽤户可以呼叫它
重新加载配置使其⽣效
要添加⽤户Jason,分机号是1020,只需要到conf/directory/default⽬录下,将l拷贝到l,然后打开l,将所有1000都改为1020,并把effective_caller_id_name的值改为Jason,然后保存退出。
如:
接下来,打开 conf/l,到
<condition fied=“destionation_number” expression=“^(10[01][0-9]) $” >
⾏,将其改为:
<condition field=“destionation_number”
expresstion=“^(10[01][0-9]|1020) $” >
保存退出,回到控制台,然后执⾏reloadxml命令或按快捷键F6,使新的配置⽣效,那么新⽤户1020便添加成功。
freeSwitch 的外呼配置
1.到FreeSWITCH\conf\sip_l⽂件
3.到FreeSWITCH\conf\autoload_f.xml⽂件
FreeSwitcch 使⽤
SIP 软件的使⽤
前端demo
javaj 和FreeSwitch 连接(esl )
包引⼊//第133⾏左右将 <param name="local-network-acl" value="localnet.auto"/>// 改为 <param name="local-network-acl" value="lan"/> <!-- <param name ="local-network-acl" value ="localnet.auto"/> --> <param name ="local-network-acl" value ="lan"/>
1
2
3
4//约314⾏ <X -PRE -PROCESS cmd ="stun-set" data ="external_sip_ip=你的ip"/>
1
2//将12⾏-16⾏,改成如下 <list name ="lan" default ="allow"> <!-- <node type ="deny" cidr ="192.168.42.0/24"/> --> <!-- <node type ="allow" cidr ="192.168.42.42/32"/> --> <node type ="allow" cidr ="0.0.0.0/0"/> </list >
1
2
3
4
5
6
7
主要程序连接<dependency > <groupId >org .freeswitch .esl .client </groupId > <artifactId >org .freeswitch .esl .client </artifactId > <version >0.9.2</version ></dependency >
1
2
3
4
5package ;
import IEslEventListener ;import Client ;import InboundConnectionFailure ;import EslEvent ; /** * @author 菩提树下的杨过 */public class InboundApp { public static void main (String [] args ) throws InterruptedException { Client client = new Client (); try { //连接freeswitch client .connect ("localhost", 8021, "ClueCon", 10); client .addEventListener (new IEslEventListener () { @Override public void eventReceived (EslEvent event ) { String eventName = event .getEventName (); //这⾥仅演⽰了CHANNEL_开头的⼏个常⽤事件 if (eventName .startsWith ("CHANNEL_")) { String calleeNumber = event .getEventHeaders ().get ("Caller-Callee-ID-Number"); String callerNumber = event .getEventHeaders ().get ("Caller-Caller-ID-Number"); switch (eventName ) { case "CHANNEL_CREATE": System .out .println ("发起呼叫, 主叫:" + callerNumber + " , 被叫:" + calleeNumber ); break ; case "CHANNEL_BRIDGE": System .out .println ("⽤户转接, 主叫:" + callerNumber + " , 被叫:" + calleeNumber ); break ; case "CHANNEL_ANSWER": System .out .println ("⽤户应答, 主叫:" + callerNu
mber + " , 被叫:" + calleeNumber ); break ; case "CHANNEL_HANGUP": String response = event .getEventHeaders ().get ("variable_current_application_response"); String hangupCause = event .getEventHeaders ().get ("Hangup-Cause"); System .out .println ("⽤户挂断, 主叫:" + callerNumber + " , 被叫:" + calleeNumber + " , response:" + response + " ,hangup cause:" break ; default : break ; } } } @Override public void backgroundJobResultReceived (EslEvent event ) { String jobUuid = event .getEventHeaders ().get ("Job-UUID"); System .out .println ("异步回调:" + jobUuid ); } }); client .setEventSubscriptions ("plain", "all"); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
代码稍微解释⼀下:
a)18⾏,连接fs的⽤户名、密码、端⼝,可以在freeswitch安装⽬录下conf/autoload_configs/f.xml
到
强烈建议,把第4⾏listen-ip改成0.0.0.0(或具体的本机ip地址),默认的::是ipv6格式,很多情况会导致esl
client连接失败,改成0.0.0.0相当于强制使⽤ipv4
b) 考虑到⽹络可能发⽣抖动,在发送命令前,建议参考60⾏的做法,先判断canSend()
c) 61⾏,client.sendAsyncApiCommand
java switch case string这⾥以异步⽅式,发送了⼀个命令给fs(即:呼叫1000⽤户,接通后再放段声⾳)。异步⽅式下,命令是否发成功当时并不知道,但是这个⽅法会返回⼀个uuid的字符串,fs收到后,会在backgroundJobResultReceived回调中,把这个uuid再还回来,参见上⾯贴出的输出结果。(基于这个机制,可以做些重试处理,⽐如:先把uuid存下来,如果约定的时间内,uuid异步回调还没回来,可以视为发送失败,再发⼀次)
重要提⽰:esl java client
0.9.2这个版本,inbound模式下,长时间使⽤有内存泄露问题,⽹上有很多这个介绍及修复办法,建议⽣产环境使⽤前,先修改esl client的源码。
需要注意新⽤户的添加和SIP的开启,只有满⾜呼叫⽤户都是在⽤户列表中的,且被呼叫对象已经注册,才回呼叫成功 java后端动态⽣成⽤户的⽅法 需要注意的是,这个⽣成需要以管理员⾝份运⾏IDEA //这⾥必须检查,防⽌⽹络抖动时,连接断开 if (client .canSend ()) { S
ystem .out .println ("连接成功,准备发起呼叫..."); //(异步)向1000⽤户发起呼叫,⽤户接通后,播放⾳乐/tmp/demo1.wav String callResult = client .sendAsyncApiCommand ("originate", "user/1000 &playback(/tmp/demo.wav)"); System .out .println ("api uuid:" + callResult ); } } catch (InboundConnectionFailure inboundConnectionFailure ) { System .out .println ("连接失败!"); inboundConnectionFailure .printStackTrace (); } }}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71<configuration name="f" 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="loopback.auto"/>--> <!--<param name="stop-on-bind-error" value="true"/>--> </settings></configuration>
1
2
3
4
5
6
7
8
9
10
查看对象是否注册:sofia status profile internal reg import File ;import FileInputStream ;import File
OutputStream ;import IOException ;public class FreeSwitchUtils { public static void CreateFreeswitchUser (Long sipId ) throws IOException , InterruptedException { String template = "C:\\Program Files\\FreeSWITCH\\conf\\directory\\default\\"; String templateContent = read (template + "l"); //创建指定⽤户 String newUser = StringUtils .leftPad (sipId + "", 3, '0'); String newContent = templateContent .replaceAll ("1000", newUser ); String newFileName = template + newUser + ".xml"; write (newFileName , newContent ); System .out .println (newFileName + " done!"); } static String read (String fileName ) throws IOException { File f = new File (fileName ); if (f .exists ()) { FileInputStream fs = new FileInputStream (f ); String result = null ; byte [] b = new byte [fs .available ()]; fs .read (b ); fs .close (); result = new String (b ); return result ; } else { return null ; } } static void write (String fileName , String fileContent ) throws IOException { File f = new File (fileName ); if (f .exists ()) { f .delete (); } FileOutputStream fs = new FileOutputStream (f ); fs .write (fileContent .getBytes ()); fs .flush (); fs .close (); }}
1234567891011121314151617181920212223242526272829303132333435363738394041424344
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论