我在Angular应用中使用Twilio。我会在用户访问某个页面时初始化Twilio设备(让我们将其称为客户页面),以便用户可以呼叫特定客户。在客户控制器中调用此初始化函数:
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
}
问题是,只要用户离开客户页面并返回(不刷新页面),客户控制器就会再次运行,导致_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
然后我离开客户页面并再次返回,无需刷新,因此控制器再次运行初始化代码并复制处理程序:
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();
}
答案 0 :(得分:4)
Twilio.Device目前不支持取消注册侦听器。这似乎是由于它的单身行为。这可能会在将来发生变化,但是现在您可以使用以下内容直接删除侦听器,用于您已绑定的每个事件:
Twilio.Device.instance.removeListener('eventName', handlerFn);
请注意不要removeAllListeners
,因为Device
实例正在侦听自己的某些事件。
答案 1 :(得分:1)
如果您致电Twilio.Device.destroy()
时未处理您的处理程序,则可能会遇到令牌过期问题,因为您需要在致电Device.destroy()
后使用Device.setup()致电new token为了再次使用设备。
确保连接终止的另一种方法是调用Twilio.Device.disconnectAll();
。也许你的处理函数实际上并没有处理终止。
请在签入令牌设置并提供相关日志信息后告诉我它是如何发生的,这些信息可以帮助我更清楚地看到问题。