阅读 184

基于 FreeSWITCH XML-RPC 开发 软交换

最近回头看了一下 FreeSWITCH 的 官方文档资料,发现它还有个 mod_xml_rpc 模块,然后随便点了一下,发现了基于RPC模块的接口,于是模仿 官方文档 写了一些例子。

由于笔者一直以来使用的技术栈是vue和springboot,然而官方文档已经有了基于JAVA的例子,所里这里写一个基于JS的。

首先我们看一下npmjs上有啥好的轮子...www.npmjs.com/package/xml…

一、环境准备

1.1 构建freeswitch镜像

使用docker提供一致的环境:

FROM debian:buster

USER root

RUN apt-get update && apt-get install -y gnupg2 wget lsb-release && \
    wget -O - https://files.freeswitch.org/repo/deb/debian-release/fsstretch-archive-keyring.asc | apt-key add - && \
    echo "deb http://files.freeswitch.org/repo/deb/debian-release/ `lsb_release -sc` main" > /etc/apt/sources.list.d/freeswitch.list  && \
    echo "deb-src http://files.freeswitch.org/repo/deb/debian-release/ `lsb_release -sc` main" >> /etc/apt/sources.list.d/freeswitch.list  && \
    apt-get update && apt-get install -y freeswitch-meta-all && \
    apt-get clean && rm -rf /var/lib/apt/lists/* /srv/* /var/srv/* && \
    sed -i 's/default_password=1234/default_password=12345/g' /etc/freeswitch/vars.xml && \
    sed -i 's/User=freeswitch/User=root/g' /lib/systemd/system/freeswitch.service && \
    sed -e '16 a <load module="mod_xml_rpc"/>' -i /etc/freeswitch/autoload_configs/modules.conf.xml

SHELL ["/bin/bash"]
HEALTHCHECK --interval=15s --timeout=5s \
    CMD  fs_cli -x status | grep -q ^UP || exit 1

## Ports
# Open the container up to the world.
### 8021 fs_cli, 5060 5061 5080 5081 sip and sips, 64535-65535 rtp
EXPOSE 8021/tcp
EXPOSE 5060/tcp 5060/udp 5080/tcp 5080/udp
EXPOSE 5061/tcp 5061/udp 5081/tcp 5081/udp
EXPOSE 7443/tcp
EXPOSE 5070/udp 5070/tcp
EXPOSE 64535-65535/udp
EXPOSE 16384-32768/udp

VOLUME [ "/etc/freeswitch/" ]

ENTRYPOINT [ "freeswitch" ]

CMD ["version"]
复制代码

1.2 运行freeswitch服务

docker run -d --net=host pbx
复制代码

二、编写示例

2.1 构建简单的js模块工程

mkdir cc && cd cc
npm init -y
npm install xmlrpc --save
复制代码

2.2 获取fs内置号码

代码:

var xmlrpc = require('xmlrpc')

let client = xmlrpc.createClient({
    host: '172.22.24.101',
    port: 8080,
    path: '/RPC2',
    basic_auth: {
        user: 'freeswitch',
        pass: 'works'
    }
})

client.methodCall('freeswitch.api', ["list_users", ""], function (error, value) {
    if (error) {
        console.log('error:', error);
        console.log('req headers:', error.req && error.req._header);
        console.log('res code:', error.res && error.res.statusCode);
        console.log('res body:', error.body);
    } else {
        console.log('value:', value);
    }
});
复制代码

测试:

node app1.js
复制代码

输出:

value: userid|context|domain|group|contact|callgroup|effective_caller_id_name|effective_caller_id_number
1000|default|172.22.24.101|default|sofia/internal/sip:1000@10.8.0.30:55648;ob;fs_nat=yes;fs_path=sip%3A1000%40172.22.3.28%3A55648%3Bob|techsupport|Extension 1000|1000
1001|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1001|1001
1002|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1002|1002
1003|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1003|1003
1004|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1004|1004
1005|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1005|1005
1006|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1006|1006
1007|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1007|1007
1008|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1008|1008
1009|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1009|1009
1010|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1010|1010
1011|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1011|1011
1012|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1012|1012
1013|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1013|1013
1014|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1014|1014
1015|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1015|1015
1016|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1016|1016
1017|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1017|1017
1018|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1018|1018
1019|default|172.22.24.101|default|error/user_not_registered|techsupport|Extension 1019|1019
brian|default|172.22.24.101|default|error/user_not_registered||Brian West|1000
default||172.22.24.101|default|error/user_not_registered|||
example.com||172.22.24.101|default|error/user_not_registered|||
SEP001120AABBCC||172.22.24.101|default|error/user_not_registered|||
1000|default|172.22.24.101|sales|sofia/internal/sip:1000@10.8.0.30:55648;ob;fs_nat=yes;fs_path=sip%3A1000%40172.22.3.28%3A55648%3Bob|techsupport|Extension 1000|1000
1001|default|172.22.24.101|sales|error/user_not_registered|techsupport|Extension 1001|1001
1002|default|172.22.24.101|sales|error/user_not_registered|techsupport|Extension 1002|1002
1003|default|172.22.24.101|sales|error/user_not_registered|techsupport|Extension 1003|1003
1004|default|172.22.24.101|sales|error/user_not_registered|techsupport|Extension 1004|1004
1005|default|172.22.24.101|billing|error/user_not_registered|techsupport|Extension 1005|1005
1006|default|172.22.24.101|billing|error/user_not_registered|techsupport|Extension 1006|1006
1007|default|172.22.24.101|billing|error/user_not_registered|techsupport|Extension 1007|1007
1008|default|172.22.24.101|billing|error/user_not_registered|techsupport|Extension 1008|1008
1009|default|172.22.24.101|billing|error/user_not_registered|techsupport|Extension 1009|1009
1010|default|172.22.24.101|support|error/user_not_registered|techsupport|Extension 1010|1010
1011|default|172.22.24.101|support|error/user_not_registered|techsupport|Extension 1011|1011
1012|default|172.22.24.101|support|error/user_not_registered|techsupport|Extension 1012|1012
1013|default|172.22.24.101|support|error/user_not_registered|techsupport|Extension 1013|1013
1014|default|172.22.24.101|support|error/user_not_registered|techsupport|Extension 1014|1014

+OK


复制代码

2.2 向号码1000发送回声测试

代码:

var xmlrpc = require('xmlrpc')

let client = xmlrpc.createClient({
    host: '172.22.24.101',
    port: 8080,
    path: '/RPC2',
    basic_auth: {
        user: 'freeswitch',
        pass: 'works'
    }
})

client.methodCall('freeswitch.api', ["originate", "user/1000 &echo"], function (error, value) {
    if (error) {
        console.log('error:', error);
        console.log('req headers:', error.req && error.req._header);
        console.log('res code:', error.res && error.res.statusCode);
        console.log('res body:', error.body);
    } else {
        console.log('value:', value);
    }
});
复制代码

测试:

node app1.js
复制代码

结果:

emmmmmmmmm,是不是很简单

最后

这种方式还是蛮轻便的,比ESL更容易做到跨语言、跨平台。

至于开发大型项目会遇到什么坑,我们以后再写系列文章。

文章分类
后端
文章标签