<script type="text/javascript" src="https://oss.tcjskd.com/delivery/upload/plugin/websocket/sockjs.min.js"></script>
<script type="text/javascript" src="https://oss.tcjskd.com/delivery/upload/plugin/websocket/stomp.min.js"></script>
<script>
    /**
     * 连接Socket，延时10秒钟发起连接，等待页面Http请求执行完毕，否则会有大概率的连接失败
     * @param args
     */
    function socketListen(args) {
        setTimeout(function () {
            let socket=new SockJS('${ctx!}/socket/connect');;
            let stompClient=Stomp.over(socket);
            stompClient.heartbeat.outgoing = 5000; //若使用STOMP 1.1 版本，默认开启了心跳检测机制（默认值都是10000ms）
            stompClient.heartbeat.incoming = 5000;//客户端不从服务端接收心跳包
            stompClient.connect({}, function (frame) {
                    //lightyear.notify("Socket监听服务连接成功！", 'success');
                    args.forEach(function (item,index) {
                        stompClient.subscribe(item.point,function (data) {
                            //是否打开语音提示
                            let open=getCookie("volume_open");
                            if(open!=null){
                                if(!JSON.parse(open)){
                                    console.log("语音监听已关闭")
                                    return;
                                }
                            }
                            //是否有回调方法,有则调用
                            if(item.method){
                                if(!JSON.parse(document.getElementById("id_notice_volume").getAttribute("open"))){
                                    return;
                                }
                                item.method(JSON.parse(data.body));return;
                            }
                            let prs=JSON.parse(data.body);
                            if(prs.audio){
                                let audio=new Audio(prs.audio);
                                audio.play();
                            }
                            else{
                                let utterThis = new window.SpeechSynthesisUtterance(prs.text);
                                window.speechSynthesis.speak(utterThis);
                            }
                            lightyear.notify(prs.text, prs.type);
                            if(prs.refresh){
                                search();
                            }
                        });
                    });
                },
                function (error) {
                    console.log("服务器连接失败！"+error);
                    //lightyear.notify("Socket监听服务连接失败，即将发起重试，请耐心等待！", 'danger');
                    reLoad();
                });
        },1000)
    }

    /**
     * 刷新页面
     */
    function reLoad() {
        //document.getElementById("js_load").style.display = "inline";
        setTimeout(function(){
            location.reload();
        },10000);
    }
</script>