简体   繁体   English

Socket.io/ARI错误:发出警报

[英]Socket.io / ARI error: Emit Alert

I am currently trying to add a bit of error handling which shows up in the front end of my web application at the moment, but I don't know how to get socket.io to emit an alert box to the front end so far example, I put in an exetension number to bridge call, if that call doesnt exist then display alert. 我目前正在尝试添加一些错误处理,该错误处理此刻显示在我的Web应用程序的前端,但是到目前为止,我不知道如何获取socket.io来向前端发出警报框,我输入了分机号来桥接呼叫,如果该呼叫不存在,则显示警报。

The code should make it seem a bit more clear. 该代码应该使它看起来更加清晰。

Client Side 客户端

When button is clicked emit the data found in the input box to the server side 单击按钮时,将在输入框中找到的数据发送到服务器端

$(document).on('click', '.bridge', function () {
    var bridge = $('input').val();
    socket.emit('bridge', bridge);
    console.log(bridge)
});

Server Side 服务器端

Store the data received from client side into a var and then call the stasis function. 将从客户端接收的数据存储到var中,然后调用stasis函数。

io.sockets.on('connection', function (socket) {
socket.on('bridge', function (data) {
  bridgeCall(data);
  console.log("Reached listener for bridge")
});
    });

Stasis function 停滞功能

Bridge the channel which you have just passed from client to server side using ARI client commands, user will be bridged and will show in stasis application. 使用ARI客户端命令将您刚刚从客户端传递到服务器端的通道桥接,用户将被桥接并显示在停滞应用程序中。 I am trying to somehow emit an alert to front end web page if there's an error. 如果出现错误,我试图以某种方式向前端网页发出警报。

function bridgeCall(exeten) {
    console.log("Extension being bridged: " + exeten);
    client.channels.originate({
      endpoint : "SIP/" + exeten,
      app : 'bridge-hold'
    },
      function (err, channel) {
      console.log("Channel: " + exeten + " does not exist");
      alert("Channel: " + exeten + " does not exist");
    });

  }

I simply am trying to emit an alert if there is an error to the front end. 我只是想在前端出现错误时发出警报。

You have to emit an event that the client can listen to and then act upon it. 您必须发出一个事件,客户端可以监听并对其采取行动。

First on your client have: 首先在您的客户上有:

socket.on("my_error",function(err){alert(err)}

Then on the server side, pass the socket on to the bridgeCall function: 然后在服务器端,将套接字传递给bridgeCall函数:

bridgeCall(data,socket);

Then in bridgeCall, do: 然后在bridgeCall中,执行以下操作:

function bridgeCall(exeten,socket) {
    console.log("Extension being bridged: " + exeten);
    client.channels.originate({
      endpoint : "SIP/" + exeten,
      app : 'bridge-hold'
    },
      function (err, channel) {
          console.log("Channel: " + exeten + " does not exist");
          socket.emit("my_error","Channel: " + exeten + " does not exist");
    });

  }

How I ended up getting it working based off the original answer! 我最终如何根据原始答案使它正常工作!

First on your client you have to insert the following to receive the event to display the error if there is one: 首先在客户端上,您必须插入以下内容以接收事件以显示错误(如果存在):

socket.on("error", function (err) {
$("<div title='Error'>" + err + "</div>").dialog({
    modal : true,
    buttons : {
        Ok : function () {
            $(this).dialog("close");
        }
    }
});

}); });

Then on the server side, pass the socket on to the bridgeCall function: 然后在服务器端,将套接字传递给bridgeCall函数:

bridgeCall(data,socket); bridgeCall(数据,套接字);

Then in bridgeCall, you need to store the error message in a global var and then use it in the error function, ARI/Stasis is weird like that so do the following: 然后在bridgeCall中,您需要将错误消息存储在全局变量中,然后在错误函数中使用它,ARI / Stasis很奇怪,因此请执行以下操作:

  function bridgeCall(exeten) {
    console.log("Extension being bridged: " + exeten);
    client.channels.originate({
      endpoint : "SIP/" + exeten,
      app : 'ARI-Web-App'
    },
      function (err) {
              if (err) {
      console.log("Channel: " + exeten + " does not exist");
      error = ("Channel: " + exeten + " does not exist");
      Error();
      }
    });

  }

Error function you call at the end of bridgeCall then looks like this, inserted along with the server side socket logic: 然后,您在bridgeCall末尾调用的错误函数如下所示,与服务器端套接字逻辑一起插入:

 function Error() {
io.sockets.emit("error",error);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM