Java教程

使用java esl写一个FreeSwitchEventListener 服务

本文主要是介绍使用java esl写一个FreeSwitchEventListener 服务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用java esl写一个FreeSwitchEventListener 服务

package com.sip;

import com.google.common.base.Throwables;
import org.freeswitch.esl.client.IEslEventListener;
import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.outbound.AbstractOutboundClientHandler;
import org.freeswitch.esl.client.outbound.AbstractOutboundPipelineFactory;
import org.freeswitch.esl.client.outbound.SocketClient;
import org.freeswitch.esl.client.outbound.example.SimpleHangupOutboundHandler;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class FreeSwitchEventListener {

    private static Logger logger = LoggerFactory.getLogger(FreeSwitchEventListener.class);

    public static void main(String[] args) {
        try {
            final Client inboudClient = new Client();
            inboudClient.connect("192.168.1.196", 8021, "ClueCon", 10);

            inboudClient.addEventListener(new IEslEventListener() {
                @Override
                public void eventReceived(EslEvent eslEvent) {
                    System.out.println(eslEvent.getEventName());
                    String eventname = eslEvent.getEventName();
                    if ("CHANNEL_HANGUP_COMPLETE".equals(eventname)) {
                        System.out.println("进入挂机完成事件");
                        Map<String, String> dd = eslEvent.getEventHeaders();
                        System.out.println("variable_effective_caller_id_number is::::::" + dd.get("variable_effective_caller_id_number"));
                    }
                }

                @Override
                public void backgroundJobResultReceived(EslEvent eslEvent) {
                    System.out.println("事件名称++++"+eslEvent.getEventName());
                }
            });
            inboudClient.setEventSubscriptions("plain", "all");
       //发送命令
            String re= inboudClient.sendAsyncApiCommand("originate", "user/1000 1001");
            System.out.println("呼叫结果:" + re);

            final SocketClient outboundServer = new SocketClient(
                    8084,
                    new AbstractOutboundPipelineFactory() {
                        @Override
                        protected AbstractOutboundClientHandler makeHandler() {
                  //这个类可以自己重写一个
                            return new SimpleHangupOutboundHandler();
                        }
                    }
            );
            outboundServer.start();
        } catch (Throwable t) {
            Throwables.propagate(t);
        }
    }
}

pom文件添加以下依赖

 <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.freeswitch.esl.client</groupId>
            <artifactId>org.freeswitch.esl.client</artifactId>
            <version>0.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>25.1-jre</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.17.Final</version>
            <type>bundle</type>
        </dependency>
    </dependencies>

 

这篇关于使用java esl写一个FreeSwitchEventListener 服务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!