Freeswitch代码

1.引入依赖

 Freeswitch依赖版本


    org.freeswitch.esl.client
    esl-client
    0.10.1

2.代码



import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.inbound.IEslEventListener;
import org.freeswitch.esl.client.internal.Context;
import org.freeswitch.esl.client.internal.IModEslApi;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.freeswitch.esl.client.transport.message.EslMessage;
import sun.management.resources.agent;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ClientTest {

    private static class DemoEventListener implements IEslEventListener {

        // @Override
        public void eventReceived(EslEvent event) {
            System.out.println("eventReceived:" + event.getEventName());
        }

        // @Override
        public void backgroundJobResultReceived(EslEvent event) {
            System.out.println("backgroundJobResultReceived:" + event.getEventName());
        }

        @Override
        public void onEslEvent(Context ctx, EslEvent eslEvent) {

            System.out.println("eventReceived:" + eslEvent.getEventName());
            System.out.println("EventBody********:" + eslEvent.getEventHeaders().toString());
            String eventName = eslEvent.getEventName();
            //这里仅演示了CHANNEL_开头的几个常用事件
            if (eventName.startsWith("CHANNEL_")) {
                String calleeNumber = eslEvent.getEventHeaders().get("Caller-Callee-ID-Number");
                String callerNumber = eslEvent.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("用户应答, 主叫:" + callerNumber + " , 被叫:" + calleeNumber);
                        break;
                    case "CHANNEL_HANGUP":
                        String response = eslEvent.getEventHeaders().get("variable_current_application_response");
                        String hangupCause = eslEvent.getEventHeaders().get("Hangup-Cause");
                        System.out.println("用户挂断, 主叫:" + callerNumber + " , 被叫:" + calleeNumber + " , response:" + response + " ,hangup cause:" + hangupCause);
                        break;
                    default:
                        break;
                }

            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        SocketAddress host = new InetSocketAddress("192.168.5.97", 8021);
        String password = "ClueCon";
        int timeoutSeconds = 10;
        Client inboundClient = new Client();
        try {
            inboundClient.connect(host, password, timeoutSeconds);
            inboundClient.addEventListener(new DemoEventListener());
            inboundClient.setEventSubscriptions(IModEslApi.EventFormat.PLAIN, "all");

            /*<--注册分机-->*/
            EslMessage eslMessage1 =inboundClient.sendApiCommand("callcenter_config agent set contact", "21009@default user/1000");
            System.out.println("#####dial eslMessage: "+eslMessage1.getHeaders().toString());
            System.out.println("#####dial eslMessage: "+eslMessage1.getBodyLines().toString()+"=&&="+eslMessage1.getBodyLines().get(0));

            /*<--查询分机状态-->*/
            // EslMessage eslMessage=inboundClient.sendApiCommand("list_users", "group default user 1000");
            //System.out.println("#####eslMessage: "+eslMessage.getHeaders().toString());
            //System.out.println("#####eslMessage: "+eslMessage.getBodyLines().get(0));

            /*<--播放欢迎语-->*/
            //inboundClient.sendBackgroundApiCommand("originate", "user/1000 &playback(/usr/local/freeswitch/sounds/huanying.wav)");
            //inboundClient.sendBackgroundApiCommand("callcenter_config agent set status", "21009@default 'Available'");

            /*<--就绪-->*/
            //inboundClient.sendBackgroundApiCommand("callcenter_config agent set status", "21009@default 'Available'");

            /*<--登出-->*/
            //inboundClient.sendBackgroundApiCommand("callcenter_config agent set status", "21009@default 'Logged Out'");

            /*<--拨打电话异步(没有事件消息)-->*/
            //inboundClient.sendBackgroundApiCommand("originate", "user/1000 1001");

            /*<--拨打电话同步-->*/
            //EslMessage eslMessage1=inboundClient.sendApiCommand("originate", "user/1000 1001");
            //System.out.println("#####dial eslMessage: "+eslMessage1.getHeaders().toString());
            //System.out.println("#####dial eslMessage: "+eslMessage1.getBodyLines().toString()+"&&"+eslMessage1.getBodyLines().get(0));

            /*<--登录到queue-->*/
            //inboundClient.sendBackgroundApiCommand("callcenter_config tier add", "support@default 21009@default 1 1");
            //callcenter_config tier set [key(state|level|position)] [queue name] [agent name] [value] //*<--登录到queue-->*/
            // inboundClient.sendBackgroundApiCommand("callcenter_config tier add", "support@default 21009@default 1 1");

            //EslMessage eslMessage1=inboundClient.sendApiCommand("callcenter_config tier add", "support@default 21009@default 1 1");;
            //#####dial eslMessage: [-ERR Tier already exist!]&&-ERR Tier already exist!
            //System.out.println("#####dial eslMessage: "+eslMessage1.getHeaders().toString());
            //System.out.println("#####dial eslMessage: "+eslMessage1.getBodyLines().toString()+"&&"+eslMessage1.getBodyLines().get(0));

            /*<--这个queue未就绪-->*/
            // support@default 21009@default on break
            //inboundClient.sendBackgroundApiCommand("callcenter_config tier set state", "support@default 21009@default on break");
            System.out.println("###################Fs excuted start############");
        } catch (Exception e) {
            System.out.println("connect fail error: "+e);
        }

        //health-check
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);
        service.scheduleAtFixedRate(() -> {
            System.out.println(System.currentTimeMillis() + " " + inboundClient.canSend());
            if (!inboundClient.canSend()) {
                try {
                    //重连
                    inboundClient.connect(host, password, timeoutSeconds);
                    inboundClient.cancelEventSubscriptions();
                    inboundClient.setEventSubscriptions(IModEslApi.EventFormat.PLAIN, "all");
                } catch (Exception e) {
                    System.out.println("connect fail");
                }
            }
        }, 1, 500000000, TimeUnit.MILLISECONDS);

        System.out.println("other process ...");
    }
}

3.模拟请求,包括异步和同步方法

登录,查询分机状态用同步方法,就绪未就绪,拨打电话用异步方法。

你可能感兴趣的:(Freeswitch,java,开发语言)