【发布时间】:2019-01-30 10:05:28
【问题描述】:
我有一个 React 应用程序,它使用 Twilio 可编程聊天库来提供聊天功能。设置代码通常看起来像这样,全部包装在一个 try/catch 块中:
this.accessManager = new AccessManager(twilioToken.token);
const chatClientOptions = config.debugMode ? { logLevel: 'debug' } : {};
this.chatClient = await TwilioChat.Client.create(twilioToken.token, chatClientOptions);
// Regsiter chatClient to get new access tokens from AccessManager
this.accessManager.on('tokenUpdated', function(am) {
console.log("Chat client getting new token from AccessManager");
// get new token from AccessManager and pass it to the library instance
self.chatClient.updateToken(am.token);
});
// IF accesstoken expries, grab a new one from the server
this.accessManager.on('tokenExpired', async function() {
console.log("Chat access token expired. Requesting new one from server...");
// generate new token here and set it to the accessManager
const updatedToken = (await axios.get(`/users/${user.publicId}/twilioToken`)).data;
self.accessManager.updateToken(updatedToken.token);
});
this.channel = await this.createOrGetChannel();
createOrGetChanne() 获取频道,然后我们可以正确获取消息并发送消息。
90% 的时间,一切正常,但有时 Twilsock(我假设这是底层连接管理库)似乎断开了我的连接,之后客户端无法正确调用 this.channel.sendMessage()。 sendMessage() 调用超时,并且客户端没有要捕获的 Promise 拒绝(因此我无法重新连接并重试)。
启用调试模式后,我在客户端上遇到的错误似乎是:
2018-08-23T22:02:21.425Z Twilsock T: closing socket
然后客户端无法正常工作。如果我切换到另一个应用程序或断开 wifi,然后返回浏览器和聊天页面,我可以在手机上重现这种情况(但并非总是如此)。
这是关闭套接字后失败的消息尝试的样子:
2018-08-23T22:02:28.214Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQb01f6b1df35f4f7ead730d990fcc0ef8
2018-08-23T22:02:45.209Z Chat Messages D: Sending text message One more after another app {}
2018-08-23T22:02:45.209Z Chat Session I: Adding command: sendMessage 11707143-0933-429d-bce0-ac2d37b5f699
2018-08-23T22:02:45.210Z Chat Session D: command arguments: {"channelSid":"something","text":"One more after another app","attributes":"{}","action":"sendMessage"} true
2018-08-23T22:02:45.223Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQd8cdea2425724ba8b1c70970c4d890ea
2018-08-23T22:02:48.234Z Twilsock D: request is timed out
接下来是:
2018-08-23T22:04:04.480Z Chat Session E: Failed to add a command to the session r@https:[some stuff] promiseReactionJob@[native code]
和
Uncaught (in promise) scheduleAttempt@https://[some stuff] promiseReactionJob@[native code]
刷新页面(并重新初始化客户端)可以解决问题。
问题:
1) 由于 sendMessage() 承诺没有被拒绝,我可以做些什么来捕捉它并重试或重新初始化连接?
2) 或者,当 Twilsock 关闭套接字时,是否有一个回调可以挂接到我可能重新连接的地方?
3) 是否需要添加任何其他连接管理最佳实践以使我的应用尽可能健壮?
任何帮助表示赞赏。谢谢,谢谢,谢谢!
【问题讨论】:
-
Twilio 支持对此有一张票。这是他们的 Twilsock 库中的一个错误,它似乎在没有通知更高级别的聊天库的情况下断开了连接。当我听到更多关于修复的信息时,会更新这个问题。
标签: javascript reactjs twilio chat twilio-programmable-chat