gpt4 book ai didi

javascript - 如何检查 Twilio.Device 处理程序是否已经存在

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:46 25 4
gpt4 key购买 nike

我在 Angular 应用程序中使用 Twilio。每当用户访问某个页面(我们称之为客户页面)时,我都会初始化 Twilio 设备,以便用户可以调用特定客户。这个初始化函数在客户 Controller 中被调用:

function _initializeDevice(token, connectHandler, disconnectHandler) {
console.log('CALLED INITIALIZE DEVICE');
var device = Twilio.Device;
device.setup(token, {debug: true});
console.log(device);

device.connect(connectHandler);
device.disconnect(disconnectHandler);

device.offline(function() {
_getToken().then(function(result) {
device.setup(result.data.token, {debug: true});
});
});

device.error(_handleTwilioError);
}

这是上面传入的断开连接处理程序:

function onDisconnect() {
console.log('SAVING CALL');
// code to save call
}

问题是,每当用户离开客户页面并返回(没有刷新页面)时,客户 Controller 再次运行,导致 _initializeDevice 函数也再次运行。多个连接/断开/等。处理程序最终会注册到同一个设备,这会导致应该只运行一次的东西运行多次。

这是我的控制台日志示例,用于说明问题...

下面是我第一次转到客户页面并第一次调用 _initializeDevice 时发生的情况:

CALLED INITIALIZE DEVICE
[Device] Setting up PStream
[WSTransport] Opening socket
[WSTransport] attempting to connect
[WSTransport] Socket opened
[PStream] Setting token and publishing listen
[Device] Stream is ready
[Device] Found existing Device; using new token but ignoring options
[PStream] Setting token and publishing listen
[Twilio.PeerConnection] signalingState is "have-local-offer"
[Twilio.PeerConnection] signalingState is "stable"
[Twilio.PeerConnection] iceConnectionState is "checking"
[Twilio.PeerConnection] iceConnectionState is "connected"
[Twilio.PeerConnection] iceConnectionState is "completed"
[Connection] Disconnecting...
[Twilio.PeerConnection] iceConnectionState is "closed"
[Twilio.PeerConnection] signalingState is "closed"
SAVING CALL

然后我离开客户页面并再次返回,没有刷新,所以 Controller 再次运行初始化代码并复制处理程序:

CALLED INITIALIZE DEVICE
[Device] Found existing Device; using new token but ignoring options
[PStream] Setting token and publishing listen
CALLED INITIALIZE DEVICE
[Device] Found existing Device; using new token but ignoring options
[PStream] Setting token and publishing listen
[Device] Found existing Device; using new token but ignoring options
[PStream] Setting token and publishing listen
[Twilio.PeerConnection] signalingState is "have-local-offer"
[Twilio.PeerConnection] signalingState is "stable"
[Twilio.PeerConnection] iceConnectionState is "checking"
[Twilio.PeerConnection] iceConnectionState is "connected"
[Twilio.PeerConnection] iceConnectionState is "completed"
[Connection] Received HANGUP from gateway
[Connection] Disconnecting...
[Twilio.PeerConnection] iceConnectionState is "closed"
[Twilio.PeerConnection] signalingState is "closed"
SAVING CALL
SAVING CALL
SAVING CALL

我尝试使用Twilio.Device.destroy(),但处理程序仍然存在。

如何检查处理程序是否已附加到 Twilio 设备?或者,我应该在我的 Angular 应用程序的其他地方附加事件处理程序吗?

编辑:作为引用,这里是我断开调用的方式(附在一个按钮上):

function hangUp() {
Twilio.Device.disconnectAll();
}

最佳答案

Twilio.Device 目前不支持注销监听器。似乎这是由于它的单例行为。这在未来可能会改变,但现在您可以为您绑定(bind)的每个事件使用以下命令直接删除监听器:

Twilio.Device.instance.removeListener('eventName', handlerFn);

注意不要 removeAllListeners,因为 Device 实例正在监听它自己的一些事件。

关于javascript - 如何检查 Twilio.Device 处理程序是否已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com