Twilio可编程聊天-从JS客户端断开连接

时间:2018-08-23 23:53:33

标签: javascript reactjs twilio chat twilio-programmable-chat

我有一个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)是否需要添加其他任何连接管理最佳实践,以使我的应用程序尽可能健壮?

任何帮助表示赞赏。谢谢,谢谢,谢谢!

1 个答案:

答案 0 :(得分:1)

Twilio刚刚发布了其聊天SDK的3.1.0版本,并修复了此连接问题。升级到此版本应该可以解决您的问题。

相关问题