Javascript 优雅地重新连接和断开与聊天频道的连接
考虑以下场景Javascript 优雅地重新连接和断开与聊天频道的连接,javascript,twilio,twilio-api,twilio-programmable-chat,Javascript,Twilio,Twilio Api,Twilio Programmable Chat,考虑以下场景 会员连接聊天频道 成员从聊天频道断开连接 同一成员尝试重新连接到同一聊天频道 我的问题如下…在步骤2和步骤3之间,当成员尝试重新连接到同一通道时,我得到错误,“成员已存在”。为了解决这个问题,我尝试了以下步骤: 2.1呼叫通道。离开() 2.2 channel.leave()成功返回 2.3会员尝试重新连接到同一聊天频道 成员成功连接到同一聊天频道 成功重新连接后,当成员尝试发送消息时,该消息会出现两次。这不是一个有效的解决方案。除了使用channel.leave(),还尝试使用c
const token = await axios.post('/twilio/chat',
{ identity: identity , room: channelName },
{ headers: header })
第二步
const client = await Chat.Client.create(token);
第三步
const channel = await client.getChannelByUniqueName(channelName)
第四步
const joinedChannel = await channel.join();
步骤5
const messages = await channel.getMessages()
messages.items.forEach((message) => {
//Consume unread messages...
})
channel.setAllMessagesConsumed()
第六步。收听添加的消息
channel.on('messageAdded', (message) => {
//When sending a message, this is where I get it duplicated after reconnecting to room
})
const previousChannel = await channel.leave()
第七步。离开海峡时
const previousChannel = await channel.leave()
经过多次尝试和错误,我终于得出以下结论。为了“修复”问题,我必须刷新选项卡,为了重新创建它,我按照前面提到的步骤操作,而不刷新选项卡。。。内存泄漏
Firefox 65.0.1
铬72.0.3626.53
更新:
固定的。在步骤7中,离开房间后,客户端必须正常关闭
client.shutdown()
这不是一个非常友好的解决方案,因为它甚至没有被记录为离开房间的必要步骤。最可能的原因确实是某个地方的内存泄漏。希望这个错误能很快被修复 这里是Twilio开发者福音传道者 我认为重复消息是因为您没有断开
messageAdded
处理程序与旧通道对象的连接。离开频道时,请尝试删除事件侦听器
channel.off('messageAdded', this.messageAdded);
至于离开和重新加入之间的错误,在完全确定成员已离开之前,您可能需要侦听Channel事件。否则,处理错误是一种合理的处理方法。无法直接查看或连接专用频道。进入这些通道的通行证仅通过REST邀请。私人频道的创建者和管理员成员将获得一个独特的邀请,他们可以通过该邀请让人们加入他们的群组。这些仅对参与者可见,并将减少客户端启动时的频道同步时间。如果用户已加入频道并尝试再次加入,则会抛出一个错误: 成员已存在 为了避免这种情况,我在服务器端创建了频道(这是twilio自己推荐的),将两个参与者都添加到该频道(仍然在服务器端)。渠道 其中,
identity
是您登录用户的唯一标识符,invested\u identity
是您希望与之建立一对一聊天的用户的唯一标识符。添加成员函数是,我使用
create\u channel\u和\u add\u members
向前端返回了一个通道SID
,我使用该代码在前端获取通道本身
chatClient.getChannelBySid(sid_of_channel).then((channel)=>{
console.log(channel);
// you can now set event listeners on this channel object directly.
// no need to call join() on it since, our server side code has already added the members(which means they've joined it)
//to receive the messages of this channel, set an event listener on the channel
channel.on('messageAdded', function(message) {
console.log("Messagge is received", message);
});
})
下次您想要获取用户的频道时。您只需获取用户已订阅(即已加入)的频道
chatClient.getSubscribedChannels().then(function(paginator) {
console.log("Your subscribed channels are ", paginator.items);
// each item is a channel, on which you can set event Listeners and do other stuff related to the channel
});
不要忘记在项目的前端和后端包含Twilio sdk。
我希望这对将来的人有所帮助。这很有趣。你能分享你正在使用的代码而不是描述吗?这将使我们更容易找出哪里出了问题,以及如何更好地编写它。谢谢嘿@philnash,添加了代码!非常感谢您的帮助我也有同样的问题,即使在关闭频道messageAdded后,当我重新加入频道时,它在调用getChannelByUniqueName时被复制了。您好@f158087MuhammadZohaib,我不知道您在问什么。如果您问了一个新问题,并向我发送了一个指向该问题的链接,这样我就可以查看您的问题的更多上下文和您自己的代码,可能会更好。当我尝试为添加的
message
事件分离事件侦听器时,off
函数未为频道定义channel.off不是函数。我正在使用v4.0。0@Prime我不确定您为什么不能访问频道上的关闭方法,除非在您的情况下频道
不知何故超出了范围,并且您正在调用关闭
打开未定义
。也许你可以问一个新问题并分享你的一些代码?
chatClient.getChannelBySid(sid_of_channel).then((channel)=>{
console.log(channel);
// you can now set event listeners on this channel object directly.
// no need to call join() on it since, our server side code has already added the members(which means they've joined it)
//to receive the messages of this channel, set an event listener on the channel
channel.on('messageAdded', function(message) {
console.log("Messagge is received", message);
});
})
chatClient.getSubscribedChannels().then(function(paginator) {
console.log("Your subscribed channels are ", paginator.items);
// each item is a channel, on which you can set event Listeners and do other stuff related to the channel
});